Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php telegram sendAudio

I have a problem with sendAudio() function in php telegram bot.

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

This don't work with an audio lenghtof 9 or more seconds. I also tried with .mp3 but nothing. Same function with an audio lenght of 6 or less seconds works. I looked in the documentation and it says only 50MB files are restricted. Help pls. Here's my $telegram.

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

And here 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);
}
like image 358
user3058842 Avatar asked Sep 26 '22 13:09

user3058842


1 Answers

I am using this code to send mp3 audio file to telegram from my php application and It's working fine for me.

    $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);
like image 170
amir22 Avatar answered Sep 28 '22 06:09

amir22