-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (66 loc) · 2.83 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
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import *
import sys
import os
import configparser
import sys
sys.setrecursionlimit(2000)
# Add the directory containing your module to the Python path (wants absolute paths)
scriptpath = '/app'
sys.path.append(os.path.abspath(scriptpath))
import ImageScrapper
from IfoodieCrawler import IFoodie
app = Flask(__name__)
conf = configparser.ConfigParser()
conf.read("config.ini")
line_bot_api = LineBotApi(conf["LineBot"]["channelaccesstoken"]) # Channel Access Token
handler = WebhookHandler(conf["LineBot"]["channelsecret"]) # Channel Secret
# 監聽所有來自 /callback 的 Post Request
@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)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# 處理訊息
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
if event.message.text == "找菜單":
#food = IFoodie("台中市" ,"雞排") #使用者傳入的訊息文字
#line_bot_api.reply_message( # 回應前五間最高人氣且營業中的餐廳訊息文字
# event.reply_token,
# TextSendMessage(text=food.scrape())
#)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="請輸入餐廳名稱:")
)
if event.message.type == 'text':
GoogleImageUrlQuery = ImageScrapper.GoogleImageUrl(event.message.text + " 菜單")
ImageUrl = GoogleImageUrlQuery.GetGoogleImageUrlByKeyword()
image = ImageSendMessage(original_content_url=ImageUrl, preview_image_url=ImageUrl)
line_bot_api.reply_message(event.reply_token, image)
else:
GoogleImageUrlQuery = ImageScrapper.GoogleImageUrl(event.message.text + " 菜單")
ImageUrl = GoogleImageUrlQuery.GetGoogleImageUrlByKeyword()
image = ImageSendMessage(original_content_url=ImageUrl, preview_image_url=ImageUrl)
line_bot_api.reply_message(event.reply_token, image)
# elif event.message.text == "影片":
# video = VideoSendMessage(original_content_url='影片網址', preview_image_url='預覽的圖片網址')
# line_bot_api.reply_message(event.reply_token, video)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)