---
# System prepended metadata

title: "Messenger bot: Exploring the API \U0001F50D"
tags: [MessengerBot]

---

Messenger bot: Exploring the API 🔍
===

###### tags: `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" :eyes:

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 :smile:

---

We'll be using the **PyMessager** Python wrapper for the FB Messenger API to make it easier on us!

[PyMessager Github Repo](https://github.com/eceusc/PyMessager/)

It might be useful for us to look at the [message.py](https://github.com/eceusc/PyMessager/blob/master/pymessager/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](https://github.com/eceusc/PyMessager/blob/master/pymessager/message.py#L148-L150)) to send a text message.

Behind the scene, this is what it looks like!
![](https://i.imgur.com/MEM0QQS.png)


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 :sunglasses: 


---

## Attachments

### Images

[def send_image()](https://github.com/eceusc/PyMessager/blob/master/pymessager/message.py#L152-L161)

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

---

## Buttons

### URL Button

[def send_buttons()](https://github.com/eceusc/PyMessager/blob/master/pymessager/message.py#L163-L175)

send_buttons() takes in a list of buttons, each of which is an [ActionButton](https://github.com/eceusc/PyMessager/blob/master/pymessager/message.py#L67) object

https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#url

``` python
url_buttons = [
    ActionButton(
        ButtonType.WEB_URL, 
        <TITLE>, 
        url=<URL>
    )
]
```

**Challenge 2**: Send a URL button as a response when your bot is messaged.

---

### Postback Button

Similar to the url button, postback buttons use the same send_buttons() method.

https://developers.facebook.com/docs/messenger-platform/send-messages/buttons#postback

``` python
postback_buttons = [
    ActionButton(
        ButtonType.POSTBACK, 
        <TITLE>, 
        payload=<PAYLOAD_STRING>
    )
]
```

#### Q: What is a Postback?
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).

---

## Parse Webhook Events

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:
 - Message's text
 - Postback Button Selection

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!

### Project Examples:

 - Meme Generator
     - Send a meme scraped from a website when the bot is prompted
 - Messenger Quiz
     - A fun Buzzfeed-style quiz using Messenger Bot!
 - RateMyProfessor / CAPES Bot
     - Return student reviews of a class or professor
 - Sports Bot
     - Return updates of your favorite teams' games

A ton of examples here: https://manychat.com/blog/chatbot-examples/

