in ,

How To Using Telegram Bot And PHP To Send A Message?

telegram bot php

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:

  1. Create our Telegram Bot.
  2. Add The Telegram Bot To Our Channel or The Group AS An Admin.
  3. Coding Out PHP Script To Send Our Message To The Target Recipients.
  4. 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.

Telegram Bot BotFather

When Click on the @BotFather the Telegram Bot, it will be available to start creating new or modifying and managing your previous Bots

Telegram Bot BotFather-Start

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_botTelegram Bot BotFather Create New 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.

Telegram Bot BotFather-show-bot-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

Telegram-Manage-Group-Menu

And Select Administrators

Telegram-Manage-Group-Admins

Then the Add Administrator

Telegram-Manage-Group-Add-Administrator

Then Search and Select Our Telegram Bot TTrigger_Bot, to make it an admin to the Group/Channel Then accept and save.

Telegram-Manage-Group-Add-Administrator-Accept

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).

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.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

    One Comment

    wordpress cron jobs events

    How To Setting Up WordPress Cron? The Best Way

    telegram bot python

    How to Using Telegram Bot To Send A Message With Python?