From f0d72b75cac0508ca75bb15def0416f3b35e7ae0 Mon Sep 17 00:00:00 2001 From: TPPonmat Date: Thu, 27 Jun 2019 10:08:31 +0700 Subject: [PATCH] add new --- .idea/heroku-basic-flask.iml | 14 + .../inspectionProfiles/profiles_settings.xml | 7 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 387 ++++++++++++++++++ app.py | 269 +++++++++++- requirements.txt | 16 +- runtime.txt | 2 +- 9 files changed, 698 insertions(+), 15 deletions(-) create mode 100644 .idea/heroku-basic-flask.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/heroku-basic-flask.iml b/.idea/heroku-basic-flask.iml new file mode 100644 index 00000000..7806842b --- /dev/null +++ b/.idea/heroku-basic-flask.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..c23ecacb --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..543294c4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..d747d3c3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..42f40ac1 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,387 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1561604262452 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app.py b/app.py index b56b7317..c1354bb8 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,263 @@ -from flask import Flask -from datetime import datetime +import json +import requests +from flask import Flask, request, abort + +from linebot import LineBotApi +from linebot.models import TextSendMessage +from linebot.exceptions import LineBotApiError +from linebot import ( + LineBotApi, WebhookHandler +) + +from linebot.exceptions import ( + InvalidSignatureError +) +from linebot.models import ( + MessageEvent, TextMessage, TextSendMessage, +) + app = Flask(__name__) -@app.route('/') -def homepage(): - the_time = datetime.now().strftime("%A, %d %b %Y %l:%M %p") +line_channel_access_token = 'FT9zaPscYQJbAQtWq4bHl1iM4Kpx7tHthvQZn4KcCj4eta1Nfq0T+jBpYxGtxPtrRrj3/GD6QLDCQxThjS52itajTMpnvsnJzRwuMj9xivIQIN1fkIEslMimIRNnyXF/heI7MCcCtpCNcbD2Y4EDewdB04t89/1O/w1cDnyilFU=' +line_bot_api = LineBotApi(line_channel_access_token) +handler = WebhookHandler('96363b03e615241b7d68c6ef2ba8dcf2') +Authorization = "Bearer {}".format(line_channel_access_token) + + +@app.route("/callback", methods=['POST']) +def callback(): + # get X-Line-Signature header value + signature = request.headers['X-Line-Signature'] + + # get request body as text + body = request.get_data(as_text=True) + app.logger.info("Request body: " + body) + + body = json.loads(body) + print (body) + + reply_token = body['events'][0]["replyToken"] + print("reply_token: {}".format(reply_token)) + + event_type = body['events'][0]['type'] + print("event_type: {}".format(event_type)) + + if event_type == "message": + message_type = body['events'][0]['message']['type'] + # print("message_type: {}".format(message_type)) + if message_type == "text": + text = body['events'][0]['message']['text'] + print("text: {}".format(text)) + if "home" in text or "Home" in text: + print("replying text:{}".format(text)) + reply_menu3(reply_token) + elif text == "weather": + line_bot_api.reply_message(reply_token, TextSendMessage(text='ตอนนี้อุณหภูมิ ที่บ้าน 30 C ')) + elif text == "energy": + line_bot_api.reply_message(reply_token, TextSendMessage(text='การใช้ไไฟ้าที่บ้านวันนี้ 3.4 หน่วย คิดเป็นเงิน 12 บาท')) + + return '',200 + + + +def reply_menu(reply_token): + print('from reply menu') + print("Authorization:{}".format(Authorization)) + print("reply_token:{}".format((reply_token))) + + # line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World222!')) + + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [ + { + "type": "text", + "text": "เชิญเลือกเมนูได้เลยครับ", + "quickReply": { + "items": [ + { + "type": "action", + "imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2', + "action": { + "type": "message", + "label": "home", + "text": "Home" + } + }, + { + "type": "action", + "imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2', + "action": { + "type": "message", + "label": "weather", + "text": "weather" + } + }, + { + "type": "action", + "imageUrl": 'https://drive.google.com/uc?export=download&id=1Hsd2w0YF1ThUIQEiXXm-6s67fmV7azh2', + "action": { + "type": "message", + "label": "energy", + "text": "energy" + } + } + ] + } + } + ] + }) + ) + print('Response HTTP Status Code: {status_code}'.format( + status_code=response.status_code)) + print('Response HTTP Response Body: {content}'.format( + content=response.content)) + + +def reply_menu2(reply_token): + print('from reply menu') + print("Authorization:{}".format(Authorization)) + print("reply_token:{}".format((reply_token))) + + # line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World222!')) + + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [{ + "type": "template", + "altText": "this is a carousel template", + "template": { + "type": "carousel", + "actions": [], + "columns": [ + { + "thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg", + "text": "Energy", + "actions": [ + { + "type": "message", + "label": "click", + "text": "energy" + } + ] + }, + { + "thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg", + "text": "Weather", + "actions": [ + { + "type": "message", + "label": "click", + "text": "weather" + } + ] + } + ] + } +}] + }) + ) + + +def reply_menu3(reply_token): + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [{ + "type": "template", + "altText": "this is a carousel template", + "template": { + "type": "carousel", + "actions": [], + "columns": [ + { + "thumbnailImageUrl": "https://sv1.picz.in.th/images/2019/06/27/1CCpqZ.th.jpg", + "text": "weather", + "actions": [ + { + "type": "message", + "label": "weather", + "text": "weather" + } + ] + }, + { + "thumbnailImageUrl": "https://d3n8a8pro7vhmx.cloudfront.net/edonsw/pages/995/attachments/original/1386210667/green_energy_320.jpg", + "text": "energy", + "actions": [ + { + "type": "message", + "label": "energy", + "text": "energy" + } + ] + } + ] + } +}] + }) + ) + + + + +def reply_menu4(reply_token): + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [] + }) + ) + - return """ -

Hello heroku

-

It is currently {time}.

+def reply_menu5(reply_token): + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [] + }) + ) - - """.format(time=the_time) -if __name__ == '__main__': - app.run(debug=True, use_reloader=True) +def reply_menu6(reply_token): + response = requests.post( + url="https://api.line.me/v2/bot/message/reply", + headers={ + "Content-Type": "application/json", + "Authorization": Authorization, + }, + data=json.dumps({ + "replyToken": str(reply_token), + "messages": [] + }) + ) +if __name__ == "__main__": + app.run() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index cef5a165..45cd53f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,14 @@ -Flask -gunicorn +certifi==2019.6.16 +chardet==3.0.4 +Click==7.0 +Flask==1.0.2 +future==0.17.1 +gunicorn==19.9.0 +idna==2.8 +itsdangerous==1.1.0 +Jinja2==2.10.1 +line-bot-sdk==1.12.1 +MarkupSafe==1.1.1 +requests==2.22.0 +urllib3==1.25.3 +Werkzeug==0.15.4 diff --git a/runtime.txt b/runtime.txt index 78082e3a..9fbd3bf0 100644 --- a/runtime.txt +++ b/runtime.txt @@ -1 +1 @@ -python-3.5.1 +python-3.6.8