-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfb.py
executable file
·213 lines (174 loc) · 5.6 KB
/
fb.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import requests
import os
from pprint import pprint as pp
import time
try:
FB_ACCESS_TOKEN = os.environ['FB_ACCESS_TOKEN']
except:
FB_ACCESS_TOKEN = ""
def setToken():
global FB_ACCESS_TOKEN
FB_ACCESS_TOKEN = raw_input('Token: >')
def setSearchIdsUrl(topic):
# set info
latitude = "-2.9183953"
longitude = "-79.0362543"
radio = "5000" # en metros
return ("https://graph.facebook.com/v2.8/search?q=" + topic +
"&type=place¢er=" + latitude + "%2C" + longitude + "&distance="
+ radio + "&access_token=" + FB_ACCESS_TOKEN)
# funtion that from a Url outputs jsonData
def getDataUrl(url):
webURL = requests.get(url)
return webURL.json()
def getFirstPage(query):
requests = getDataUrl(setSearchIdsUrl(query))
print(requests)
if 'error' in requests:
print(requests['error']['message'])
return None
if 'data' not in requests:
print('No data')
return None
print("Facebook Data: \n Query: %s" %query)
# pp(requests['data'][0]['name'])
return(requests['data'][0]['name'])
def getDataPage(query):
requests = getDataUrl(setSearchIdsUrl(query))
# print(requests)
if 'data' in requests:
# check if data is empty
if requests['data']:
# print('FB.py sent data - getDataPage()')
# pp(requests['data'])
return(requests['data'])
elif 'error' in requests and len(requests['data']) > 0:
print(requests['error']['message'])
return None
else:
print('No data')
return None
def searchPage(pageId):
url = ("https://graph.facebook.com/v2.8/" + pageId + "?fields=name%2Cabout%2Coverall_star_rating%2Clocation%2Chours%2Cpicture&access_token=" + FB_ACCESS_TOKEN)
requests = getDataUrl(url)
print(requests)
if 'name' in requests:
print('FB.py sent data')
print("Facebook Data: \n PageId: %s" % pageId)
pp(requests)
return(requests)
elif 'error' in requests:
print(requests['error']['message'])
return None
else:
print('No data')
return None
def listIdPages(data):
# list the ids of Pages
for place in data['data']:
print(place['id'])
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 fb_boton_message(sender_id, text_boton):
data = {
"recipient":{"id" : sender_id},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":text_boton,
"buttons":[
{
"type":"web_url",
"url":"https://edzzn.com",
"title":"Show Website"
},
{
"type":"postback",
"title":"Start Chatting",
"payload":"USER_DEFINED_PAYLOAD"
}
]
}
}
}
}
# 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 is_Open(hours):
day_actual = time.strftime("%a", time.localtime())
hora_actual = time.strftime("%H:%M", time.localtime())
horas = []
is_open = True
for hour in hours:
if day_actual.upper() in hour.upper():
horas.append(hour)
for hora in horas:
if "open" in hora and hora_actual < hours[hora]:
is_open = False
print hora, hora_actual
if "close" in hora and hora_actual > hours[hora]:
is_open = False
print hora, hora_actual
return is_open
def fb_generic_message(sender_id, pages_id, maxi):
elements = []
if len(pages_id) < maxi:
maxi = len(pages_id) - 1
for i in range(maxi):
page_info = searchPage(pages_id[i]['id'])
try:
about = page_info['about']
except:
about = "Tiene un promedio de: " + str(page_info['overall_star_rating']) + " estrellas"
elem_i = {
"title":page_info['name'],
"image_url": page_info['picture']['data']['url'],
"subtitle":about,
"default_action": {
"type": "web_url",
"url": "https://www.facebook.com/" + page_info['id'],
# "webview_height_ratio": "tall",
# "fallback_url": "https://edzzn.com/"
},
"buttons":[
{
"type":"web_url",
"url":"https://www.messenger.com/t/" +page_info['id'],
"title":"Enviar un mensaje"
}
]
}
elements.append(elem_i)
data = {
"recipient":{"id" : sender_id},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements": elements
}
}
}
}
# prepare query
qs = 'access_token=' + FB_ACCESS_TOKEN
# send post request to messenger
resp = requests.post('https://graph.facebook.com/v2.6/me/messages?' + qs,
json=data)
return resp.content