Step 2 — Building…
Yey! We are ready to build it!
1. Go to the BotFather and get your Bot token and shhh… it`s a secret token.
2. As I said, we can work with Telegram API only by HTTS interface, so that`s why we need cUrl in our application. You can test it, by making this simple request from browser, just paste into url input: https://api.telegram.org/bot<SecretBotToken>/getMe
3. Now we need to create some connection service from here, we always will use cUrl connection, example:
define('BASIC_API_URL', 'https://api.telegram.org/bot<BOT_TOKEN>/');public function make(string $action): array
{
$curlInit = curl_init();
curl_setopt($curlInit, CURLOPT_URL, BASIC_API_URL . $action);
$curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($curlInit); return json_decode($output, true);
}
I used this CurlConnectionService which implements a ConnectionService interface.
4. For every action you will need a secret token, it will be used in url, like /getMe example. So imagine, that we have already created some method with сURL, which take a url for request. Then simple action will be like this:
$aboutMe = (new ConnectionService())->make('getMe');
And yes, result in $aboutMe will be in json format:
{
"ok": true,
"result": {
"id": <bot_id>,
"is_bot": true,
"first_name": "bot_name",
"username": "bot_username"
}
}
Okay, but what’s next? Now we need set a webhook, you need hosting with HTTPS URL. On that URL, you need the unique route or filename (.php) — this route or file will work as CORE handler for our bot.
This path to webhook must be secret (in security reasons), you can name it as your bot token, no one knows it, so it`s safe. Information about webhook setting: https://core.telegram.org/bots/api#setwebhook
You can simply setup webhook from the browser search input.
https://api.telegram.org/bot<SecretBotToken>/setWebhook?url=<webhookPageUrl>
In my case, I prepared helper for this thing: WebhookConfigurationHelper
5. From now, we can catch updates from bot, without reloading pages. Let’s prepare handler for this task:
// getting all data from incoming POST requestfile_get_contents("php://input");/*
* handle update as Associative array
* (or you can use object for this task)
*/$update = json_decode($update, true);
All params, which ‘Update’ request contains, you can see in official docs: https://core.telegram.org/bots/api#update
We want to react on user commands. So we need to catch message data with sub params ‘chat’ and ‘text’, and exactly ‘text’ sub param we need.
switch ($update['text']) {
case '/start':
//some action
exit;
}
okay, now we react on user commad ‘/start’, let`s send him a message:
/*
* if user send /start command
* we answer hi!
*/$connectionService->make(
'sendMessage?chat_id=' . $update['chat']['id'] . '&text=hi!'
);
After that you can test it on your bot. Just send him a message ‘/start’.
6. Now, we can catch commands as we want. Next — we need to create data saving service, feel free to do it on your own.
More information, what information you can get from available types read here: https://core.telegram.org/bots/api#available-types