This repository has been archived by the owner on Feb 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
184 lines (157 loc) · 4.75 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# KODING UNTUK FULLFILLMENT DIALOGFLOW
from flask import Flask
import os
from flask import request
from flask import make_response
import dialogflow
import logging
import json
import random
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(message)s')
app = Flask(__name__)
# all requests from dialogflow will go throught webhook function
@app.route('/webhook', methods=['POST'])
def webhook():
# get dialogflow request
req = request.get_json(silent=True, force=True)
logger.info("Incoming request: %s", req)
intent = get_intent_from_req(req)
logger.info('Detected intent %s', intent)
# user asks for today's special
if intent == 'sticker':
# pick any :)
response = {
"fulfillmentMessages": [
{
"text": {
"text": [
"olrit"
]
},
"platform": "LINE"
},
{
"payload": {
"line": {
"type": "sticker",
"packageId": "11538",
"stickerId": "51626507"
}
},
"platform": "LINE"
}
]
}
else:
# something went wrong here, we got unknow intent or request without intent
response = {
'fulfillmentText': 'Yeah, I\'m here but still learning, please wait for part 2 of this tutorial!',
}
res = create_response(response)
return res
def get_intent_from_req(req):
""" Get intent name from dialogflow request"""
try:
intent_name = req['queryResult']['intent']['displayName']
except KeyError:
return None
return intent_name
def create_response(response):
""" Creates a JSON with provided response parameters """
# convert dictionary with our response to a JSON string
res = json.dumps(response, indent=4)
logger.info(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
# if __name__ == '__main__': //for ngrok
# app.run(debug=False, port=5000, host='0.0.0.0', threaded=True)
# Koding with heroku directly to line
# # mybot/app.py
# import os
# from decouple import config
# from flask import (
# Flask, request, abort
# )
# from linebot import (
# LineBotApi, WebhookHandler
# )
# from linebot.exceptions import InvalidSignatureError
# from linebot.models import (
# MessageEvent, TextMessage, TextSendMessage,StickerMessage, StickerSendMessage
# )
# app = Flask(__name__)
# # get LINE_CHANNEL_ACCESS_TOKEN from your environment variable
# line_bot_api = LineBotApi(
# config("LINE_CHANNEL_ACCESS_TOKEN",
# default=os.environ.get('LINE_ACCESS_TOKEN'))
# )
# # get LINE_CHANNEL_SECRET from your environment variable
# handler = WebhookHandler(
# config("LINE_CHANNEL_SECRET",
# default=os.environ.get('LINE_CHANNEL_SECRET'))
# )
#
#
# @app.route("/callback", methods=['POST'])
# def callback():
# 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_text_message(event):
# mess = [StickerSendMessage(package_id=11538, sticker_id=51626503), StickerSendMessage(package_id=11538, sticker_id=51626507)]
# if event.message.text == "Hello" :
# line_bot_api.reply_message(
# event.reply_token,
# TextSendMessage(text=event.message.text)
# )
# elif event.message.text == "Sleep" :
# line_bot_api.reply_message(
# event.reply_token,
# StickerSendMessage(package_id=1, sticker_id=1)
# )
# elif event.message.text == "Spirit" :
# line_bot_api.reply_message(
# event.reply_token,
# mess
# )
# else :
# line_bot_api.reply_message(
# event.reply_token,
# TextSendMessage(text="Sorry we don't know what you mean yet :)")
# )
# if __name__ == "__main__":
# port = int(os.environ.get('PORT', 5000))
# app.run(host='0.0.0.0', port=port)