OSDN Git Service

exit debug mode
[simple-tornado-bot/simple-tornado-bot.git] / bot.py
1 # -*- coding: utf-8 -*-
2 """
3 Created on Sat Sep  1 11:18:39 2018
4
5 @author: fuke masasi
6 """
7
8 import tornado.ioloop
9 import tornado.web
10 import json, os
11 from linebot import LineBotApi, WebhookParser
12 from linebot.exceptions import InvalidSignatureError
13 from linebot.models import MessageEvent, TextMessage, TextSendMessage
14
15
16 class WebHookHandler(tornado.web.RequestHandler):
17     def post(self):
18         signature = json.load(self.request.headers['X-Line-Signature'])
19         data = json.load(self.request.body)
20         try:
21             events = webhook.parse(data, signature)
22         except InvalidSignatureError:
23             self.abort(400)
24         for event in events:
25             if not isinstance(event,MessageEvent):
26                 continue
27             if not isinstance(event.message,TextMessage):
28                 continue
29             linebot.reply_message(
30                 event.rply_token,
31                 TextSendMessage(text=event.message.text)
32             )
33
34 application = tornado.web.Application([(r'/callback',WebHookHandler)],{
35         #'debug':True
36         })
37
38 if __name__ == '__main__':
39     ch_id = os.environ['Channel_ID']
40     ch = os.environ['Channel_Secret']
41     linebot = LineBotApi(ch_id)
42     webhook = WebhookParser(ch)  
43     application.listen(5000)
44     tornado.ioloop.IOLoop.instance().start()