具有cron和PHP脚本的电报机器人 [英] telegram bot with cron and PHP script

查看:140
本文介绍了具有cron和PHP脚本的电报机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EC2上

我有一个crontab

I have a crontab like that

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

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已创建,因此cronTab可以工作。

testCron.txt is created so the cronTab works.

当我使用firefox打开bot.php时,API调用有效,并且该机器人在Telegram上发送了消息,但是带有curl的PHP脚本不适用于Cron。

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.

我将htdocs和bot.php权限设置为777进行测试,但它不起作用。

I put htdocs and bot.php permissions to 777 for testing and it didn't work.

任何

推荐答案

提示:我将其添加为答案,因为我的声誉(截至目前)低于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。在执行cron期间使错误可见

在这种情况下,您可以检查cron内部的日志。我建议通过this将任何输出记录到文件中到您的cron > /var/log/my-cron.log 2>& 1

In this case you could check your logs inside your cron. I would suggest to log any output into a file by the 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。检查您的错误日志配置

可能未显示您的错误。这是一些配置的一个很好的起点

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.

这篇关于具有cron和PHP脚本的电报机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆