简体   繁体   中英

How can I send notifications every minute to the telegram bot with PHP?

The scenario is as follows, I want to send messages or notifications (without commands sent by users) every minute on a Telegram channel.

It happens that I can send the messages every time I enter the URL and the message is sent, but I want it to be sent automatically (without entering the URL) every minute. I do not know how to do that, thanks in advance.

<?php $botToken="<<BOT-TOKEN>>" ; $website="https://api.telegram.org/bot" .$botToken; $chatId="337957895" ; //**===>

NOTE: this chatId MUST be the chat_id of a person, NOT another bot chatId !!!** $params=[ 'chat_id'=>$chatId, 'text'=>'This is my message !!!', ]; $ch = curl_init($website . '/sendMessage'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, ($params)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); ?>

I have to enter the URL still for the message to be sent ... https://bot.unpixelmas.com/bot.php

If we say that the URL you want to enter is <YOUR CUSTOM URL> , then you can make a PHP file and use the following code:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => '<YOUR CUSTOM URL>',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
));

$response = curl_exec($curl);

curl_close($curl);

?>

Then using crontab -e command you can open cronjob file and add your PHP file as record:

* * * * *  <PHP bin path> <your PHP source file path>

如果 URL 始终相同,则应使用cronjob并将其设置为每分钟打开该 URL。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM