编程语言
首页 > 编程语言> > php电报sendAudio

php电报sendAudio

作者:互联网

我在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的文件受到限制.请帮忙.
这是我的电报.

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应用程序的电报,它对我来说工作正常.

    $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,audio,telegram,telegram-bot
来源: https://codeday.me/bug/20190702/1355956.html