-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.py
executable file
·86 lines (67 loc) · 2.47 KB
/
start.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
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
from PIL import Image
from io import StringIO
import requests
import random
import json
import math
import time
import datetime
#---------------- self define module ----------------
import text_push as text_push
import text_reply as text_reply
#---------------- self define variables ----------------
from mykey import *
#---------------- line settings ----------------
# Channel Access Token
line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
# Channel Secret
handler = WebhookHandler(LINE_CHANNEL_SECRET)
#---------------------------------------------------
app = Flask(__name__)
# 監聽所有來自 /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)
def handle_message(event):
print(event)
message_send_time = float(event.timestamp)/1000
message_get_time = float(time.time())
msg_type = event.message.type
if event.message.text == "info":
output_message = TextSendMessage(text=str(event))
line_bot_api.reply_message(event.reply_token, output_message)
if event.message.text.lower() == "speed" :
output_message = ("【收到訊息時間】\n{} 秒\n【處理訊息時間】\n{} 秒".format(message_get_time-message_send_time,float(time.time())-message_get_time))
output_message = text_reply.text_reply_message(user_message)
line_bot_api.reply_message(event.reply_token, output_message)
if msg_type == "sticker":
output_message = StickerSendMessage(package_id='2',sticker_id=str(random.randint(140,180)))
line_bot_api.reply_message(event.reply_token, output_message)
elif msg_type == "text":
user_message = event.message.text
output_message = text_reply.text_reply_message(user_message)
line_bot_api.reply_message(event.reply_token, output_message)
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)