Messenger API
list of methods for sending messages to messenger
Note
All returns of these functions are a POST Requests <Response>
send_message
This method allows you to send a text message to the given recipient, Note that the number of characters to send is limited to 2000 characters
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages#sending_text
Args:
dest_id (str): user id facebook for the destination
message (str): message want to send
Example:
chat.send_message(sender_id, "Hello world")
send_action
This method is used to simulate an action on messages. example: view, writing.
Action available: [‘mark_seen’, ‘typing_on’, ‘typing_off’]
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages/sender-actions
Args:
dest_id (str): user id facebook for the destination
action (str): action [‘mark_seen’, ‘typing_on’, ‘typing_off’]
Example:
chat.send_action(sender_id, "mark_seen")
send_quick_reply
Quick replies provide a way to present a set of up to 13 buttons in-conversation that contain a title and optional image, and appear prominently above the composer.
You can also use quick replies to request a person’s location, email address, and phone number.
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
Args:
dest_id (str): user id facebook for the destoination
quick_rep(list of Button): list of the different quick_reply to send a user
text (str): A text of a little description for each <quick_reply>
Example:
from ampalibe.ui import QuickReply
...
quick_rep = [
QuickReply(
title="Angela",
payload="/membre",
image_url="https://i.imgflip.com/6b45bi.jpg"
),
QuickReply(
title="Rivo",
payload="/membre",
image_url="https://i.imgflip.com/6b45bi.jpg"
),
]
# next=True in parameter for displaying directly next list quick_reply
chat.send_quick_reply(sender_id, quick_rep, 'who do you choose ?')
from ampalibe.ui import QuickReply
...
quick_rep = [
QuickReply(
title=f"response {i+1}",
payload= Payload("/response", item=i+1)
image_url="https://i.imgflip.com/6b45bi.jpg"
)
for i in range(30)
]
# put a value in `next` parameter to show directly next options with the specified word.
chat.send_quick_reply(sender_id, quick_rep, 'who do you choose ?', next='See More')
send_template
The method send_template represent a Message templates who offer a way for you to offer a richer in-conversation experience than standard text messages by integrating buttons, images, lists, and more alongside text a single message. Templates can be use for many purposes, such as displaying product information, asking the messagerecipient to choose from a pre-determined set of options, and showing search results.
For this, messenger only validates 10 templates for the first display, so we put the parameter <next> to manage these numbers if it is a number of elements more than 10. So, there is a quick_reply which acts as a “next page” displaying all requested templates
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages/template/generic
Args:
dest_id (str): user id facebook for the destination
elements(list of Element): the list of the specific elements to define the structure for the template
quick_rep(list of QuickReply): addition quick reply at the bottom of the template
next(bool): this params activate the next page when elements have a length more than 10
Example:
from ampalibe import Payload
from ampalibe.ui import Element, Button
...
list_items = []
for i in range(30):
buttons = [
Button(
type="postback",
title="Get item",
payload=Payload("/item", id_item=i+1),
)
]
list_items.append(
Element(
title="iTem",
image_url="https://i.imgflip.com/6b45bi.jpg",
buttons=buttons,
)
)
# next=True for displaying directly next page button.
chat.send_template(sender_id, list_items, next=True)
# next=<word> for displaying directly next page button with custom text.
# chat.send_template(sender_id, list_items, next='Next page')
send_file_url
The Messenger Platform allows you to attach assets to messages, including audio, video, images, and files.All this is the role of this Method. The maximum attachment size is 25 MB.
Args:
dest_id (str): user id facebook for destination
url (str): the origin url for the file
filetype (str, optional): type of showing file[“video”,”image”,”audio”,”file”]. Defaults to ‘file’.
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages#url
Example:
chat.send_file_url(sender_id, 'https://i.imgflip.com/6b45bi.jpg', filetype='image')
send_file
This method send an attachment from file
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages#file
Args:
dest_id (str): user id facebook for the destination
file (str): name of the file in local folder
filetype (str, optional): type of the file[“video”,”image”,…]. Defaults to “file”.
filename (str, optional): A filename received for de destination . Defaults to name of file in local.
Example:
chat.send_file(sender_id, "mydocument.pdf")
chat.send_file(sender_id, "intro.mp4", filetype='video')
chat.send_file(sender_id, "myvoice.m4a", filetype='audio')
send_media
Method that sends files media as image and video via facebook link. This model does not allow any external URLs, only those on Facebook.
Ref: https://developers.facebook.com/docs/messenger-platform/send-messages/template/media
Args:
dest_id (str): user id facebook for the destination
fb_url (str): url of the media to send on facebook
media_type (str): the type of the media who to want send, available[“image”,”video”]
Example:
chat.send_media(sender_id, "https://www.facebook.com/iTeam.Community/videos/476926027465187", 'video')
get_started
Method that GET STARTED button when the user talk first to the bot.
Args:
dest_id (str): user id facebook for the destination
payload (str): payload of get started, default: ‘/’
Example:
chat.get_started()