简单的电报机器人,但没有响应命令(PHP)

发布于 2025-01-27 20:42:19 字数 2574 浏览 3 评论 0原文

我对所有这些都是新手的,但是我创建了一个简单的电报机器人来测试它们的功能。我已经设置了Webhook,并且正在为“主机”使用Ngrok。但是,当我键入命令时,机器人只是什么都不做。
有人知道如何解决这个问题吗?

这是index.php文件:

<?php
    require('token.php');
    require('config.php');

    try{
        $ngrokUrl = "ngrok url";
        
        $bot = new TelegramBot($token);
        $jH = new jsonHandler($token);

        var_dump($bot->setWebhook($ngrokUrl));

        $webhookJson = $jH->getWebhookJson();
        $chatId = $jH->getChatId($webhookJson);

        $msg = $jH->getText($webhookJson) !== "" ? $jH->getText($webhookJson) : "";

        switch($msg){
            default:{
                if ($msg[0] == '/')
                    $bot->sendMessage($chatId, 'The command does not exist');
                break;
            }

            case '/test':{
                $msg = 'test test';
                $bot->sendMessage($chatId, $msg);
                break;
            }

            case '/help':{
                $msg = 'Help!';
                $bot->sendMessage($chatId, $msg);
                break;
            }
        }

    }catch(ErrorException $e){
        echo $e->getMessage();
    }
?> 

和config.php文件:

<?php
    function fetchApi($url){
        $req = curl_init($url);

        $resp = curl_exec($req);

        if($resp == false){
            $error = curl_error($req);
            curl_close($req);
            throw new ErrorException($error);
        }
        else{
            curl_close($req);
            //return $resp;
        }
    }

    class TelegramBot{
        protected $tUrl;

        function __construct($token){
            $this->tUrl = "https://api.telegram.org/bot".$token;
        }

        function setUrl($method){
            return $this->tUrl."/".$method;
        }

        function sendMessage($chatId, $msg){
            $data = [
                'chat_id' => $chatId, 
                '&text' => $msg
            ];

            $url = $this->setUrl("sendMessage?".http_build_query($data));
            fetchApi($url);

            file_get_contents($url);
        }

    class jsonHandler extends TelegramBot{
        function getWebhookJson(){
            $json = file_get_contents("php://input");
            return json_decode($json, true);
        }

        function getChatId($jsonDecoded){
            return $jsonDecoded["message"]["chat"]["id"];
        }

        function getText($jsonDecoded){
            return $jsonDecoded["message"]["text"];
        }
    }
?>

I'm fairly new to all of this, but I have created a simple telegram bot to test their functionality. I have set the webhook and I'm using ngrok for the "host". However when I type a command the bot just doesn't do anything.
Does anyone know how to fix this?

Here is the index.php file:

<?php
    require('token.php');
    require('config.php');

    try{
        $ngrokUrl = "ngrok url";
        
        $bot = new TelegramBot($token);
        $jH = new jsonHandler($token);

        var_dump($bot->setWebhook($ngrokUrl));

        $webhookJson = $jH->getWebhookJson();
        $chatId = $jH->getChatId($webhookJson);

        $msg = $jH->getText($webhookJson) !== "" ? $jH->getText($webhookJson) : "";

        switch($msg){
            default:{
                if ($msg[0] == '/')
                    $bot->sendMessage($chatId, 'The command does not exist');
                break;
            }

            case '/test':{
                $msg = 'test test';
                $bot->sendMessage($chatId, $msg);
                break;
            }

            case '/help':{
                $msg = 'Help!';
                $bot->sendMessage($chatId, $msg);
                break;
            }
        }

    }catch(ErrorException $e){
        echo $e->getMessage();
    }
?> 

And the config.php file:

<?php
    function fetchApi($url){
        $req = curl_init($url);

        $resp = curl_exec($req);

        if($resp == false){
            $error = curl_error($req);
            curl_close($req);
            throw new ErrorException($error);
        }
        else{
            curl_close($req);
            //return $resp;
        }
    }

    class TelegramBot{
        protected $tUrl;

        function __construct($token){
            $this->tUrl = "https://api.telegram.org/bot".$token;
        }

        function setUrl($method){
            return $this->tUrl."/".$method;
        }

        function sendMessage($chatId, $msg){
            $data = [
                'chat_id' => $chatId, 
                '&text' => $msg
            ];

            $url = $this->setUrl("sendMessage?".http_build_query($data));
            fetchApi($url);

            file_get_contents($url);
        }

    class jsonHandler extends TelegramBot{
        function getWebhookJson(){
            $json = file_get_contents("php://input");
            return json_decode($json, true);
        }

        function getChatId($jsonDecoded){
            return $jsonDecoded["message"]["chat"]["id"];
        }

        function getText($jsonDecoded){
            return $jsonDecoded["message"]["text"];
        }
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文