简体   繁体   English

从电报机器人获取完整消息

[英]get Full message from telegram bot

I want to get the full message sent by the person's ID in telegram bot, including all the attached files, such as a photo, audio, image, or a caption and photo... , and send it to another person's ID.我想在电报机器人中获取此人的 ID 发送的完整消息,包括所有附件,例如照片、音频、图像或标题和照片...,并将其发送到另一个人的 ID。 I don't want it to be forward, I want to be sent: I receive all updates this way:我不希望它被转发,我想被发送:我以这种方式接收所有更新:

$data=json_decode(file_get_contents("php://input"));

my code:我的代码:

    <?php
const apiKey='112';
$channels=[
    '1'=>'-1001233909561',
    '2'=>'-1001198102700',
];
const admin='668400001';
//-------------------------------------------------------- End Channels and ADMIN Info
$data=json_decode(file_get_contents("php://input"));
$json_data=json_encode($data);
file_put_contents('data.json', $json_data);
function bot($method, $datas = [])
{
    $url = "https://api.telegram.org/bot" . apiKey . "/" . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datas));
    $res = curl_exec($ch);
    if (curl_error($ch))
    {
        var_dump(curl_error($ch));
    }
    else
    {
        return json_decode($res);
    }
}

 function forwardMessage($messageId)
{

    bot('forwardMessage',[
        'chat_id'=>backupChannelId,
        'from_chat_id'=>firstChannelId,
        'message_id'=>$messageId,
    ]);
}

function sendMessage($toChannel,$message)
{

}



?>

If I understand correctly, you want to get the text and all the attachments of all messages sent to the bot.如果我理解正确,您希望获取发送到机器人的所有消息的文本和所有附件。 For example, the text of the message is in update -> message -> text .例如,消息的文本在update -> message -> text中。

$text = $data['message']['text'];
$audio = $data['message']['audio'];

The easiest way to send the exact same massage to another chat is by forwarding it, otherwise you have to search for all the possible attachments in the message object and, if present, send them to the other chat with the corrisponding method ( sendPhoto , sendAudio etc.).将完全相同的消息发送到另一个聊天的最简单方法是转发它,否则您必须在消息object 中搜索所有可能的附件,如果存在,使用相应的方法( sendPhotosendAudio将它们发送到另一个聊天ETC。)。


Tip:小费:

use利用

$data = json_decode(file_get_contents("php://input"), true);

instead of代替

$data = json_decode(file_get_contents("php://input"));

More details here更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM