def send_quick_reply(text_str, set_num, recipient_id): #https://cdn3.iconfinder.com/data/icons/diagram_v2/PNG/32x32/diagram_v2-23.png #https://cdn3.iconfinder.com/data/icons/diagram_v2/PNG/32x32/diagram_v2-27.png #https://cdn3.iconfinder.com/data/icons/diagram_v2/PNG/32x32/diagram_v2-10.png #https://cdn1.iconfinder.com/data/icons/military-filled/64/army-09-48.png qr = [{ "content_type": "text", "title": "Search", "payload": "User Hit Search", "image_url": "https://cdn3.iconfinder.com/data/icons/diagram_v2/PNG/32x32/diagram_v2-10.png" }, { "content_type": "location", "title": "where do I send te nukes?", "image_url": "https://cdn1.iconfinder.com/data/icons/military-filled/64/army-09-48.png" }, { "content_type": "text", "title": "hit me please!!!", "payload": "QR_COF", "image_url": "https://cdn3.iconfinder.com/data/icons/diagram_v2/PNG/32x32/diagram_v2-23.png" }] data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "text": text_str, "quick_replies": qr } }) params = {"access_token": os.environ["PAGE_ACCESS_TOKEN"]} headers = {"Content-Type": "application/json"} rq = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) if rq.status_code != 200: logar(rq.status_code) logar(rq.text)
def get_send_products(category, recipient): logar("NO* REQUEST PRODUCTS cat={cate}".format(cate=category)) #ffjfjfjfjvjksdir gdjhkvfhdghvdv,tcj, kgr,ggr gxx jfjfvaonron #fkcnvnv wc_api_mfip = API(url="https://myfriendsinportugal.com", consumer_key=os.environ["WC_CONSUMER_KEY"], consumer_secret=os.environ["WC_CONSUMER_SECRET"], wp_api=True, version="wc/v2", query_string_auth=True) #query_string_auth=True // Force Basic Authentication as query string true and using under HTTPS w = wc_api_mfip.get("products?status=publish&filter[lang]=pt") #"name": "Ship Your Idea", #"permalink": "https://example.com/product/ship-your-idea-22/", # "images": [ # "src": "https://example.com/wp-content/uploads/2017/03/T_4_front-11.jpg", response_products = w.json() logar("WC_RESPONSE ? ") logar(response_products[0]["name"]) #logar(w.text) ### HTTP REQUEST for getting products? # params = { # "access_token": os.environ["PAGE_ACCESS_TOKEN"] # } # headers = { # "Content-Type": "application/json" # } # data = json.dumps({ # "recipient": { # "id": recipient_id # }, # "message":{ # "text": message_text # } # }) # wcp = requests.post("https://myfriendsinportugal.com/wp/v2/posts", params=params, headers=headers, data=data) # if wcp.status_code != 200: # logar(wcp.status_code) # logar(wcp.text) #arr_title=[response_products[0]["name"], response_products[1]["name"], response_products[2]["name"]] #arr_image=[response_products[0]["images"][0]["src"], response_products[1]["images"][0]["src"], response_products[2]["images"][0]["src"]] #arr_link=[response_products[0]["permalink"], response_products[1]["permalink"], response_products[2]["permalink"]] send_webview(arr_title, arr_image, arr_link, recipient)
def get_user_name(user_id): #"https://graph.facebook.com/v2.6/<PSID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>" params = { "access_token": os.environ["PAGE_ACCESS_TOKEN"], "fields": "first_name" } headers = {"Content-Type": "application/json"} u = requests.get( "https://graph.facebook.com/v2.6/{userPSID}".format(userPSID=user_id), params=params, headers=headers) if u.status_code != 200: logar(u.status_code) logar(u.text) user = u.json() logar("OOO - user name") logar(user["first_name"]) return user["first_name"]
def send_message(message_content, recipient_id): send_object = "there seems to be an error" if isinstance(message_content, basestring): # -------------------------------Normal Text logar("sending message to {recipient}: {text}".format( recipient=recipient_id, text=message_content)) send_object = {"text": message_content} else: # -------------------------------Web View logar("SEND_WEBVIEW") send_object = { "attachment": { "type": "template", "payload": { "template_type": "generic", "elements": message_content } } } # -------------------------------Sending data = json.dumps({ "recipient": { "id": recipient_id }, "message": send_object }) params = {"access_token": os.environ["PAGE_ACCESS_TOKEN"]} headers = {"Content-Type": "application/json"} wv = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) if wv.status_code != 200: logar(wv.status_code) logar(wv.text)
def pass_thread_control(chatter_id): passData = json.dumps({ "recipient": { "id": chatter_id }, "target_app_id": 263902037430900, "metadata": "-X pass_thread_control X-" }) passParams = { "access_token": os.environ["PAGE_ACCESS_TOKEN"], } passHeaders = {"Content-Type": "application/json"} hj = requests.post( "https://graph.facebook.com/v2.6/me/pass_thread_control", params=passParams, headers=passHeaders, data=passData) if hj.status_code != 200: logar(hj.status_code) logar(hj.text) else: logar("HANDOVER")
def webhook(): # endpoint for processing incoming messaging events data = request.get_json() logar("_DATA_") logar( data ) # you may not want to log every incoming message in production, but it's good for testing # VTOR # if data["object"] == "page": # for entry in data["entry"]: # for messaging_event in entry["messaging"]: # if messaging_event["message"].get("is_echo"): # logar("WE GOT ECHO") # #strdata = str(data) #strdata= json.dumps(data) #if "standby" in strdata: #logar("WE HAVE STANBY") # #time.sleep(500) # original if data["object"] == "page": for entry in data["entry"]: if entry.get("standby"): for standby_event in entry[ "standby"]: # a message was sent in standby logar("STANBY EVENT") elif entry.get("messaging"): for messaging_event in entry["messaging"]: sender_id_pass = messaging_event["sender"]["id"] if messaging_event.get( "message"): # someone sent us a message sender_id = messaging_event["sender"][ "id"] # the facebook ID of the person sending you the message recipient_id = messaging_event["recipient"][ "id"] # the recipient's ID, which should be your page's facebook ID if messaging_event["message"].get("attachments"): logar("Bot got a sticker!") else: message_text = messaging_event["message"][ "text"] # the message's text if message_text == "produtos": get_send_products(0, sender_id) elif message_text == "qr": send_quick_reply("Reply to this", 0, sender_id) elif message_text == "name": nome = get_user_name(sender_id) send_message( "You are {usna}".format(usna=nome), sender_id) else: send_message("BOT :D", sender_id) pass if messaging_event.get("request_thread_control" ): # ADMIN requested control logar("ADMIN REQUEST CONTROL sender_id={sendr}".format( sendr=sender_id_pass)) pass_thread_control(sender_id_pass) if messaging_event.get( "request_thread_control"): # ADMIN control passed logar("PASS CONTROL TO ADMIN") if messaging_event.get( "delivery"): # delivery confirmation pass if messaging_event.get("optin"): # optin confirmation pass if messaging_event.get( "postback" ): # user clicked/tapped "postback" button in earlier message pass else: logar("DEVDO - No Standby, neither Messaging") else: logar("DEVDO - no PAGE") return "ok", 200
def get_send_products(category, recipient): max_nop = 3 #---------------------------------------------------------------------------------WOOCOMMERCE PyPa logar("REQUEST PRODUCTS *WC_PyPa* cat={cate}".format(cate=category)) wc_api_mfip = API(url="https://myfriendsinportugal.com", consumer_key=os.environ["WC_CONSUMER_KEY"], consumer_secret=os.environ["WC_CONSUMER_SECRET"], wp_api=True, version="wc/v2", query_string_auth=True) #query_string_auth=True // Force Basic Authentication as query string true and using under HTTPS w = wc_api_mfip.get("products?status=publish&per_page=5") response_products = w.json() logar("WC_RESPONSE ? ") logar(response_products[0]["name"]) #logar(type(w)) #logar(w.text) #arr_title=[, response_products[1]["name"], response_products[2]["name"]] #arr_image=[, response_products[1]["images"][0]["src"], response_products[2]["images"][0]["src"]] #arr_link= [response_products[0]["permalink"], , response_products[2]["permalink"]] element = {} arr_elements = [] for i in range(0, max_nop): element = { "title": response_products[i]["name"], "image_url": response_products[i]["images"][0]["src"], "subtitle": "", "buttons": [{ "type": "web_url", "url": response_products[i]["permalink"], "title": "Read More" }] } logar("element{num}".format(num=i)) logar(element) arr_elements.append(element) logar("OOO - arr_elements") logar(arr_elements) send_message(arr_elements, recipient)