Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .idea/heroku-basic-flask.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

387 changes: 387 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

269 changes: 257 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
@@ -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 """
<h1>Hello heroku</h1>
<p>It is currently {time}.</p>
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": []
})
)

<img src="http://loremflickr.com/600/400" />
""".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()
16 changes: 14 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-3.5.1
python-3.6.8