In This lab, we will send a notification message to a Telegram channel, or public/private group using the Telegram bot and PHP.
This lab aims to be a part of a trigger/action that fires when a monitoring flag is turned on.
We will:
- Create our Telegram Bot.
- Add The Telegram Bot To Our Channel or The Group AS An Admin.
- Coding Out PHP Script To Send Our Message To The Target Recipients.
- Send Telegram Message With Photo, Text, And Link.
Create A Simple Telegram Bot
In the Telegram App (I use the telegram desktop app), make a search for @BotFather as shown in the below screenshot, and select the verified signed BotFather which has a green right verification sign.
When Click on the @BotFather the Telegram Bot, it will be available to start creating new or modifying and managing your previous Bots
You will need to press the Start button to start creating our lab Bot:
From The Appearing interactive menu:
1- Press on /newbot (or chat with @BotFather with /newbot and press enter.)
Then the @BotFather will reply to you: Alright, a new bot. How are we going to call it? Please choose a name for your bot.
2- Enter our lab Bot name and username (the Bot username must end in ‘bot').
I named the Bot: Linux Labs TTrigger Bot
And The Bot username: TTrigger_bot
All we need to have now is our Telegram Bot username (TTrigger_bot), and Token (5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg) as shown in the output @BotFather message.
If you missed your Bot username/or token, you can talk any time to the @BotFather and ask him for /mybots so select your Bot, and select API Token.
Now we will go to the next step, and make our Bot an Admin for the target recipient Channel or Group.
Add The Telegram Bot To Our Channel or The Group AS An Admin.
From our Channel or Group Menu, we need to select Manage Channel/Manage Group
And Select Administrators
Then the Add Administrator
Then Search and Select Our Telegram Bot TTrigger_Bot, to make it an admin to the Group/Channel Then accept and save.
Coding Out PHP Script To Send Our Message To The Target Recipients
The simplest PHP script code to send a Telegram message using our Bot can be like the following (The Telegram Bot API can be found here).
Send Telegram Message With Photo, Text, and Link
If we need to send a photo + text (as a photo caption) including an URL to the Chat_id which is our Gropu/Channel username
<?php // Our Telegram Bot Token $apiToken = "5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg"; // A variable hold an URL $nurl = 'https://domain.com/index.html'; // A variable hold our full text message $message = "This Photo message is sent using our Telegram Bot ". "\n" .$nurl ; // A variable hold the photo url $photo_url = "https://domain.com/images/photo.png"; // JSON data will pass to the Telegram API $data = [ 'chat_id' => '@GroupID', 'caption' => $message, 'photo'=>$photo_url ]; // Send $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendPhoto?" . http_build_query($data) ); ?>
And if we need only to send a text message only simply code as
<?php $apiToken = "5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg"; $nurl = 'https://domain.com/index.html'; $message = "This Text message is sent using our Telegram Bot ". "\n" .$nurl ; $data = [ 'chat_id' => '@GroupID', 'text' => $message ]; $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) ); ?>
Save your script code above as telegram.php file, and to test it you can use the command php -f /path/to/script/file/telegram.php
, and you can read more about installing and running PHP script files CLI, on windows from here: A Simple Way To Installing PHP And Run Script Files On Windows.
yes