At the moment, messengers is considered one of the best messaging apps in the world and is used by a lot of people. The use of instant messengers has become a new breakthrough for companies in the field of communication with customers. And you know why? Customers are not always ready to wait for information by e-mail or by phone, they need instant service, instant response and information. And messengers is one of the most attractive communication channels: a higher opening rate and more trust.
The development of chat bots for messengers is growing rapidly. This guide for programmers will help to take the first step in creating a bot.
I will tell you how to send a messengers message in PHP using the Chat API.
At the beginning
Ensure that your local development environment has PHP installed.
Create your account in Chat-Api. It's free!
After that, you will be redirected to the admin panel, where your personal instance will be generated and created for you.
Go to your personal account and get a QR code there. Next, open messengers on your mobile phone, go to Settings -> messengers Web -> Scan a QR code.
And here is the whole script:
$data = [ 'phone' => '15034365851', // Receivers phone 'body' => 'Hello, world!', // Message ]; $json = json_encode($data); // Encode data to JSON // URL for request POST /message $token = '83763g87x'; $instanceId = '777'; $url = 'https://api.chat-api.com/instance'.$instanceId.'/message?token='.$token; // Make a POST request $options = stream_context_create(['http' => [ 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => $json ] ]); // Send a request $result = file_get_contents($url, false, $options);
Just copy and use it! Don't use brackets, hyphens and any other formatting symbols in the phone. Use only digits. You will only need to substitute your token from your personal account into the $token variable and instance number (or ApiURL)
Here in this article we analyze the code in parts. So it will be easier to understand!
What if I want to do this with another language?
Get API key, try it today!
Feel free to reach out and share your experiences or ask any questions.