Skip to content

Commit b5b6965

Browse files
committed
first commit
1 parent d79a908 commit b5b6965

15 files changed

+748
-163
lines changed

.idea/inspectionProfiles/profiles_settings.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/turisbot-dev.iml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+609
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Procfile

100644100755
File mode changed.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# turisbot

__pycache__/fb.cpython-36.pyc

-2.05 KB
Binary file not shown.

app-commadnline.py

-68
This file was deleted.

app.old.py

100644100755
+98-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,68 @@
1-
import sys
1+
# Implemented with Bottle
2+
3+
import os
24
from wit import Wit
3-
from fb import getFirstPage
5+
import requests
6+
from bottle import Bottle, request, debug, response
7+
from fb import getDataPage, searchPage
8+
from random import randrange
9+
from sys import argv
10+
11+
# Declare some constants
12+
FB_VERIFY_TOKEN = os.environ['FB_VERIFY_TOKEN']
13+
# FB_PAGE_ID = os.environ['FB_PAGE_ID']
14+
FB_ACCESS_TOKEN = os.environ['FB_ACCESS_TOKEN']
15+
WIT_TOKEN = os.environ['WIT_TOKEN']
16+
17+
# Setup Bottle Server
18+
debug(True)
19+
app = Bottle()
20+
21+
22+
# Facebook Messenger GET Webhook
23+
@app.get('/webhook')
24+
def messenger_webhook():
25+
verify_token = request.query.get('hub.verify_token')
26+
if verify_token == FB_VERIFY_TOKEN:
27+
challenge = response.status = 200
28+
challenge = request.query.get('hub.challenge')
29+
return challenge
30+
else:
31+
return 'Invalid Request or Verification Token'
432

5-
if len(sys.argv) != 2:
6-
print('usage: python ' + sys.argv[0] + ' <wit-token>')
7-
exit(1)
8-
access_token = sys.argv[1]
33+
34+
# Facebook Messenger POST Webhook
35+
@app.post('/webhook')
36+
def messenger_post():
37+
data = request.json
38+
print('Data Received')
39+
print(data)
40+
if data['object'] == 'page':
41+
for entry in data['entry']:
42+
messages = entry['messaging']
43+
if messages[0]:
44+
# Get the first message
45+
message = messages[0]
46+
fb_id = message['sender']['id']
47+
text = message['message']['text']
48+
client.run_actions(session_id=fb_id, message=text)
49+
else:
50+
# Returned another event
51+
return 'Received Different Event'
52+
return None
53+
54+
55+
def fb_message(sender_id, text):
56+
data = {
57+
'recipient': {'id': sender_id},
58+
'message': {'text': text}
59+
}
60+
# prepare query
61+
qs = 'access_token=' + FB_ACCESS_TOKEN
62+
# send post request to messenger
63+
resp = requests.post('https://graph.facebook.com/me/messages?' + qs,
64+
json=data)
65+
return resp.content
966

1067

1168
def first_entity_value(entities, entity):
@@ -18,33 +75,63 @@ def first_entity_value(entities, entity):
1875

1976

2077
def send(request, response):
21-
print(response['text'])
78+
# sender function
79+
fb_id = request['session_id']
80+
text = response['text']
81+
print('Fb_di:')
82+
print(fb_id)
83+
print('text: ')
84+
print(response)
85+
fb_message(fb_id, text)
2286

2387

2488
def merge(request):
89+
# get the context, type
2590
context = request['context']
2691
entities = request['entities']
92+
print('In merge(), context: ' + str(context) +
93+
' entities:' + str(entities))
2794

2895
if 'place' in context:
2996
del context['place']
3097
category = first_entity_value(entities, 'category')
3198
if category:
3299
context['cat'] = category
100+
if 'ack' in context:
101+
print(context['ack'])
102+
del context['ack']
33103
return context
34104

35105

36106
def select_place(request):
37107
context = request['context']
38-
context['place'] = getFirstPage(context['cat'])
39-
return context
108+
data = getDataPage(context['cat'])
109+
if data is not None:
110+
i = randrange(0, len(data) - 1, 1)
111+
dataPage = searchPage(data[i]['id'])
112+
msj = data[i]['name']
113+
if 'street' in dataPage['location']:
114+
msj = msj + ", esta en las calles: " + str(dataPage['location']['street'])
115+
if 'overall_star_rating' in dataPage:
116+
msj = msj + " y tiene un promedio de " + \
117+
str(dataPage['overall_star_rating']) + ' estrellas'
118+
context['place'] = msj
119+
return context
120+
else:
121+
context['place'] = 'No place found'
122+
return context
40123

41124

125+
# Setup Actions
42126
actions = {
43127
'send': send,
44128
'merge': merge,
45129
'select-place': select_place,
46130
}
47131

132+
# Setup Wit Client
133+
client = Wit(access_token=WIT_TOKEN, actions=actions)
48134

49-
client = Wit(access_token=access_token, actions=actions)
50-
client.interactive()
135+
if __name__ == '__main__':
136+
# Run Server
137+
app.run(host='0.0.0.0', port=argv[1])

app.py

100644100755
File mode changed.

fb.old.py

-83
This file was deleted.

fb.py

100644100755
File mode changed.

requirements.txt

100644100755
-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Flask==0.11.1
21
Jinja2==2.8
32
MarkupSafe==0.23
43
Werkzeug==0.11.10

0 commit comments

Comments
 (0)