Create Facebook Messaenger Bot
Tags: php, facebook, bot
Published: 2023-05-24 10:36:03

We want to create a bot to answer users for us.
Imagine there are many people in facebook that they want something from you, they have so many question or they want buy something from you, they need help and you should answer their question, or help for to complete their order.
But you can't answer all question . in this time we need a Responsive bot for answering users question.
Let's go to see how we can create a messaenger bot in facebook.

![Image description](https://www.insegment.com/blog/wp-content/uploads/2020/11/chatbot-marketing.gif)

In First step we need a Facebook app, so we must visit to [facebook developer site](https://developers.facebook.com/) after login you can find My app In header in this page you should create your first app like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7l5n23axilwa2rc7126o.png)

after select a name for app like this:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5m8nbngssrsk2197tx62.png)

You should be redirected to the dashboard that looks like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7z59nmvds2zooju7q9v3.png)

 ok now i must mention that if you just need to send a message to any user you need a set up "messenger" in this dashboard, but if you want create a conversation to user and get message process on it and send a response you need set up a "webhook" too. 
 i know, a bot that can conversation with user is more attractive so we need two setup,
if you do not have a page you must crate page firstly

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/loehdcjbg7sc38rgy4us.png)

and then generate token for this page.
this token is a authorization for use facebook api and send message to users.

**Send a Basic Text**

 To send a basic text message to a person who sent your Page a message, send a POST request to the /PAGE-ID/messages endpoint.

sample request to send a message be like this:

```
curl -X POST -H "Content-Type: application/json" -d '{
  "recipient":{
    "id":"PSID"
  },
  "messaging_type": "RESPONSE",
  "message":{
    "text":"Hello, world!"
  }
}' "https://graph.facebook.com/LATEST-API-VERSION/PAGE-ID/messages?access_token=PAGE-ACCESS-TOKEN"
```
 now we don't have 'PSID' where can we find this Id, you probably know, yes from webhook that we must setup.

 if you want submit a webhook for the first time facebook send a post request to your webhook with some parameter, and and you should return one of those parameters that call "hub_challenge".
 and done your webhook is set.
ok now in messenger setting we have a webhook configuration and for get message from user we should edit that like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/por1nftmytfqhc8zr9xo.png)

and set messages

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zo51eeb1w0z5xivovl6z.png)

now send a message from another account to this page in facebook you get a json body like this:

```
{
  "object": "page",
  "entry": [
    {
      "time": 1682502617664,
      "id": "0",
      "messaging": [
        {
          "sender": {
            "id": "12334"
          },
          "recipient": {
            "id": "23245"
          },
          "timestamp": "1527459824",
          "message": {
            "mid": "test_message_id",
            "text": "Hello world"
          }
        }
      ]
    }
  ]
}
```
and this send id:

```
"sender": {
            "id": "12334"
          },
```
be that PSID you looking for it.