PHP WebHook Telegram Bot - SSL CloudFlare

标签 php ssl cloudflare telegram telegram-bot

在论坛上进行了多次研究后,我决定写信看看您是否可以帮助我。

出于教育目的,我想用 PHP 创建机器人 Telegram 。 我阅读了它的文档,并在 CloudFlare 上为我的域创建了一个灵活的 SSL 证书。

我通过 Telegram 创建了我的机器人并收到了 token ,并使用以下代码设置了我的 webhook

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

答案是:

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

我把这段代码放在我的 page.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);

?>

但没什么,如果我写入 Bootstrap ,我不会收到任何答复,也不会在日志文件中输入任何内容。 我该如何调试? 你有什么建议?? 提前感谢大家

最佳答案

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

要设置 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()它们。

希望对您有所帮助。

关于PHP WebHook Telegram Bot - SSL CloudFlare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33809008/

相关文章:

php - Codeigniter falshdata 中是否有最大长度?

hosting - 带有 CloudFlare 的子域

ssl - Kafka SSL 握手失败问题

amazon-web-services - x509:证书对 *<rest-of-hostname> 有效,而不是 <bucket/hostname>

ssl - 如何安装Zimbra认证?

terraform - 如何使用 terraform 导入/下载现有的 cloudflare 资源?

http-headers - 如何确保请求真的被 CloudFlare 代理了?

抽象方法中的PHP类型提示接口(interface)和方法实现中的类型提示接口(interface)的子类

php - 合并两个表并将它们的数据显示在一个数组中

php - 我是否应该坚持使用 mysql* 函数来执行简单的 mysqli_num_rows(),并使用 PDO 来执行更大的查询?