Telegram is a popular cloud-based messaging Russian app. This app is available for Android, iOS, and Windows. Using this app users can send messages, audio, videos, and files. Telegram app stored data on the cloud server and Bot is the outstanding feature of it. More important Telegram app has a feature for optional end-to-end encrypted secret chats.
How a Telegram Bot Works
Telegram Bot is an automated software application that does some tasks repeatedly. This application runs inside the telegram, Using bot API you can manage HTTP requests. You can integrate telegram with another third-party web service, send messages, accept payment.
Create a Telegram Bot Step by Step
Open your Telegram app and search for botfather, it’s a bot itself. It will let you create a telegram bot. Click on the Start option.
You can see multiple options on-screen like /newbot, /mybots, /token, /revoke etc.
Click or Type /newbot command on it and it asks for choose a bot name. Enter any relevant bot name, don’t use space in bot name.
Now you will receive a message that the bot is created successfully and the HTTP API token shows. This token is used to authorize the telegram bot API calls. Keep note it down for future use.
Test Telegram API Call
To check the API response, let execute a sample API call using the ‘getMe’ method. Just enter below Web URL in the browser. Must enter your bot token in that URL. This will show your bot account a detailed response.
https://api.telegram.org:443/bot{token}/getMe
{ "ok":true, "result":{ "id":135XXXX042, "is_bot":true, "first_name":"junzy", "username":"junzygram_bot", "can_join_groups":true, "can_read_all_group_messages":false, "supports_inline_queries":false } }
Get Messages sent to your Telegram Bot
There are two API methods to get an update from your bot.
- getUpdates
- setWebhook
getUpdates Telegram API Method
This method is used to receive incoming messages from users. It receives messages in JSON format.
<?php class Chatbot extends CI_Controller { public function __construct() { parent::__construct(); } public function get_update() { $token = '135XXXXXX2:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbUA'; $link = 'https://api.telegram.org:443/bot'.$token.''; $update = file_get_contents($link.'/getUpdates'); $response = json_decode($update, TRUE); $chat_id = $response['result'][0]['message']['chat']['id']; $message = 'Hello There'; $parameters = [ 'chat_id' => $chat_id, 'text' => $message, ]; $url = $link.'/sendMessage?'.http_build_query($parameters); file_get_contents($url); } } ?>
Whenever you execute the above code, it will post a “Hello There” message on your bot. To send a message to the user you have to run this code repeatedly, this is the manual process.
To get this task automate you can use the second-way setWebhook.
setWebhook Telegram API Method
In this method telegram automatically execute the server script. Every time the bot gets a new notification, it sends a webhook response on your set webhook URL. This is the most convenient way to create your Telegram bot.
To automate the chatbot, First set the webhook URL via the setWebhook API method. Use the below web URL to do that, must pass a valid secure web page link in the URL parameter.
https://api.telegram.org:443/bot{token}/setWebhook? url=https://infovistar.com/api-url
After executing this link in the web browser, you will get a successful response.
{"ok":true,"result":true,"description":"Webhook is already set"}
To receive the telegram bot response you need to get a webhook response in your web server Chatbot.php controller file. Check the below code example.
<?php class Chatbot extends CI_Controller { public function __construct() { parent::__construct(); } public function callback() { $token = '135XXXXXX2:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXbUA'; $link = 'https://api.telegram.org:443/bot'.$token.''; $getupdate = file_get_contents('php://input'); // for webhook $update = json_decode($getupdate, TRUE); $chatid = $update['message']['chat']['id']; $req_message = $update["message"]["text"]; // message from user $chat_id = $response['result'][0]['message']['chat']['id']; $res_message = 'Chatbot message'; $parameter = [ 'chat_id' => $chatid, 'text' => $res_message ]; $request_url = $link.'/sendMessage?'.http_build_query($parameter); file_get_contents($request_url); } } ?>
Whenever anyone writes a message on your bot then this webhook posts “Chatbot message” on your bot as a reply. This is an automated process, doesn’t need any manual action.
Using a Telegram bot API, we have successfully created a chatbot.
To see the complete Bot API description, check this page: https://core.telegram.org/bots/api