MessengerBot
Last time, we made an echo bot that only replies back whatever you sent it.
This time, we will mess around with the Messenger API and further explore its capabilities.
This tutorial will include multiple resources that can aid you with your "exploration"
I'm trying out a new approach for this tutorial. There won't be step-by-step instructions, but instead there will be several "challenges" throughout this tutorial accompanied by different resources I used to learn when I was learning how to do this myself!
This might involve a lot of "Googling" and exploring on your own, but don't worry, feel free to message/Discord me if you have any questions at all
We'll be using the PyMessager Python wrapper for the FB Messenger API to make it easier on us!
It might be useful for us to look at the message.py file, where all of the functions we will call are located! This can help us understand how to use the function and what to pass in as parameters.
For example, when making the echo bot, we used the send_text() function (can be found in message.py) to send a text message.
Behind the scene, this is what it looks like!
All we had to do to use this is to call the function and pass in appropriate parameters. Similarly, let's explore the other functions inside this library to make our messenger bot do cooler things
https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api
Challenge 1: Send an image of your choice as a response when your bot is messaged.
Feel free to reuse your Echo bot so you don't have to create another one from scratch. Just delete or comment out the part where we code our bot to echo and replace it with other function calls
send_buttons() takes in a list of buttons, each of which is an ActionButton object
https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#url
url_buttons = [
ActionButton(
ButtonType.WEB_URL,
<TITLE>,
url=<URL>
)
]
Challenge 2: Send a URL button as a response when your bot is messaged.
Similar to the url button, postback buttons use the same send_buttons() method.
https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#postback
postback_buttons = [
ActionButton(
ButtonType.POSTBACK,
<TITLE>,
payload=<PAYLOAD_STRING>
)
]
Postback is useful to invoke certain actions in your bot depending on user's input.
When a postback button is pressed, the 'payload' that you specified will be sent back to your Flask app.
Challenge 3: When your bot is messaged, present the user with 2 postback buttons. Observe what happens when you press on one of the button (print and check your logs).
How could we respond to the user depending on what they say/choose?
Challenge 4: Try sending a certain response depending on the user's:
Working example: https://glitch.com/edit/#!/yummy-season-rose?path=server.py
Now that you have the basic tools, let's go wild and make our own bot!
A ton of examples here: https://manychat.com/blog/chatbot-examples/