2018年12月26日 星期三

[Line Bot] 建置Line Bot環境(使用Bottender + Heroku)

Bottender

Bottender是chot bot框架,著重async function語法
安裝時,node.js的版本需大於7.6

安裝Bottender的環境參考下篇
https://bottender.js.org/docs/GettingStarted



Heroku

安裝完heroku cli後
$ heroku login
將repository clone至本機
$ heroku git:clone -a <heroku app name>
$ cd <heroku app name>
push 至hekoku
$ git add .
$ git commit -am "make it better"
$ git push heroku master


Line Developer

登入至line developer新增帳號
若要刪除自動回覆訊息,要進入Line @Manager移除


訊息 -> 自動回覆訊息 delete
記下Line Developer頁面底下的Channel secret、Channel access token
並於到Heroku頁面設定參數 (Settings -> Config Vars新增變數)




另外要Use Webhhok要啟用Enabled
Webhook URL加入heroku app的網址


JS有兩的地方要調整,accessToken和channelSecret改成讀參數

bottender.config.js

module.exports = {
  // line
  accessToken: process.env.ACCESS_TOKEN,
  channelSecret: process.env.CHANNEL_SECRET,
};

sendMethod改成 reply
index.js

const { LineBot } = require('bottender');
const { createServer } = require('bottender/express');

const config = require('./bottender.config.js');

const bot = new LineBot({
  accessToken: config.accessToken,
  channelSecret: config.channelSecret,
  sendMethod: 'reply', // Default: 'push'
});

bot.onEvent(async context => {
  await context.sendText('Hello World');
});

const server = createServer(bot);

server.listen((process.env.PORT || 5000), () => {
  console.log('server is running on 5000 port...');
});


執行畫面


Ref:
http://rainstingtw.blogspot.com/2018/07/how-to-use-bottender-with-heroku-line-bot.html
來寫個氣象機器人吧!


 

沒有留言:

張貼留言