使用PHP无法自动接收电报CHATBOT功能的消息 [英] Message not receiving automatically for Telegram CHATBOT funciton using PHP

查看:97
本文介绍了使用PHP无法自动接收电报CHATBOT功能的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用PHP集成Telegram Chatbot,我已经遵循以下步骤.

In order to integrate Telegram Chatbot using PHP, I have already followed below steps.

  • 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屋!

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