Telegram 机器人 - sendMessage 文本

Telegram bot - sendMessage text

我在 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;
}

我在电报机器人中的文本看起来像:

"here+is+my+text.+this+is+new+line."

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

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

谢谢

您文本中的特殊字符是由 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;
    }

考虑使用 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"
]);