【问题标题】:Sending voice to Telegram through PHP app通过 PHP 应用程序向 Telegram 发送语音
【发布时间】:2018-09-10 15:58:45
【问题描述】:

情况如下。

我有一个 Android 应用程序,它可以在我指定的任何地方使用 HTTP POST 发送语音文件。我也想将这些语音文件自动下载到 Telegram bot。我写了一个 PHP 机器人,我可以接受并翻译到 Telegram 中的任何字段。我想念的一件事是文件本身。

我不知道如何对电报 API “说”我下载了一个文件。

那么我有什么:在$_FILE 流中显示文件(我可以读取它的名称),我将它作为$voice 扔到sendVoice 函数中,我也知道我必须使用简单的multipart/form-data POST发送文件的方法。

我的问题是:是否可以使用 CURL 以外的东西直接从 PHP 发布?如果没有,那我错过了什么?我使用的其他方法是通过file_get_contents()发送消息和照片,不知道它是否仍然适合语音文件?

这是我现在拥有的代码,我在某个地方找到的(我有 0 个 cURL 知识):

function sendVoice($chat_id, $voice, $caption) {
    $boundary = uniqid();
    $delimiter = '-------------' . $boundary;
    $fields = array(
        "chat_id" => $chat_id,
        "caption" => $caption
    );

    $post_data = build_data_files($boundary, $fields, $voice);

    $ch = curl_init($GLOBALS['api'].'/sendVoice');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        //"Authorization: Bearer $TOKEN",
        "Content-Type: multipart/form-data; boundary=" . $delimiter,
        "Content-Length: " . strlen($post_data))
        );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_exec($ch);
    curl_close($ch);
}


function build_data_files($boundary, $fields, $files){
    $data = '';
    $eol = "\r\n";
    $delimiter = '-------------' . $boundary;
    foreach ($fields as $name => $content) {
        $data .= "--" . $delimiter . $eol
            . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
            . $content . $eol;
    }
    foreach ($files as $name => $content) {
        $data .= "--" . $delimiter . $eol
            . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
            //. 'Content-Type: image/png'.$eol
            . 'Content-Transfer-Encoding: binary'.$eol
            ;
        $data .= $eol;
        $data .= $content . $eol;
    }
    $data .= "--" . $delimiter . "--".$eol;
    return $data;
}

【问题讨论】:

    标签: php-telegram-bot


    【解决方案1】:

    这实际上要容易得多。由于$_FILES 中存在文件,因此我们可以在 sendVoice() 函数中完成所有 cURL,而无需重新绑定边界或指定内容类型。

    这是对我有用的最终代码:

    function sendVoice($chat_id, $voice, $caption) {
        $filepath = realpath($_FILES['file']['tmp_name']);
        $post_data = array(
            'chat_id' => $chat_id,
            'voice' => new CURLFile($filepath),
            'caption' => $caption
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $GLOBALS['api'].'/sendVoice');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_exec($ch);
        curl_close($ch); 
    }
    

    这将下载一个 .OGG 文件作为语音消息(因为我不希望任何文件出现在我的音乐播放器中)。稍后可以在$post_data 数组中添加更多字段,例如持续时间,Telegram 会相应地吃掉它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 2019-01-12
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多