Telegram BOT Notification on Events

Tips submitted by PHPMaker users
Post Reply
innovativeshadow
User
Posts: 86
Location: Almaty, Kazakhstan

Telegram BOT Notification on Events

Post by innovativeshadow »

To Keep things short I will give a brief explanation on how to create a bot in telegram.

Just talk to @BotFather in telegram, follow the instructions to create a bot and get the Bot ID and API KEY fro your bot.

paste the code in:
Server Events -> Global -> All Pages -> Global Code

// Sending Notifications to Telegram Bot
function SendBotMsg($ChatID, $MesgTxt){
$parameters=['parse_mode'=>"markdown", 'chat_id'=>$ChatID, 'text'=> $MesgTxt,];
$btToken="BOTID:API-KEY"; // Here you need to enter your Bot
$website="h**ps://api.telegram.org/bot".$btToken;
return file_get_contents($website . "/sendMessage?" . http_build_query($parameters));
}

Now you have a function for sending message in bot. You need chat Id for sending messages in bot.
It can be done by searching for your bot in find contacts and send message to Bot, then with this link get chat ID : h**ps://api.telegram.org/botBotID:APIKEY/getUpdates

We are ready now, Just use this to send a message:
SendBotMsg(ChatID, "*Your Message Here *".$rsold['ID']."\n any column info ".$Usr." user Info, anything");

thats it!
Hope this helps and add a lot of power to your project, it helped my project a lot.


vuongduongquoc
User
Posts: 133

Post by vuongduongquoc »

Dear Innovativeshadow,

This is really a great post. it's truly what i'm looking for.
Before i've to combine MIRC for notification and automation with report by email.
Now we've this Telegram tool.
i've succeed to send Telegram message to my account with this link
h**ps://api.telegram.org/BOTID:API-KEY/sendMessage?parse_mode=markdown&chat_id=625475972&text=%2AYour+Message+Here+%2AVietnam%0A+any+column+info+user+Info%2C+anything
Can someone help me how to implement Php CURL auto visit this link in my code?


innovativeshadow
User
Posts: 86
Location: Almaty, Kazakhstan

Post by innovativeshadow »

Dear vuongduongquoc,

very glad to here it helped you.

with regard to Php Curl, try this:

function TgramBotSendMsg($ChatId, $ChatTxt){
$prams=['chat_id'=>$ChatId, 'text'=>$ChatTxt,];
$BToken="BOT:TOKEN";
$webSite="h***s://api.telegram.org/bot".$BToken;


$chr = curl_init($webSite . '/sendMessage');
curl_setopt($chr, CURLOPT_HEADER, false);
curl_setopt($chr, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chr, CURLOPT_POST, 1);
curl_setopt($chr, CURLOPT_POSTFIELDS, ($prams));
curl_setopt($chr, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($chr);
curl_close($chr);
}

send message with:
TgramBotSendMsg(12345678, Your Message here);

Hope it helps.


innovativeshadow
User
Posts: 86
Location: Almaty, Kazakhstan

Post by innovativeshadow »

stmontoya wrote:
> Greetings innovativehadow
>
> How to send image in the add event

Hi, Sorry for the delayed reply..

To send an file you can build function like this:


function TSbotSendFile($ChatID, $Fpath, $Captn){
if(isset($ChatID) && isset($Fpath)) {
$params=['parse_mode'=>"markdown", 'chat_id'=>$ChatID, 'document'=>new CurlFile($Fpath), 'caption'=>$Captn, ];
$BToken="BOT:TOKEN";
$webSite="h***s://api.telegram.org/bot".$BToken;
// return file_get_contents($Wsite."/sendDocument?" . http_build_query($params) );

$cur = curl_init($Wsite . '/sendDocument');
curl_setopt($cur, CURLOPT_HEADER, false);
curl_setopt($cur, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cur, CURLOPT_POST, 1);
curl_setopt($cur, CURLOPT_POSTFIELDS, ($params));
curl_setopt($cur, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($cur);
curl_close($cur);
return $result;
}}

innovativeshadow
User
Posts: 86
Location: Almaty, Kazakhstan

Post by innovativeshadow »

To send a file using above function, use:

$name = <Name of the file with extension here, usually get from database>;
$FPath = realpath('../Path/to/Folder/'.$name);

TSbotSendFile($UsrChatID, $FPath, "Caption Text here"), true);

Post Reply