/
app.py
135 lines (123 loc) · 5.13 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
#-*-coding: utf-8 -*-
##from __future__ import absolute_import
###
from flask import Flask, jsonify, render_template, request
import json
import numpy as np
import requests
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
SourceUser, SourceGroup, SourceRoom,
TemplateSendMessage, ConfirmTemplate, MessageAction,
ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
PostbackAction, DatetimePickerAction,
CameraAction, CameraRollAction, LocationAction,
CarouselTemplate, CarouselColumn, PostbackEvent,
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
MemberJoinedEvent, MemberLeftEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, SpacerComponent, IconComponent, ButtonComponent,
SeparatorComponent, QuickReply, QuickReplyButton,
ImageSendMessage)
from linebot.models.template import *
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
LineBotApiError, InvalidSignatureError
)
from web_scraping_the_standard import *
from web_scraping_rotten_tomatoes import *
app = Flask(__name__)
lineaccesstoken = 'CSdgziqlU0NO/Sf3MmAkinm92OOKEUYNwNV9xlzta/Z85ZdUUc6sQ5eHl2aJrCGgmR6nFvtNYhhEOG1kG8B0XxayECT8jqSHdszjg7derd6JKI/fZqVDpA5iv9+qICJxk43PeGbYDoQG3Ph7YVAblAdB04t89/1O/w1cDnyilFU='
linesecret = '74d75448fe78ae3cf293ab1a8cfce9b0'
line_bot_api = LineBotApi(lineaccesstoken)
handler = WebhookHandler(linesecret)
####################### new ########################
@app.route('/')
def index():
return "This is Line Chatbot"
@app.route("/callback", methods=['POST']) ## or 'webhook' it's actually the same
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)
# handle webhook body
try:
handler.handle(body, signature)
except LineBotApiError as e:
print("Got exception from LINE Messaging API: %s\n" % e.message)
for m in e.error.details:
print(" %s: %s" % (m.property, m.message))
print("\n")
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
global news
global movies
text = event.message.text
print(event.message.text)
##line_bot_api.reply_message(
##event.reply_token,
##TextSendMessage(text=event.message.text))
##line_bot_api.broadcast(
##TextSendMessage(text=get_thestandard_news()))
if text.lower().strip() == 'news':
news=1
buttons_template = ButtonsTemplate(
title='Choose', text='เลือกสำนักข่าวที่ต้องการ', actions=[
MessageAction(label='The Standard', text='The Standard'),
MessageAction(label='BBC', text='BBC')
])
template_message = TemplateSendMessage(
alt_text='Buttons alt text', template=buttons_template)
line_bot_api.reply_message(
event.reply_token,template_message)
elif text == 'The Standard' and news==1:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='กรุณารอสักครู่'))
line_bot_api.push_message(
event.source.user_id,
TextSendMessage(text=get_thestandard_news()))
news=0
elif text.lower().strip() == 'movies':
movies=1
buttons_template = ButtonsTemplate(
title='Choose', text='เลือกเว็บไซต์ที่ต้องการ', actions=[
MessageAction(label='Rotten Tomatoes', text='Rotten Tomatoes'),
MessageAction(label='IMDb', text='IMDb')
])
template_message = TemplateSendMessage(
alt_text='Buttons alt text', template=buttons_template)
line_bot_api.reply_message(
event.reply_token,template_message)
elif text == 'Rotten Tomatoes' and movies==1:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='กรุณารอสักครู่'))
line_bot_api.push_message(
event.source.user_id,
TextSendMessage(text=get_rottentomatoes()))
movies=0
elif text == 'Hello, Friend':
ans = '''Hello, Friend
ขอบคุณที่แอดเราเป็นเพื่อนนะ
ตอนนี้ indyspaceapp อยู่ในช่วงกำลังพัฒนาสามารถเสนอความคิดเห็นมาได้เลย
https://forms.gle/Em3AKBT8mem6ZwqL9'''
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=ans))
else:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='งงค้าบ'))
if __name__ == "__main__":
app.run()