def send_button_message(recipient_id): ''' Send a button message using the Send API. ''' recipient = messages.Recipient(recipient_id=recipient_id) web_button = elements.WebUrlButton( title='Open Web URL', url='https://www.oculus.com/en-us/rift/' ) postback_button = elements.PostbackButton( title='Trigger Postback', payload='DEVELOPER_DEFINED_PAYLOAD' ) phone_number_button = elements.PhoneNumberButton( title='Call Phone Number', payload='+16505551234' ) template = templates.ButtonTemplate( text='This is test text', buttons=[ web_button, postback_button, phone_number_button ] ) attachment = attachments.TemplateAttachment(template=template) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_quick_reply(recipient_id): ''' Send a message with Quick Reply buttons. ''' recipient = messages.Recipient(recipient_id=recipient_id) postback1_reply = elements.TextQuickReply( title='Action', payload='DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_ACTION' ) postback2_reply = elements.TextQuickReply( title='Comedy', payload='DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_COMEDY' ) postback3_reply = elements.TextQuickReply( title='Drama', payload='DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_DRAMA' ) quick_replies = templates.QuickReplyTemplate( quick_replies=[ postback1_reply, postback2_reply, postback3_reply ] ) message = messages.Message(text="What's your favorite movie genre?", quick_replies=quick_replies) request = messages.MessageRequest(recipient, message) log("data being sent {request}".format(request=request.serialise())) messenger.send(request)
def help(event): sender_id = event["sender"][ "id"] # the facebook ID of the person sending you the message recipient = messages.Recipient(recipient_id=sender_id) message_text = "Here's your help" message = messages.Message(text=message_text) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_huehue(event): sender_id = event["sender"][ "id"] # the facebook ID of the person sending you the message recipient = messages.Recipient(recipient_id=sender_id) message_text = 'huehue' message = messages.Message(text=message_text) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_file_message(recipient_id): ''' Send a file using the Send API. ''' url = SERVER_URL + "/assets/test.txt" recipient = messages.Recipient(recipient_id=recipient_id) attachment = attachments.FileAttachment(url=url) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_video_message(recipient_id): ''' Send a video using the Send API. ''' url = SERVER_URL + "/assets/allofus480.mov" recipient = messages.Recipient(recipient_id=recipient_id) attachment = attachments.VideoAttachment(url=url) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_audio_message(recipient_id): ''' Send audio using the Send API. ''' url = SERVER_URL + "/assets/sample.mp3" recipient = messages.Recipient(recipient_id=recipient_id) attachment = attachments.AudioAttachment(url=url) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_giff_message(recipient_id): ''' Send a Gif using the Send API. ''' url = SERVER_URL + "/assets/instagram_logo.gif" recipient = messages.Recipient(recipient_id=recipient_id) attachment = attachments.ImageAttachment(url=url) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_generic_message(recipient_id): # Send a Structured Message (Generic Message type) using the Send API. recipient = messages.Recipient(recipient_id=recipient_id) rift_web_button = elements.WebUrlButton( title='Open Web URL', url='https://www.oculus.com/en-us/rift/' ) rift_postback_button = elements.PostbackButton( title='Call Postback', payload='Payload for first bubble' ) rift_element = elements.Element( title='rift', subtitle='Next-generation virtual reality', item_url='https://www.oculus.com/en-us/rift/', image_url='http://messengerdemo.parseapp.com/img/rift.png', buttons=[ rift_web_button, rift_postback_button ] ) touch_web_button = elements.WebUrlButton( title='Open Web URL', url='https://www.oculus.com/en-us/touch/' ) touch_postback_button = elements.PostbackButton( title='Call Postback', payload='Payload for second bubble' ) touch_element = elements.Element( title='touch', subtitle='Your Hands, Now in VR', item_url='https://www.oculus.com/en-us/touch/', image_url='http://messengerdemo.parseapp.com/img/touch.png', buttons=[ rift_web_button, rift_postback_button ] ) template = templates.GenericTemplate( elements=[ rift_element, touch_element ] ) attachment = attachments.TemplateAttachment(template=template) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def send_image_message(recipient_id): ''' Send an image using the Send API. ''' # url = SERVER_URL + "/assets/rift.png" url = "http://messengerdemo.parseapp.com/img/rift.png" log("url is {url}".format(url=url)) recipient = messages.Recipient(recipient_id=recipient_id) attachment = attachments.ImageAttachment(url=url) message = messages.Message(attachment=attachment) request = messages.MessageRequest(recipient, message) messenger.send(request)
def get_location(attachment, event): log('get location, with attachment: {attachment}'.format( attachment=attachment)) sender_id = event["sender"][ "id"] # the facebook ID of the person sending you the message recipient = messages.Recipient(recipient_id=sender_id) lat = attachment.get("payload").get("coordinates").get("lat") long = attachment.get("payload").get("coordinates").get("long") message_text = "Your location: lat: {lat}, and long: {long}".format( lat=lat, long=long) message = messages.Message(text=message_text) request = messages.MessageRequest(recipient, message) messenger.send(request)
def receive_message(event): ''' * Message Event * * This event is called when a message is sent to your page. The 'message' * object format can vary depending on the kind of message that was received. * Read more at https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-received * * For this example, we're going to echo any text that we get. If we get some * special keywords ('button', 'generic', 'receipt'), then we'll send back * examples of those bubbles to illustrate the special message bubbles we've * created. If we receive a message with an attachment (image, video, audio), * then we'll simply confirm that we've received the attachment. ''' # log('message event data: ', event) sender_id = event["sender"][ "id"] # the facebook ID of the person sending you the message recipient_id = event["recipient"][ "id"] # the recipient's ID, which should be your page's facebook ID time_of_message = event["timestamp"] time_converted = convert_timestame(time_of_message) message = event["message"] recipient = messages.Recipient(recipient_id=sender_id) log("Received message for user {sender_id} and page {recipient_id} at {time} with message:" .format(sender_id=sender_id, recipient_id=recipient_id, time=time_of_message)) log(message) is_echo = message.get("is_echo") message_id = message.get("mid") app_id = message.get("app_id") metadata = message.get("metadata") message_text = message.get("text") message_attachments = message.get("attachments") quick_reply = message.get("quick_reply") if is_echo: log("Received echo for message {message_id} and app {app_id} with metadata {metadata}" .format(message_id=message_id, app_id=app_id, metadata=metadata)) return elif quick_reply: quick_reply_payload = quick_reply["payload"] log("Quick reply for message {message_id} with payload {payload}". format(message_id=message_id, payload=quick_reply_payload)) # send_text_message(sender_id, "Quick reply tapped") message = messages.Message(text='Quick reply tapped') request = messages.MessageRequest(recipient, message) messenger.send(request) return # If we receive a text message, check to see if it matches a keyword # and send back the example. Otherwise, just echo the text we received. if message_text: if message_text == 'image': send_image_message(sender_id) elif message_text == 'giff': send_giff_message(sender_id) elif message_text == 'audio': send_audio_message(sender_id) elif message_text == 'video': send_video_message(sender_id) elif message_text == 'file': send_file_message(sender_id) elif message_text == 'button': send_button_message(sender_id) elif message_text == 'generic': send_generic_message(sender_id) elif message_text == 'receipt': send_receipt_message(sender_id) elif message_text == 'quick reply': send_quick_reply(sender_id) elif message_text == 'read receipt': send_read_receipt(sender_id) elif message_text == 'typing on': send_typing_on(sender_id) elif message_text == 'typing off': send_typing_off(sender_id) elif message_text == 'account linking': send_account_linking(sender_id) else: # send_text_message(sender_id, message_text) message = messages.Message(text=message_text) request = messages.MessageRequest(recipient, message) messenger.send(request) elif message_attachments: # send_text_message(sender_id, "Message with attachment received") message = messages.Message(text='Message with attachment received') request = messages.MessageRequest(recipient, message) messenger.send(request)