PHP WebHook Telegram Bot - SSL CloudFlare


PHP WebHook Telegram Bot - SSL CloudFlare

在论坛上进行了几次研究后,我决定写信看看你是否能帮助我。

我想用PHP创建一个用于教育目的的bot电报。我阅读了它的文档,在CloudFlare上为我的域创建了一个灵活的SSL证书。

我通过电报创建了我的机器人,并收到了令牌,并用以下代码设置了我的网络挂钩

https://api.telegram.org/bot <my token>? url = https: //miodominio.eu/page.php

答案是:

{"Ok": true, "result": false, "description": "Webhook was set"}

我把这个代码放在我的页面上。php

<?php 
function checkJSON($chatID,$update){
  $myFile = "log.txt";
  $updateArray = print_r($update,TRUE);
  $fh = fopen($myFile, 'a') or die("can't open file");
  fwrite($fh, $chatID ."'n'n");
  fwrite($fh, $updateArray."'n'n");
  fclose($fh);
}
function sendMessage()
{
  $message = "I am a baby bot.";
  return $message;
}
define('BOT_TOKEN', '< mio token >');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];

// compose reply
$reply =  sendMessage();
// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
checkJSON($chatID,$update);
?>

但是什么都没有,如果我写引导,我不会收到任何答案,日志文件也没有输入任何内容。如何调试?你有什么建议吗??感谢所有提前

我不确定这是否能解决您的问题,但我发现您的代码有两个问题。

要设置webhook,您的PHP字符串应该是:

'https://api.telegram.org/bot<token>/setWebhook?url='.urlencode('https://miodominio.eu/page.php');

要发送消息,您的PHP字符串应该是:

API_URL."sendmessage?chat_id=".$chatID."&text=".urlencode($reply)

对于任何非平凡的参数,在将它们添加到查询字符串之前,应该始终urlencode()它们。

希望能有所帮助。

使用此代码并替换API:

$content=file_get_contents("php://input");
$content = json_decode($content,TRUE);

 $params = array( 
 'chat_id'   => $content["message"]["chat"]["id"],  
 'action'     => 'typing'   
 );
$api="123";
$url = 'https://api.telegram.org/bot'.$api."/sendChatAction";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
   curl_setopt($ch, CURLOPT_URL,$url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   enter code herecurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
   $result=curl_exec ($ch);