Php telegram sendAudio

I have a problem with the 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 does not work with an audio length of 9 or more seconds. I also tried with .mp3, but nothing. The same thing works with a sound duration of 6 or less seconds. I looked through the documentation and says that only 50 MB of files are limited. Help pls. Here is my telegram.

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

And here is 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);
}
+4
source share
3 answers

I use this code to send an mp3 audio file to a telegram from my php application and it works 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);
+2
source

Have you tried using sendVoice for an ogg file instead of sendAudio?

0
source

The following code worked well for me:

<?php
exec( "curl -i -F 'chat_id=1234567890' -F 'voice=@audio.ogg' 'https://api.telegram.org/bot1234567890:AABBCCDDEEFFGGHH/sendVoice' 2>&1", $output , $return );
print_r( json_decode( end( $output ) ) );
0
source

Source: https://habr.com/ru/post/1626123/


All Articles