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.
In First step we need a Facebook app, so we must visit to facebook developer site after login you can find My app In header in this page you should create your first app like this:
after select a name for app like this:
You should be redirected to the dashboard that looks like this:
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
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:
and set messages
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.
Top comments (0)