当前位置: 首页 > 知识库问答 >
问题:

电报机器人-发送消息文本

邵奇
2023-03-14

我有两个问题与编程电报机器人的PHP。

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

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

“这是我的短信。这是新线。”

你能帮我做这个吗?

谢谢你

共有2个答案

姬朗
2023-03-14

考虑使用Westacks/远程机器人库。这段代码做同样的事情,但代码更少,更清晰

php prettyprint-override"><?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"
]);
西门淮晨
2023-03-14

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

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

此外,还有一些语法错误,例如额外的后面的$混乱=$混乱。"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;
    }
 类似资料:
  • 我不知道如何使用电报机器人诱导(触发)本地服务器向通道发送消息。 例如,从用户注册并连接电报的网站向用户发送通知。我们假设用户已经开始与bot对话,并准备好接收来自bot的消息。 我有一个单独的服务器,可以使用bot向服务器发送请求,但我不明白如何在这个电报机器人服务器上接收和处理请求。 我正在使用go电报-机器人-api库,此时服务器使用长轮询方法而不是webhooks。所以它通过更新通道接收一

  • 我用php编写了一个电报机器人。我想向所有成员发送一条短信,我保存了所有成员的chat\u id。我尝试使用此功能发送消息: 并使用此功能获取用户聊天ID并发送消息: 但这并不能正常工作,消息会多次发送给成员,并且在我清除数据库之前不会停止。

  • 我正在nodejs中构建一个可以在webhook上运行的电报聊天机器人。目前,bot在聊天中的每条消息都会点击我的webhook URL。对于机器人来说,可以在命令执行时只推送有效负载吗? 因此,我只想在用户执行 /test命令时从聊天中获取有效负载,聊天中的任何其他消息都不应git到我的URL。 #编辑 当前隐私设置 “启用” - 机器人将仅接收以“/”符号开头或按用户名提及机器人的消息。“禁用

  • 这不是重复的:| 我添加了一个用于管理goup的新机器人。通过此信息: 我的机器人是管理员 我的机器人隐私被禁用 “启用”-您的机器人只会接收以“/”符号开头或通过用户名提及机器人的消息。“禁用”-您的机器人将接收人们发送到组的所有消息。当前状态是:禁用成功!新状态是:禁用 bot可以读取除其他bot消息外的所有成员!但可以在回复中看到信息。 我的tg api是:https://github.co

  • 我想让用户按下电报机器人键盘上的按钮,但不发送消息。该消息将出现在消息框中,供用户进行进一步编辑,但不会发送出去。目的是生成模板消息,供用户进行进一步编辑。 这里有一个简单的键盘代码示例。 https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py 我愿意使用任何

  • 创建电报机器人并获得机器人令牌后,我想向机器人应用编程接口发送请求。 这个链接说我们必须像这样发送HTTP请求: