使用PHP无法自动接收电报CHATBOT功能的消息 [英] Message not receiving automatically for Telegram CHATBOT funciton using PHP
问题描述
为了使用PHP集成Telegram Chatbot,我已经遵循以下步骤.
In order to integrate Telegram Chatbot using PHP, I have already followed below steps.
- 具有SSL证书的服务器
- 具有工作条件的Webhook 网址: https://api.telegram.org/bot /setWebhook?url = https://mywebsite.com/path/to/filename.php
- 创建了ChatBot并具有有效的令牌
- Server with SSL certificate
- Webhook with working condition URL : https://api.telegram.org/bot/setWebhook?url=https://mywebsite.com/path/to/filename.php
- Created ChatBot and have a valid Token
之后,我使用下面的代码行制作了简单的PHP文件.
After that, I have made simple PHP file using following line of code.
<?php
define('BOT_TOKEN', 'CHANGE-ME');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
// This is to read incoming message and fetch chatid
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
// compose reply
$reply ="";
switch ($message) {
case "/start":
$reply = "Welcome to chatbot world. Type /help to see commands";
break;
case "/test":
$reply = "test message";
break;
case "/hi":
$reply = "Hello from ChatBot";
break;
case "/help":
$reply = "commands: /start , /test , /hi , /help ";
break;
default:
$reply = "Something wrong";
}
// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
// Create a debug log.txt to check the response/reply from Telegram in JSON format.
// You can disable it by commenting checkJSON.
checkJSON($chatID,$update);
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 ."nn");
fwrite($fh, $updateArray."nn");
fclose($fh);
}
尽管我没有正确收到消息.
推荐答案
不确定,但是您可能会遇到这种情况.
Not sure but this might be the case for you.
您只需要确保已成功运行webhook URL ,即可开始监听您的请求,然后在Telegram ChatBot中发送消息.
You just need to make sure that you have successfully run webhook URL that start listening your request before messaging in Telegram ChatBot.
URL : https://api.telegram.org/bot/setWebhook?url=https://mywebsite.com/path/to/filename.php
最后,到目前为止,我已经使用PHP成功创建了Telegram Chatbot演示.
As of now, finally, I have successfully created demo of Telegram Chatbot using PHP.
这篇关于使用PHP无法自动接收电报CHATBOT功能的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!