【问题标题】:Telegram bot - sendMessage text电报机器人 - sendMessage 文本
【发布时间】:2020-12-11 10:43:12
【问题描述】:

我在使用 PHP 编程电报机器人时遇到 2 个问题。

  1. 问题: 请,当我尝试使用电报 API 发送更多行的文本时。通过此代码:
<?php
$update = file_get_contents('php://input');
$update = json_decode($update, true);
$chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
$mess= "here is my text.";
$mess = $mess. "\n";
$mess = $mess. " this is new line".;
send($mess, $chatId);

function send($text,$chat){
   if(strpos($text, "\n")){
        $text = urlencode($text);
    }

    $parameters = array(
        "chat_id" => $chat,
        "text" => $text,
        "parse_mode" => "Markdown"
    );

    api("sendMessage?", $parameters)
}

function api($method,$command){
$token = "******";
$api = "https://api.telegram.org/bot$token/$method";

    if(!$curld = curl_init()){
       echo $curld; 
    }
    curl_setopt($curld, CURLOPT_VERBOSE, true);
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
    curl_setopt($curld, CURLOPT_URL, $api);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curld, CURLOPT_TIMEOUT, 30);
    curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
    $apiRequest = curl_exec($curld);
    curl_close($curld);
    return $apiRequest;
}

我在电报机器人中的文字如下:

“这里+is+my+text.+this+is+new+line.”

  1. 问题,也许是问题: 当用户来到我的电报机器人时,我希望用户看到我创建的键盘按钮。现在我只有新用户,他必须点击“开始”按钮。 但我希望用户在他有时返回时看到键盘的按钮。

你能帮我解决这个问题吗?

谢谢

【问题讨论】:

    标签: php telegram telegram-bot


    【解决方案1】:

    文本中的特殊字符是由urlencode() 调用引起的。

    由于您将数据作为 POSTFIELD 传递,因此无需使用urlencode()

    另外,还有一些语法错误,比如. 后面的$mess = $mess. " this is new line".;

    更新脚本:

    <?php
        $update = file_get_contents('php://input');
        $update = json_decode($update, true);
        $chatId= $update["message"]["from"]["id"]?$update["message"]["from"]["id"]:null;
        $mess= "here is my text.";
        $mess = $mess. "\n";
        $mess = $mess. " this is new line";
    
        send($mess, $chatId);
    
        function send($text,$chat){
    
            $parameters = array(
                "chat_id" => $chat,
                "text" => $text,
                "parse_mode" => "Markdown"
            );
    
            api("sendMessage", $parameters);
        }
    
        function api($method, $command){
    
            $token = "!!";
            $api = "https://api.telegram.org/bot{$token}/{$method}";
    
            if(!$curld = curl_init()){
               echo $curld;
            }
            // curl_setopt($curld, CURLOPT_VERBOSE, true);
            curl_setopt($curld, CURLOPT_POST, true);
            curl_setopt($curld, CURLOPT_POSTFIELDS, $command);
            curl_setopt($curld, CURLOPT_URL, $api);
            curl_setopt($curld, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curld, CURLOPT_TIMEOUT, 30);
            curl_setopt($curld, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curld, CURLOPT_SSL_VERIFYHOST, FALSE);
            $apiRequest = curl_exec($curld);
            curl_close($curld);
            return $apiRequest;
        }
    

    【讨论】:

    • 啊,我没有意识到这一点。当我将代码粘贴到此处时,点是错误的。非常感谢
    • @PřemekTomášDoležal 很高兴你已经解决了!如果有人answers your question please 考虑upvotingaccepting 答案。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己赢得了一些声誉。
    【解决方案2】:

    考虑使用westacks/telebot 库。这段代码做同样的事情,但代码更少,更清晰

    <?php 
    
    require 'vendor/autoload.php';
    
    use WeStacks\TeleBot\TeleBot;
    
    // Init bot
    $bot = new TeleBot('your bot token');
    
    // Get update from incoming POST request
    $update = $bot->handleUpdate();
    
    // Send message
    $bot->sendMessage([
        'chat_id' => $update->chat()->id,
        'text' => "here is my text.\n this is new line",
        "parse_mode" => "Markdown"
    ]);
    

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 2019-10-14
      • 2017-08-18
      • 2021-04-08
      • 1970-01-01
      • 2019-05-13
      • 2018-07-22
      • 2020-10-28
      • 1970-01-01
      相关资源
      最近更新 更多