forked from kelilipan/eliza-line-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (49 loc) · 1.76 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
from eliza import Eliza
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage, ImageSendMessage
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot import (
LineBotApi, WebhookHandler
)
import os
from flask import Flask, request, abort
from dotenv import load_dotenv
load_dotenv()
eliza = Eliza(lang='id')
app = Flask(__name__)
# get LINE_CHANNEL_ACCESS_TOKEN from your environment variable
line_bot_api = LineBotApi(os.environ.get('LINE_CHANNEL_ACCESS_TOKEN'))
# get LINE_CHANNEL_SECRET from your environment variable
handler = WebhookHandler(os.environ.get('LINE_CHANNEL_SECRET'))
@app.route("/")
def home():
return "henlo world"
@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)
print(body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
payload = TextSendMessage(text=eliza.respond(event.message.text))
if event.message.text.lower()=='p':
payload = ImageSendMessage(
original_content_url='https://raw.githubusercontent.com/raisoturu/eliza-line-chatbot/master/assets/images/p.jpg',
preview_image_url='https://raw.githubusercontent.com/raisoturu/eliza-line-chatbot/master/assets/images/p.jpg'
)
line_bot_api.reply_message(event.reply_token,payload)
if __name__ == "__main__":
app.run()