diff --git a/Utils/FacebookAPI.py b/Utils/FacebookAPI.py index 4797987..0a7189b 100644 --- a/Utils/FacebookAPI.py +++ b/Utils/FacebookAPI.py @@ -2,10 +2,10 @@ from flask import url_for def get_user_fb(token, user_id): - r = requests.get("https://graph.facebook.com/v2.6/" + user_id, - params={"fields": "first_name,last_name,profile_pic,locale,timezone,gender" - ,"access_token": token - }) + r = requests.get("https://graph.facebook.com/v2.8/" + user_id, + params={"fields": "first_name, last_name, profile_pic, locale, timezone, gender" + , "access_token": token + }) if r.status_code != requests.codes.ok: print r.text return @@ -13,20 +13,20 @@ def get_user_fb(token, user_id): return user def show_typing(token, user_id, action='typing_on'): - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps({ "recipient": {"id": user_id}, "sender_action": action }), headers={'Content-type': 'application/json'}) - if r.status_code != requests.codes.ok: + if r.status_code != requests.codes.ok: print r.text def send_message(token, user_id, text): """Send the message text to recipient with id recipient. """ - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps({ "recipient": {"id": user_id}, @@ -39,37 +39,37 @@ def send_message(token, user_id, text): def send_picture(token, user_id, imageUrl, title="", subtitle=""): if title != "": data = {"recipient": {"id": user_id}, - "message":{ - "attachment": { - "type": "template", - "payload": { - "template_type": "generic", - "elements": [{ - "title": title, - "subtitle": subtitle, - "image_url": imageUrl - }] - } - } + "message":{ + "attachment": { + "type": "template", + "payload": { + "template_type": "generic", + "elements": [{ + "title": title, + "subtitle": subtitle, + "image_url": imageUrl + }] + } } - } + } + } else: - data = { "recipient": {"id": user_id}, + data = {"recipient": {"id": user_id}, "message":{ - "attachment": { - "type": "image", - "payload": { - "url": imageUrl - } - } + "attachment": { + "type": "image", + "payload": { + "url": imageUrl + } + } } - } - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + } + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps(data), headers={'Content-type': 'application/json'}) if r.status_code != requests.codes.ok: - print r.text + print r.text def send_quick_replies_yelp_search(token, user_id): # options = [Object {name:value, url:value}, Object {name:value, url:value}] @@ -84,17 +84,17 @@ def send_quick_replies_yelp_search(token, user_id): } ] data = json.dumps({ - "recipient":{ "id": user_id }, - "message":{ - "text":"Do you want to find more results? :D", - "quick_replies": quickRepliesOptions - } - }) + "recipient":{"id": user_id}, + "message":{ + "text":"Do you want to find more results? :D", + "quick_replies": quickRepliesOptions + } + }) data = data.encode('utf-8') - r = requests.post("https://graph.facebook.com/v2.6/me/messages", - params={"access_token": token}, - data=data, - headers={'Content-type':'application/json'}) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", + params={"access_token": token}, + data=data, + headers={'Content-type':'application/json'}) if r.status_code != requests.codes.ok: print r.text @@ -108,32 +108,32 @@ def send_quick_replies_yelp_save_location(token, user_id, location=None): }] if location != None: # Place this here to make sure this option comes second rename = {"content_type":"text", - "title": "I'll rename it", - "payload": 'yelp-save-location-rename' - } + "title": "I'll rename it", + "payload": 'yelp-save-location-rename' + } quickRepliesOptions.append(rename) no = {"content_type":"text", - "title": "No, thank you", - "payload": 'yelp-save-location-no' - } + "title": "No, thank you", + "payload": 'yelp-save-location-no' + } quickRepliesOptions.append(no) - + if location == None: message = "Do you want me to save this location?" else: message = "Do you want me to save this location as \"%s\" for the future? :D"%(location) data = json.dumps({ - "recipient":{ "id": user_id }, - "message":{ - "text": message, - "quick_replies": quickRepliesOptions - } - }) + "recipient":{"id": user_id}, + "message":{ + "text": message, + "quick_replies": quickRepliesOptions + } + }) data = data.encode('utf-8') - r = requests.post("https://graph.facebook.com/v2.6/me/messages", - params={"access_token": token}, - data=data, - headers={'Content-type':'application/json'}) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", + params={"access_token": token}, + data=data, + headers={'Content-type':'application/json'}) if r.status_code != requests.codes.ok: print r.text @@ -145,24 +145,24 @@ def send_quick_replies_yelp_suggest_location(token, user_id, locations): location_name = location['name'][:17] + "..." if len(location['name']) > 20 else location['name'] obj = {"content_type":"text", - "title": location_name, - "payload": 'yelp-cached-location-%s'%(i) - } + "title": location_name, + "payload": 'yelp-cached-location-%s'%(i) + } quickRepliesOptions.append(obj) i += 1 - + data = json.dumps({ - "recipient":{ "id": user_id }, - "message":{ - "text":"Or choose one of the saved locations :D", - "quick_replies": quickRepliesOptions - } - }) + "recipient":{"id": user_id}, + "message":{ + "text":"Or choose one of the saved locations :D", + "quick_replies": quickRepliesOptions + } + }) data = data.encode('utf-8') - r = requests.post("https://graph.facebook.com/v2.6/me/messages", - params={"access_token": token}, - data=data, - headers={'Content-type':'application/json'}) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", + params={"access_token": token}, + data=data, + headers={'Content-type':'application/json'}) if r.status_code != requests.codes.ok: print r.text @@ -174,74 +174,74 @@ def send_yelp_results(token, user_id, businesses): subtitle = "" if 'price' in business and business['price'] != "": subtitle += business['price'] + " - " - subtitle += business['address'] + subtitle += business['address'] if 'distance' in business: subtitle += " (" + str(business['distance']) + " mi.)" if 'is_open_now' in business: - subtitle += "\n" + "Open now - " if business['is_open_now'] else "\n" + subtitle += "\n" + "Open now - " if business['is_open_now'] else "\n" if 'hours_today' in business and len(business['hours_today']) > 0: subtitle += "Hours today: %s"%(business['hours_today']) subtitle += "\n" + business['categories'] img_url = business['image_url'] if business['image_url'] != "" else url_for('static', filename='assets/img/empty-placeholder.jpg', _external=True) - + obj = { - "title": business['name'] + " - " + business['rating'] , - "image_url": img_url, - "subtitle": subtitle, - "buttons":[ - { + "title": business['name'] + " - " + business['rating'], + "image_url": img_url, + "subtitle": subtitle, + "buttons":[ + { "type":"web_url", "url": business['url'], "title":"View details" - } - # ,{ - # "type":"postback", - # "title":"Start Chatting", - # "payload":"USER_DEFINED_PAYLOAD" - # } - ] } - options.append(obj) - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + # ,{ + # "type":"postback", + # "title":"Start Chatting", + # "payload":"USER_DEFINED_PAYLOAD" + # } + ] + } + options.append(obj) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps({ - "recipient": {"id": user_id}, - "message":{ - "attachment":{ - "type":"template", - "payload":{ - "template_type":"generic", - "elements": options - } - } - } + "recipient": {"id": user_id}, + "message":{ + "attachment":{ + "type":"template", + "payload":{ + "template_type":"generic", + "elements": options + } + } + } }), headers={'Content-type': 'application/json'}) if r.status_code != requests.codes.ok: print r.text def send_url(token, user_id, text, title, url): - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps({ - "recipient": {"id": user_id}, - "message":{ - "attachment":{ - "type":"template", - "payload":{ - "template_type":"button", - "text": text, - "buttons":[ - { - "type":"web_url", - "url": url, - "title": title - } - ] - } - } - } + "recipient": {"id": user_id}, + "message":{ + "attachment":{ + "type":"template", + "payload":{ + "template_type":"button", + "text": text, + "buttons":[ + { + "type":"web_url", + "url": url, + "title": title + } + ] + } + } + } }), headers={'Content-type': 'application/json'}) if r.status_code != requests.codes.ok: @@ -274,28 +274,28 @@ def send_intro_screenshots(app, token, user_id): "subtitle": "Then your memo in the same/separate message" } news = { - "title": "Keep you updated", - "image_url": url_for('static', filename="assets/img/intro/6-news.jpg", _external=True), - "subtitle": "With the most trending news" + "title": "Keep you updated", + "image_url": url_for('static', filename="assets/img/intro/6-news.jpg", _external=True), + "subtitle": "With the most trending news" } options = [chat_speak, location_text, location_gps, location_save, memo1, news] - r = requests.post("https://graph.facebook.com/v2.6/me/messages", - params={"access_token": token}, - data=json.dumps({ - "recipient": {"id": user_id}, - "message":{ - "attachment":{ - "type":"template", - "payload":{ - "template_type":"generic", - "elements": options - } - } - } - }), - headers={'Content-type': 'application/json'}) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", + params={"access_token": token}, + data=json.dumps({ + "recipient": {"id": user_id}, + "message":{ + "attachment":{ + "type":"template", + "payload":{ + "template_type":"generic", + "elements": options + } + } + } + }), + headers={'Content-type': 'application/json'}) if r.status_code != requests.codes.ok: print r.text @@ -309,73 +309,69 @@ def send_trending_news(token, user_id, posts): "subtitle": post['subtitle'], "buttons":[ { - "type":"web_url", - "url": post['url'], - "title":"Read more" + "type":"web_url", + "url": post['url'], + "title":"Read more" } ] } - options.append(obj) - r = requests.post("https://graph.facebook.com/v2.6/me/messages", + options.append(obj) + r = requests.post("https://graph.facebook.com/v2.8/me/messages", params={"access_token": token}, data=json.dumps({ - "recipient": {"id": user_id}, - "message":{ - "attachment":{ - "type":"template", - "payload":{ - "template_type":"generic", - "elements": options - } - } - } + "recipient": {"id": user_id}, + "message":{ + "attachment":{ + "type":"template", + "payload":{ + "template_type":"generic", + "elements": options + } + } + } }), headers={'Content-type': 'application/json'}) if r.status_code != requests.codes.ok: print r.text -def set_menu(): - r = requests.post("https://graph.facebook.com/v2.6/me/thread_settings", - params={"access_token": 'EAADhYj0lr14BAGP2HCx2mcYcxQbtQG7iXfaGpOieFsGlgJEYv0Y74bdIYtQ3UcnK1kktfUCDInciDniwTOm1c6l2Fq2GEBsm0Lu4syz5HUc41MGepASZBuXw1caZBkZBGRX5kIZCT7q5QOkiPVnZC3n8iBcqVMCBGnZCiSgscQogZDZD'}, - data=json.dumps({ - "setting_type" : "call_to_actions", - "thread_state" : "existing_thread", - "call_to_actions":[ - { - "type":"postback", - "title":"What can you do?", - "payload":"OPTIMIST_HELP" - }, - { - "type":"web_url", - "title":"View Facebook Page", - "url": "https://www.facebook.com/optimistPrimeBot/" - } - ] - }), - headers={'Content-type': 'application/json'}) +def set_menu(token): + r = requests.post("https://graph.facebook.com/v2.8/me/thread_settings", + params={"access_token": token}, + data=json.dumps({ + "setting_type" : "call_to_actions", + "thread_state" : "existing_thread", + "call_to_actions":[ + { + "type":"postback", + "title":"What can you do?", + "payload":"OPTIMIST_HELP" + }, + { + "type":"web_url", + "title":"View Facebook Page", + "url": "https://www.facebook.com/optimistPrimeBot/" + } + ] + }), + headers={'Content-type': 'application/json'}) print r.content if r.status_code != requests.codes.ok: print r.text -def set_get_started_button(): - - r = requests.post("https://graph.facebook.com/v2.6/me/thread_settings", - params={"access_token": 'EAADhYj0lr14BAGP2HCx2mcYcxQbtQG7iXfaGpOieFsGlgJEYv0Y74bdIYtQ3UcnK1kktfUCDInciDniwTOm1c6l2Fq2GEBsm0Lu4syz5HUc41MGepASZBuXw1caZBkZBGRX5kIZCT7q5QOkiPVnZC3n8iBcqVMCBGnZCiSgscQogZDZD'}, - data=json.dumps({ - "setting_type":"call_to_actions", - "thread_state":"new_thread", - "call_to_actions":[ - { - "payload":"OPTIMIST_GET_STARTED" - } - ] - }), - headers={'Content-type': 'application/json'}) +def set_get_started_button(token): + r = requests.post("https://graph.facebook.com/v2.8/me/thread_settings", + params={"access_token": token}, + data=json.dumps({ + "setting_type":"call_to_actions", + "thread_state":"new_thread", + "call_to_actions":[ + { + "payload":"OPTIMIST_GET_STARTED" + } + ] + }), + headers={'Content-type': 'application/json'}) print r.content if r.status_code != requests.codes.ok: print r.text - -# set_menu() -# set_get_started_button() \ No newline at end of file diff --git a/facebookbot.py b/facebookbot.py index 756729d..e63a6ce 100644 --- a/facebookbot.py +++ b/facebookbot.py @@ -170,7 +170,7 @@ def handle_messages(): elif postback_payload == 'OPTIMIST_GET_STARTED': if not Mongo.user_exists(users, sender_id): g.user = Mongo.get_user_mongo(users, sender_id) - return handle_first_time_user(users, g.user) + return handle_first_time_user(g.user) # Handle messages elif webhook_type == 'message': @@ -219,7 +219,7 @@ def processIncoming(user_id, message, just_text=False): # First time user if not Mongo.user_exists(users, user_id): g.user = Mongo.get_user_mongo(users, user_id) - return handle_first_time_user(users, g.user) + return handle_first_time_user(g.user) else: g.user = Mongo.get_user_mongo(users, user_id) @@ -643,7 +643,7 @@ def handle_quick_reply(user_id, context, cmd): return handle_find_food(user_id, context, None, None, message, None, 'receive_location_gps', 1) -def handle_first_time_user(users, user): +def handle_first_time_user(user): user_id = user['user_id'] token = app.config['PAT'] @@ -669,4 +669,4 @@ def dev_area(message_text): if len(sys.argv) == 2: app.run(port=int(sys.argv[1])) else: - app.run() \ No newline at end of file + app.run()