草庐IT

php Telegram sendAudio

coder 2024-04-18 原文

我在 php telegram bot 中遇到 sendAudio() 函数的问题。

if (strtoupper($text) == "MUSIC") {
$voice = curl_file_create('audio.ogg');
$content = array('chat_id' => $chat_id, 'audio' => $voice);
$telegram->sendAudio($content);
}

这不适用于 9 秒或更长时间的音频。我也尝试过 .mp3 但没有。音频长度为 6 秒或更短的相同功能也可以使用。我查看了文档,它说只有 50MB 的文件受到限制。请帮助。 这是我的 $telegram。

include("Telegram.php");
$bot_id = "xxxxxxx:yyyyyyyy_mytoken";
$telegram = new Telegram($bot_id);

这里是 Telegram.php:

class Telegram {
private $bot_id = "mytoken";
private $data = array();
private $updates = array();
public function __construct($bot_id) {
    $this->bot_id = $bot_id;
    $this->data = $this->getData();
}
public function endpoint($api, array $content, $post = true) {
    $url = 'https://api.telegram.org/bot' . $this->bot_id . '/' . $api;
    if ($post)
        $reply = $this->sendAPIRequest($url, $content);
    else
        $reply = $this->sendAPIRequest($url, array(), false);
    return json_decode($reply, true);
}
public function sendAudio(array $content) {
    return $this->endpoint("sendAudio", $content);
}

最佳答案

我正在使用此代码将 mp3 音频文件从我的 php 应用程序发送到 Telegram ,它对我来说工作正常。

    $BOT_TOKEN = 'yourBotToken';
    $chat_id = '@yourChannel';
    $filePath = 'your/path/file';
    define('BOTAPI', 'https://api.telegram.org/bot' . $BOT_TOKEN . '/');
    $cfile = new CURLFile(realpath($filePath));
    $data = [
        'chat_id' => $chat_id,
        'audio' => $cfile,
        'caption' => $message
    ];
    $ch = curl_init(BOTAPI . 'sendAudio');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_exec($ch);
    curl_close($ch);

关于php Telegram sendAudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032242/

有关php Telegram sendAudio的更多相关文章

随机推荐