A simple PHP script to send messages to a Telegram group using a bot. π€
-
Clone the Repository:
git clone https://github.com/jaycee0610/Basic-Telegram-Sender-PHP.git cd Basic-Telegram-Sender-PHP
-
Install Dependencies:
composer require rootscratch/telegram
- Open Telegram and search for
@BotFather
. π€ - Start a chat with
@BotFather
and create a new bot. - Once created, you will receive a bot token. Save this token for later use. π
- Create a new Telegram group.
- Add your bot to the group.
- Send any message in the group to initialize it. π
To retrieve the updates for your bot, use the following API request:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Example:
https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
Look for the "chat" object in the API response:
{
"update_id": 8393,
"message": {
"message_id": 3,
"from": {
"id": 7474,
"first_name": "AAA"
},
"chat": {
"id": -<group_ID>,
"title": "<Group name>"
},
"date": 25497,
"new_chat_participant": {
"id": 71,
"first_name": "NAME",
"username": "YOUR_BOT_NAME"
}
}
}
- The
id
inside the "chat" object is your Group Chat ID and always starts with-
. π - Use this ID when sending messages to the group. π©
Note: If you created a new group and the API response only returns:
{"ok": true, "result": []}
Try removing and re-adding the bot to the group. π
-
Modify
index.php
:require_once 'vendor/autoload.php'; use Rootscratch\Telegram\Send; Send::telegram_token('your-bot-token-here'); Send::telegram_chat_id('-your-chat-id-here'); $message = 'Hello, Telegram!'; try { $send = Send::SendGroup($message); echo json_encode($send); } catch (\Exception $e) { echo json_encode(['error' => $e->getMessage()]); }
-
Run the script:
php index.php
{
"ok": true,
"result": {
"message_id": 123,
"chat": {
"id": -1001234567890,
"title": "My Telegram Group"
},
"text": "Hello, Telegram!"
}
}
- If you get
{"ok":true,"result":[]}
when retrieving updates, try removing and re-adding the bot to the group. π - Make sure the bot has permission to send messages in the group. β
This project is licensed under the MIT License. π
John Natividad β¨