-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
152 lines (128 loc) · 4.41 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from flask import Flask, request, abort, render_template
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
#======這裡是呼叫的檔案內容=====
from message import *
from new import *
from Function import *
from mongodb_function import *
#======這裡是呼叫的檔案內容=====
#======python的函數庫==========
import os
import time
#======python的函數庫==========
#======讓heroku不會睡著======
import threading
import requests
def wake_up_heroku():
while 1==1:
url = '你的herokuapp網址' + 'heroku_wake_up'
res = requests.get(url)
if res.status_code==200:
print('喚醒heroku成功')
else:
print('喚醒失敗')
time.sleep(28*60)
threading.Thread(target=wake_up_heroku).start()
#======讓heroku不會睡著======
app = Flask(__name__,template_folder='templates')
static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
# Channel Access Token
line_bot_api = LineBotApi('你的Channel AcessToken')
# Channel Secret
handler = WebhookHandler('你的Channel Secret')
@app.route("/")
def index():
return render_template("./index.html")
@app.route("/heroku_wake_up")
def heroku_wake_up():
return "Hey!Wake Up!!"
# 監聽所有來自 /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)
write_one_data(eval(body))
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):
msg = event.message.text
if '最新合作廠商' in msg:
message = imagemap_message()
line_bot_api.reply_message(event.reply_token, message)
elif '最新活動訊息' in msg:
message = buttons_message()
line_bot_api.reply_message(event.reply_token, message)
elif '註冊會員' in msg:
message = Confirm_Template()
line_bot_api.reply_message(event.reply_token, message)
elif '旋轉木馬' in msg:
message = Carousel_Template()
line_bot_api.reply_message(event.reply_token, message)
elif '圖片畫廊' in msg:
message = test()
line_bot_api.reply_message(event.reply_token, message)
elif '功能列表' in msg:
message = function_list()
line_bot_api.reply_message(event.reply_token, message)
#======MongoDB操作範例======
elif '@讀取' in msg:
datas = read_many_datas()
datas_len = len(datas)
message = TextSendMessage(text=f'資料數量,一共{datas_len}條')
line_bot_api.reply_message(event.reply_token, message)
elif '@查詢' in msg:
datas = col_find('events')
message = TextSendMessage(text=str(datas))
line_bot_api.reply_message(event.reply_token, message)
elif '@對話紀錄' in msg:
datas = read_chat_records()
print(type(datas))
n = 0
text_list = []
for data in datas:
if '@' in data:
continue
else:
text_list.append(data)
n+=1
data_text = '\n'.join(text_list)
message = TextSendMessage(text=data_text[:5000])
line_bot_api.reply_message(event.reply_token, message)
elif '@刪除' in msg:
text = delete_all_data()
message = TextSendMessage(text=text)
line_bot_api.reply_message(event.reply_token, message)
#======MongoDB操作範例======
else:
message = TextSendMessage(text=msg)
line_bot_api.reply_message(event.reply_token, message)
@handler.add(PostbackEvent)
def handle_message(event):
print(event.postback.data)
@handler.add(MemberJoinedEvent)
def welcome(event):
uid = event.joined.members[0].user_id
gid = event.source.group_id
profile = line_bot_api.get_group_member_profile(gid, uid)
name = profile.display_name
message = TextSendMessage(text=f'{name}歡迎加入')
line_bot_api.reply_message(event.reply_token, message)
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)