【问题标题】:Hi anyone? Go here a little bit嗨有人吗?到这里一点
【发布时间】:2021-08-05 19:21:22
【问题描述】:

我的电报机器人使用此代码时遇到问题

<?php

    if ($_SERVER['HTTPS'] != "on") {
        $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        header("Location: $url");
        exit;
    }

    $botToken = "XXXX...";
    $website = "https://api.telegram.org/bot".$botToken;

    $content = file_get_contents("php://input");
    $update = json_decode($content, true);
    $chatId = $update["message"]["chat"]["id"];
    $message = $update["message"]["text"];
    // get message_id
    $messageId = $update["message"]["message_id"];
    switch($message) {
        case("action"):
            sendMessage($chatId, "What should I do?");
            break;

        case("add"):
            // add the 2nd parameter
            editMessageText($chatId, $messageId, "should I add?");
            break;

        default:
            sendMessage($chatId, "default");
    }

    function sendMessage($chatId, $message) {
        $url = $GLOBALS[website]."/sendMessage?    chat_id=".$chatId."&text=".urlencode($message)."&reply_markup".$reply1;
        file_get_contents($url);
    }

    function editMessageText($chatId, $messageId, $message) {
        $url = $GLOBALS[website]."/editMessageText?chat_id=".$chatId."&message_id=".$messageId."&text=".urlencode($message);
        file_get_contents($url);
    }

    function file_get_contents_curl($url) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
?>

我试图以此为例,如果我使用它,它会继续加载,一分钟后机器人会用我和他按下的相同数量的按钮向我发送垃圾邮件

我已经使用了上面的代码,但它没有编辑它以防它给我发送相同的消息

【问题讨论】:

  • 请确保你的标题概括了你的问题,它不应该像聊天频道上的开场消息一样读起来。
  • 欢迎来到 SO。请阅读How do I ask a good question?

标签: bots telegram


【解决方案1】:

您需要在默认值中添加中断

default:
        sendMessage($chatId, "default");
        break;
}

我捕获了这个,我认为它也出现了一些错误,您没有为变量 $reply1 分配任何值,并且在 &reply_markup 之后也缺少 = 标记

function sendMessage($chatId, $message) {
    $url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message)."&reply_markup".$reply1;
    file_get_contents($url);
}

如果你需要运行测试使用这个

function sendMessage($chatId, $message) {
    $url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."&text=".urlencode($message);
    file_get_contents($url);
}

If you need any help

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2021-02-21
    • 2016-07-18
    • 2011-01-25
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    相关资源
    最近更新 更多