├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── bin ├── dev └── line-bot ├── config.js ├── package.json └── src └── line-bot.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "stage-0" 5 | ] 6 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | test_logs 4 | *.log 5 | 6 | # Dependency directory 7 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 8 | node_modules 9 | 10 | # Babel.js 11 | lib/ 12 | 13 | # cache folder 14 | .cache 15 | 16 | # Mac DS_Store files 17 | .DS_Store 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 aszx87410 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Line 聊天機器人簡單範例 2 | 可參考 [Line 官方文件](https://developers.line.me/bot-api/getting-started-with-bot-api-trial) 3 | 4 | ## 使用 5 | 6 | ``` 7 | git clone https://github.com/aszx87410/line-bot.git 8 | ``` 9 | 10 | 把 `config.js` 裡面的三個 key 換成自己的 11 | 12 | 再來先裝必要的套件 13 | 14 | ``` 15 | npm install 16 | ``` 17 | 18 | 19 | 裝好之後,如果要 debug 的話,就是 20 | 21 | ``` 22 | npm run dev 23 | ``` 24 | 25 | 如果想直接跑的,那就 26 | 27 | ``` 28 | npm run build 29 | npm start 30 | ``` 31 | 32 | ## 教學 33 | [[教學] Line API](http://huli.logdown.com/posts/726082-line-bot-api-tutorial) 34 | 35 | -------------------------------------------------------------------------------- /bin/dev: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var nodemon = require('nodemon'); 4 | var babel = require("babel-core"); 5 | var gaze = require('gaze'); 6 | var fs = require('fs'); 7 | var path = require('path'); 8 | 9 | // Watch the src and transpile when changed 10 | gaze('src/**/*', function(err, watcher) { 11 | if (err) throw err; 12 | watcher.on('changed', function(sourceFile) { 13 | console.log(sourceFile + " has changed"); 14 | try { 15 | targetFile = path.relative(__dirname, sourceFile).replace(/\/src\//, '/lib/'); 16 | targetFile = path.resolve(__dirname, targetFile); 17 | fs.writeFile(targetFile, babel.transformFileSync(sourceFile).code); 18 | } catch (e) { 19 | console.error(e.message, e.stack); 20 | } 21 | }); 22 | }); 23 | 24 | try { 25 | // Run and watch dist 26 | nodemon({ 27 | script: 'bin/line-bot', 28 | ext: 'js json', 29 | watch: 'lib' 30 | }); 31 | } catch (e) { 32 | console.error(e.message, e.stack); 33 | } 34 | 35 | process.once('SIGINT', function() { 36 | process.exit(0); 37 | }); -------------------------------------------------------------------------------- /bin/line-bot: -------------------------------------------------------------------------------- 1 | require("../lib/line-bot"); -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | CHANNEL_ID: ' ', 3 | CHANNEL_SERECT: ' ', 4 | MID: ' ' 5 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "line-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "author": "Li-Hu (http://huli.logdown.com)", 6 | "license": "MIT", 7 | "dependencies": { 8 | "babel-polyfill": "^6.5.0", 9 | "babel-runtime": "^6.5.0", 10 | "body-parser": "^1.14.2", 11 | "express": "^4.13.4", 12 | "request": "^2.65.0" 13 | }, 14 | "devDependencies": { 15 | "babel-cli": "^6.5.1", 16 | "babel-core": "^6.5.1", 17 | "babel-istanbul": "^0.6.0", 18 | "babel-preset-es2015": "^6.5.0", 19 | "babel-preset-stage-0": "^6.5.0", 20 | "babel-register": "^6.5.1", 21 | "gaze": "^0.5.2", 22 | "nodemon": "^1.8.1" 23 | }, 24 | "scripts": { 25 | "dev": "npm run build && node bin/dev", 26 | "build": "./node_modules/.bin/babel src/ -d lib/", 27 | "start": "node ./bin/line-bot", 28 | "prepublish": "npm run build" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/line-bot.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import bodyParser from 'body-parser'; 3 | import request from 'request'; 4 | import config from '../config'; 5 | 6 | const app = express(); 7 | const port = '7123'; 8 | const { CHANNEL_ID, CHANNEL_SERECT, MID } = {...config}; 9 | const LINE_API = 'https://trialbot-api.line.me/v1/events'; 10 | 11 | app.use(bodyParser.json()); 12 | app.use(bodyParser.urlencoded({ extended: true })); 13 | 14 | app.post('/callback', (req, res) => { 15 | const result = req.body.result; 16 | for(let i=0; i console.log(`listening on port ${port}`)); 24 | 25 | function sendTextMessage(sender, text) { 26 | 27 | const data = { 28 | to: [sender], 29 | toChannel: 1383378250, 30 | eventType: '138311608800106203', 31 | content: { 32 | contentType: 1, 33 | toType: 1, 34 | text: text 35 | } 36 | }; 37 | 38 | console.log('send: ', data); 39 | 40 | request({ 41 | url: LINE_API, 42 | headers: { 43 | 'Content-Type': 'application/json; charset=UTF-8', 44 | 'X-Line-ChannelID': CHANNEL_ID, 45 | 'X-Line-ChannelSecret': CHANNEL_SERECT, 46 | 'X-Line-Trusted-User-With-ACL': MID 47 | }, 48 | method: 'POST', 49 | body: JSON.stringify(data) 50 | }, function(error, response, body) { 51 | if (error) { 52 | console.log('Error sending message: ', error); 53 | } else if (response.body.error) { 54 | console.log('Error: ', response.body.error); 55 | } 56 | console.log('send response: ', body); 57 | }); 58 | } --------------------------------------------------------------------------------