[英]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.场景如下,我想在 Telegram 频道上每分钟发送一次消息或通知(没有用户发送的命令)。
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.碰巧我每次输入 URL 时都可以发送消息并发送消息,但我希望它每分钟自动发送一次(不输入 URL)。 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 !!!', ];
注意:这个chatId 必须是一个人的chat_id,而不是另一个bot chatId !!!** $params=[ 'chat_id'=>$chatId, 'text'=>'This is my message !!!', ]; $ch = curl_init($website . '/sendMessage');
$ch = curl_init($website .'/sendMessage'); curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch);
$result = curl_exec($ch); curl_close($ch);
curl_close($ch); ?>
?>
I have to enter the URL still for the message to be sent ... https://bot.unpixelmas.com/bot.php我必须仍然输入 URL 才能发送消息... 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:如果我们说您要输入的网址是
<YOUR CUSTOM URL>
,那么您可以制作一个 PHP 文件并使用以下代码:
<?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:然后使用
crontab -e
命令您可以打开cronjob文件并将您的 PHP 文件添加为记录:
* * * * * <PHP bin path> <your PHP source file path>
如果 URL 始终相同,则应使用cronjob并将其设置为每分钟打开该 URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.