File size: 1,719 Bytes
2cf7795
 
 
 
76eaf5a
 
 
 
 
 
 
 
 
3a2e91a
a4c60e7
0613521
9a45992
2cf7795
 
 
 
 
 
 
 
 
 
4a7f8d2
 
 
 
 
 
3a2e91a
4a7f8d2
3a2e91a
4a7f8d2
 
2cf7795
4a7f8d2
3a2e91a
4a7f8d2
2cf7795
4a7f8d2
 
 
 
 
3a2e91a
4a7f8d2
 
0ea605e
4a7f8d2
 
 
 
2cf7795
 
 
0379ea1
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
from flask import Flask, request, abort
import json
import os

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
)
 
line_bot_api = LineBotApi("ZwnEWwK46AUmRDOFXJOwUh1TqAELahP+kxHY0iBevF8JiEUe0CBQpkW2Gpo3C38LOaIX7hpW4IuTOSOjVof+DX9XgZg8MTeh5Iz+NDLlw6KKMtqNsyR6zt9CMnsso21ieifC/S8dcgXx3iKlhdS7UAdB04t89/1O/w1cDnyilFU=")
line_handler = WebhookHandler("7b9984bbc5c62445a79b95b15e34b727")

working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"

app = Flask(__name__)

@app.route('/')
def main():
    return '您好'

@app.route("/webhook", methods=['POST'])
def result():
    # 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:
        line_handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    return 'OK'

@line_handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    global working_status

    if event.message.text == "再見":
        working_status = True
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text="Bye!"))
        return

    if working_status:
        out = event.message.text
        if not out:
            out = "不懂您的意思!"
        line_bot_api.reply_message(
            event.reply_token,
            TextSendMessage(text=out))

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=7860)