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 received_authentication(event): '''' Authorization Event The value for 'optin.ref' is defined in the entry point. For the "Send to Messenger" plugin, it is the 'data-ref' field. Read more at https://developers.facebook.com/docs/messenger-platform/webhook-reference/authentication ''' sender_id = event["sender"]["id"] recipient_id = event["recipient"]["id"] time_of_auth = event["timestamp"] # The 'ref' field is set in the 'Send to Messenger' plugin, in the 'data-ref' # The developer can set this to an arbitrary value to associate the # authentication callback with the 'Send to Messenger' click event. This is # way to do account linking when the user clicks the 'Send to Messenger' # lugin. pass_through_param = event["optin"]["ref"] log("Received authentication for user {sender} and page {page} with pass through param {param} at {time}".format( sender=sender_id, page=recipient_id, param=pass_through_param, time=time_of_auth )) # When an authentication is received, we'll send a message back to the sender # to let them know it was successful. send_text_message(sender_id, "Authentication successful")
def send_typing_off(recipient_id): ''' Turn typing indicator off ''' log("Turning typing indicator off") recipient = messages.Recipient(recipient_id=recipient_id) request = messages.SenderActionRequest(recipient, action=SenderActionRequest.SO_TYPE_OFF) messenger.send(request)
def send_read_receipt(recipient_id): ''' Send a read receipt to indicate the message has been read ''' log("Sending a read receipt to mark message as seen") recipient = messages.Recipient(recipient_id=recipient_id) request = messages.SenderActionRequest(recipient, action=SenderActionRequest.SO_MARK_SEEN) 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 webhook(): data = request.get_json() log( data ) # you may not want to log every incoming message in production, but it's good for testing handler.webhook_handler(data) # // Assume all went well. # // # // You must send back a 200, within 20 seconds, to let us know you've # // successfully received the callback. Otherwise, the request will time out. return "ok", 200
def receive_postback(event): sender_id = event["sender"]["id"] recipient_id = event["recipient"]["id"] time_of_postback = event["timestamp"] time_converted = convert_timestame(time_of_postback) payload = event["postback"]["payload"] log("Received postback for user {sender} and page {recipient} with payload {payload} at {time}".format( sender=sender_id, recipient=recipient_id, payload=payload, time=time_of_postback )) send_text_message(sender_id, "Postback called")
def receive_account_link(event): ''' * Account Link Event * * This event is called when the Link Account or UnLink Account action has been * tapped. * https://developers.facebook.com/docs/messenger-platform/webhook-reference/account-linking ''' sender_id = event["sender"]["id"] recipient_id = event["recipient"]["id"] status = event["account_linking"]["status"] auth_code = event["account_linking"]["authorization_code"] log("Received account link event with for user {sender} with status {status} and auth code {code} ".format( sender=senderID, status=status, code=authCode))
def receive_message_read(event): ''' * Message Read Event * * This event is called when a previously-sent message has been read. * https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-read ''' sender_id = event["sender"]["id"] recipient_id = event["recipient"]["id"] # All messages before watermark (a timestamp) or sequence have been seen. watermark = event["read"]["watermark"] sequence_number = event["read"]["seq"] log("Received message read event for watermark {watermark} and sequence number {number}".format( watermark=watermark, number=sequence_number))
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_delivery_confirmation(event): ''' * Delivery Confirmation Event * * This event is sent to confirm the delivery of a message. Read more about * these fields at https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-delivered ''' sender_id = event["sender"]["id"] recipient_id = event["recipient"]["id"] delivery = event["delivery"] message_ids = delivery.get("mids") watermark = delivery["watermark"] sequence_number = delivery["seq"] if message_ids: for message_id in message_ids: log("Received delivery confirmation for message ID: {message}".format(message=message_id)) log("All message before {watermark} were delivered.".format(watermark=watermark))
def webhook(): data = request.get_json() log(data) # you may not want to log every incoming message in production, but it's good for testing # if data["object"] == "page": # for entry in data["entry"]: # page_id = entry["id"] # time_of_event = entry["time"] # for messaging_event in entry["messaging"]: # if messaging_event.get("optin"): # optin confirmation # received_authentication(messaging_event) # elif messaging_event.get("message"): # someone sent us a message # receive_message(messaging_event) # elif messaging_event.get("delivery"): # delivery confirmation # receive_delivery_confirmation(messaging_event) # elif messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message # receive_postback(messaging_event) # elif messaging_event.get("read"): # receive_message_read(messaging_event) # elif messaging_event.get("account_linking"): # receive_account_link(messaging_event) # else: # log("Unknown event received: {event} ".format(event=messaging_event)) messenger_handler.webhook_handler(data) # // Assume all went well. # // # // You must send back a 200, within 20 seconds, to let us know you've # // successfully received the callback. Otherwise, the request will time out. return "ok", 200
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"] 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") 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) elif message_attachments: send_text_message(sender_id, "Message with attachment received")