40行で書ける!
Serverless LINE BOT
LINE Corporation

Tasuku OKUDA
Introduction
• Hadoop administrator/developer

• LINEスタンプ: https://store.line.me/stickershop/product/1464176

• Python @ LINE

• メッセージ流量やスタンプ販売数などの統計Batch

• 複雑なJob dependencyの管理

• セキュリティ監査やSpanモニタリングツールの開発

• LINE Messaging API SDK
LINE Messaging API
LINE Messaging API
• https://business.line.me/ja/services/bot

• Doc: https://devdocs.line.me/ja/

• LINEサーバ経由で、ユーザ側のLINE
appと開発者側のシステムを繋ぐAPI

• JSON Webhook

• Imagemap、Carouselなど、Messaging
APIならではのメッセージも
LINE Bot SDK for Python
• Github: https://github.com/line/line-
bot-sdk-python

• PyPi: https://pypi.python.org/pypi/line-
bot-sdk/1.3.0

• Messaging APIの様々なRequest/
ResponseをPython objectで扱えるSDK

• WebhookHandlerでEvent/MessageType
毎の処理を分離して書ける
LINE Messaging API
+
Serverless
Serverless LINE BOT
LINE app
(User)
LINE
Messaging API Server
AWS
API Gateway
AWS
Lambda
Event
Webhook

POST req
POST request

to Messaging API
Response
chalice

Python Serverless Microframework
$ pip install line-bot-sdk chalice
$ chalice new-project line-bot
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
from chalice import Chalice
app = Chalice(app_name=‘line-bot’)
from linebot import LineBotApi
from linebot import WebhookHandler
line_bot_api = LineBotApi(os.environ['LINEBOT_ACCESS_TOKEN'])
handler = WebhookHandler(os.environ['LINEBOT_SECRET'])
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
@app.route('/callback', methods=['POST'])
def callback():
signature = app.current_request.headers['X-Line-Signature']
body = app.current_request.raw_body.decode('utf-8')
try:
handler.handle(body, signature)
except InvalidSignatureError as e:
return Response({‘error': e.message}, status_code=400)
return Response({'ok': True})
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=‘Reply: {}’.format(event.message.text))
)
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
$ chalice deploy
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
40行でecho bot!
https://gist.github.com/okdtsk/af70f33e2197b759848df8d6a800ce0a
LINE BOTはかんたん!
作
っ
て
み
て
ね
Thank you!

40行で書ける! Serverless LINE BOT