Libraries
- open_in_new php-telegram-bot/core .
- open_in_new irazasyed/telegram-bot-sdk .
- open_in_new TelegramBot/Api
(
TelegramBot\Api\Client, formerly known as steadfast/telegram-bot-api).
Catching every update
With TelegramBot\Api\Client you can subscribe to all updates with on():
use TelegramBot\Api\Client;
$bot = new Client($bot_api_token);
$bot->on(function ($update) {
// use $update object here
});
Updates
When we click the “Start” button:
{
"update_id": 321311329,
"message": {
"message_id": 1,
"from": {
"id": 109075721,
"is_bot": false,
"first_name": "Andrew",
"last_name": "Dorokhov",
"username": "andrewdorokhov",
"language_code": "en",
"is_premium": true
},
"chat": {
"id": 109075721,
"first_name": "Andrew",
"last_name": "Dorokhov",
"username": "andrewdorokhov",
"type": "private"
},
"date": 1700477697,
"text": "/start",
"entities": [
{
"offset": 0,
"length": 6,
"type": "bot_command"
}
]
}
}
When we send a message:
{
"update_id": 321311330,
"message": {
"message_id": 2,
"from": {
"id": 109075721,
"is_bot": false,
"first_name": "Andrew",
"last_name": "Dorokhov",
"username": "andrewdorokhov",
"language_code": "en",
"is_premium": true
},
"chat": {
"id": 109075721,
"first_name": "Andrew",
"last_name": "Dorokhov",
"username": "andrewdorokhov",
"type": "private"
},
"date": 1700477798,
"text": "Hi! How are you?"
}
}
Adding a bot to a group
When a bot is added to a group, Telegram sends an update like this:
{
"update_id":464807479,
"message":{
"message_id":2649,
"from":{
"id":109075721,
"is_bot":false,
"first_name":"Andrew",
"last_name":"Dorokhov",
"username":"dorohoff",
"language_code":"en-US"
},
"chat":{
"id":-3944457,
"title":"Family",
"type":"group",
"all_members_are_administrators":true
},
"date":1536089448,
"new_chat_participant":{
"id":633727860,
"is_bot":true,
"first_name":"WFProject",
"username":"WFProjectBot"
},
"new_chat_member":{
"id":633727860,
"is_bot":true,
"first_name":"WFProject",
"username":"WFProjectBot"
},
"new_chat_members":[
{
"id":633727860,
"is_bot":true,
"first_name":"WFProject",
"username":"WFProjectBot"
}
]
}
}
Notes
new_chat_participant is deprecated. It was replaced by new_chat_member, which is also deprecated.
Use new_chat_members (an array). See the open_in_new Bot API changelog
.
Removing a bot from a group
When a bot is removed from a group:
{
"update_id":464807480,
"message":{
"message_id":2650,
"from":{
"id":109075721,
"is_bot":false,
"first_name":"Andrew",
"last_name":"Dorokhov",
"username":"dorohoff",
"language_code":"en-US"
},
"chat":{
"id":-3944457,
"title":"Family",
"type":"group",
"all_members_are_administrators":true
},
"date":1536091316,
"left_chat_participant":{
"id":633727860,
"is_bot":true,
"first_name":"WFProject",
"username":"WFProjectBot"
},
"left_chat_member":{
"id":633727860,
"is_bot":true,
"first_name":"WFProject",
"username":"WFProjectBot"
}
}
}
Notes
left_chat_participant is deprecated. Use left_chat_member instead.
Details: open_in_new Bot API changelog .
Editing a previously sent message
A successful sendMessage (and similar methods) returns the sent message, including message_id. Use that ID when you edit or delete the message later.
API methods
| Method | Description |
|---|---|
editMessageText |
Edit the text and/or inline keyboard of a message. |
editMessageCaption |
Edit a media caption. |
editMessageMedia |
Edit media content. |
editMessageReplyMarkup |
Edit only the inline keyboard. |
deleteMessage |
Delete a message. |
Example with TelegramBot\Api\Client:
$bot = new Client('...');
$bot->editMessageReplyMarkup($chat_id, $message_id, $keyboard);
Andrew Dorokhov