概要
Serverless FrameworkでLinebotを作成するための記事です。
アジェンダ
Serverless Frameworkの設定
Linebotの設定
パラメータの変更
デプロイ・実行
1 Serverless Frameworkの設定
aws-nodejsテンプレートのインストール
~/develop/study/linebot $ serverless create --template aws-nodejs --path linebot
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/kawamurakouji/develop/study/linebot/linebot"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v2.52.0
-------'
Serverless: Successfully generated boilerplate for template: "aws-nodejs"
~/develop/study $ cd linebot
~/develop/study/linebot $ yarn init
~/develop/study/linebot/linebot $ yarn add serverless-offline
※Serverless Frameworkを触ったことのない人はServerless FrameworkでHello World作成(入門編)を参考にしてね!!!
2.Linebotの設定
Messaging APIのチャンネルを作成する。わからない人は公式ドキュメントを見ながらやってね!!
3 実装
ディレクトリ構成
~/develop/study/linebot/linebot $ tree -I node_modules
.
├── handler.js
├── line.js ←新規作成
├── package.json
├── serverless.yml ←修正
└── yarn.lock
0 directories, 5 files
line.jsのソースコード
line.js
const request = require('superagent');
const endpoint = 'https://api.line.me/v2/bot/message/reply';
const accessToken = 'Linebotのアクセストークン';
module.exports.webhook = (event, context, callback) => {
var body = JSON.parse(event.body);
body.events.forEach(function(data) {
var replyToken = data.replyToken;
var message = data.message.text
request.post(endpoint)
.set('Content-type', 'application/json; charset=UTF-8')
.set('Authorization', 'Bearer ' + accessToken)
.send({
replyToken: replyToken,
messages: [
{
type: 'text',
text: message,
},
],
})
.end(function(error){
if (error) {
console.log(error);
}
});
});
callback(null, {statusCode: 200, body: JSON.stringify({})});
};
serverless.yml
service: linebot
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
stage: dev
region: ap-northeast-1
plugins:
- serverless-offline
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
lineWebhook:
handler: line.webhook
events:
- http:
path: line/webhook
method: post
4 デプロイ・実行
デプロイコマンド
~/develop/study/linebot/linebot $ serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
........
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service linebot.zip file to S3 (30.47 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...................................................
Serverless: Stack update finished...
Service Information
service: linebot
stage: dev
region: ap-northeast-1
stack: linebot-dev
resources: 18
api keys:
None
endpoints:
GET - https://URL/dev/hello
POST - https://URL/dev/line/webhook ←このURLに注目
functions:
hello: linebot-dev-hello
lineWebhook: linebot-dev-lineWebhook
layers:
None
POSTのURLをLineBotのWebhookに登録。これで、オウム返しbotの完成。
↧