简体   繁体   English

带有 cron 和 PHP 脚本的电报机器人

[英]telegram bot with cron and PHP script

on EC2在 EC2 上

I have a crontab like that我有一个这样的 crontab

* * * * * /usr/bin/php /opt/bitnami/apache2/htdocs/bot.php

The bot.php file bot.php 文件

  1 <?php
  2 
  3 exec( 'touch /opt/bitnami/apache2/htdocs/testCron.txt');
  4 
  5   $botToken="xxxxxxxxxxxxx";
  6 
  7   $website="https://api.telegram.org/bot".$botToken;
  8   $chatId=123456;  
  9   $params=[
 10       'chat_id'=>$chatId,
 11       'text'=>'test bitnami',
 12   ];
 13   $ch = curl_init($website . '/sendMessage');
 14   curl_setopt($ch, CURLOPT_HEADER, false);
 15   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 16   curl_setopt($ch, CURLOPT_POST, 1);
 17   curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
 18   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 19   $result = curl_exec($ch);
 20
 21   if ($result === FALSE) {
 22      echo 'An error has occured: ' . curl_error($ch) . PHP_EOL;
 23   }
 24   else {
 25      echo $result;
 26   }
 27 
 28   curl_close($ch);
 29 
 30 ?>

testCron.txt is created so the cronTab works. testCron.txt 被创建,以便 cronTab 工作。

When I open bot.php with firefox the API call works and the bot sent the message on Telegram, but the PHP script with curl doesn't work with Cron.当我用 firefox 打开 bot.php 时,API 调用有效并且机器人在 Telegram 上发送了消息,但是带有 curl 的 PHP 脚本不适用于 Cron。

I put htdocs and bot.php permissions to 777 for testing and it didn't work.我将 htdocs 和 bot.php 权限设置为 777 进行测试,但它不起作用。

Any idea?任何的想法?

Hint: I'm adding it as an answer since my reputation (as of now) is below 50.提示:我将其添加为答案,因为我的声誉(截至目前)低于 50。

As of now I would need more information in order to support.截至目前,我需要更多信息才能提供支持。 There might be multiple potential issues here.这里可能存在多个潜在问题。 Let's see if we can go down the road together.看看我们能不能一起走下去。

1. Make errors visible during cron execution 1. 在 cron 执行期间使错误可见

In this case you could check your logs inside your cron.在这种情况下,您可以检查您的 cron 中的日志。 I would suggest to log any output into a file by the this to your cron >> /var/log/my-cron.log 2>&1 .我建议通过 this to your cron >> /var/log/my-cron.log 2>&1将任何输出记录到文件中。

* * * * * /usr/bin/php /opt/bitnami/apache2/htdocs/bot.php >> /opt/bitnami/apache2/htdocs/testCron.log 2>&1

From here on we can check if there is any execution errors, permission errors or some misconfiguration.从这里我们可以检查是否有任何执行错误、权限错误或一些错误配置。

2. Check your error-log configuration 2. 检查您的错误日志配置

It might be that your errors are not displayed.可能是您的错误未显示。 Here is a good starting point for some configuration这是一些配置的一个很好的起点

// tells your script to report any error
error_reporting(E_ALL); 

// tells your script to enable error logging and the location where it should log to
ini_set('log_errors', '1'); // tells your script to enable logging
ini_set('error_log', '/dev/stderr'); 

Let's see if this is enough to get an initial idea or if we need to dig deeper.让我们看看这是否足以获得初步想法,或者我们是否需要更深入地挖掘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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