-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.old.py
executable file
·137 lines (115 loc) · 3.59 KB
/
app.old.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
# Implemented with Bottle
import os
from wit import Wit
import requests
from bottle import Bottle, request, debug, response
from fb import getDataPage, searchPage
from random import randrange
from sys import argv
# Declare some constants
FB_VERIFY_TOKEN = os.environ['FB_VERIFY_TOKEN']
# FB_PAGE_ID = os.environ['FB_PAGE_ID']
FB_ACCESS_TOKEN = os.environ['FB_ACCESS_TOKEN']
WIT_TOKEN = os.environ['WIT_TOKEN']
# Setup Bottle Server
debug(True)
app = Bottle()
# Facebook Messenger GET Webhook
@app.get('/webhook')
def messenger_webhook():
verify_token = request.query.get('hub.verify_token')
if verify_token == FB_VERIFY_TOKEN:
challenge = response.status = 200
challenge = request.query.get('hub.challenge')
return challenge
else:
return 'Invalid Request or Verification Token'
# Facebook Messenger POST Webhook
@app.post('/webhook')
def messenger_post():
data = request.json
print('Data Received')
print(data)
if data['object'] == 'page':
for entry in data['entry']:
messages = entry['messaging']
if messages[0]:
# Get the first message
message = messages[0]
fb_id = message['sender']['id']
text = message['message']['text']
client.run_actions(session_id=fb_id, message=text)
else:
# Returned another event
return 'Received Different Event'
return None
def fb_message(sender_id, text):
data = {
'recipient': {'id': sender_id},
'message': {'text': text}
}
# prepare query
qs = 'access_token=' + FB_ACCESS_TOKEN
# send post request to messenger
resp = requests.post('https://graph.facebook.com/me/messages?' + qs,
json=data)
return resp.content
def first_entity_value(entities, entity):
if entity not in entities:
return None
val = entities[entity][0]['value']
if not val:
return None
return val['value'] if isinstance(val, dict) else val
def send(request, response):
# sender function
fb_id = request['session_id']
text = response['text']
print('Fb_di:')
print(fb_id)
print('text: ')
print(response)
fb_message(fb_id, text)
def merge(request):
# get the context, type
context = request['context']
entities = request['entities']
print('In merge(), context: ' + str(context) +
' entities:' + str(entities))
if 'place' in context:
del context['place']
category = first_entity_value(entities, 'category')
if category:
context['cat'] = category
if 'ack' in context:
print(context['ack'])
del context['ack']
return context
def select_place(request):
context = request['context']
data = getDataPage(context['cat'])
if data is not None:
i = randrange(0, len(data) - 1, 1)
dataPage = searchPage(data[i]['id'])
msj = data[i]['name']
if 'street' in dataPage['location']:
msj = msj + ", esta en las calles: " + str(dataPage['location']['street'])
if 'overall_star_rating' in dataPage:
msj = msj + " y tiene un promedio de " + \
str(dataPage['overall_star_rating']) + ' estrellas'
context['place'] = msj
return context
else:
context['place'] = 'No place found'
return context
# Setup Actions
actions = {
'send': send,
'merge': merge,
'select-place': select_place,
}
# Setup Wit Client
client = Wit(access_token=WIT_TOKEN, actions=actions)
if __name__ == '__main__':
# Run Server
app.run(host='0.0.0.0', port=argv[1])