skip to Main Content

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);
}

5

Answers


  1. Have you tried to use sendVoice for ogg file instead of sendAudio?

    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
  3. Next code did work for me well:

    <?php
    exec( "curl -i -F 'chat_id=1234567890' -F '[email protected]' 'https://api.telegram.org/bot1234567890:AABBCCDDEEFFGGHH/sendVoice' 2>&1", $output , $return );
    print_r( json_decode( end( $output ) ) );
    
    Login or Signup to reply.
  4. you can use this code to send your audio file

    function sendmessage($url, $post_params) {

    $cu = curl_init();
    curl_setopt($cu, CURLOPT_URL, $url);
    curl_setopt($cu, CURLOPT_POSTFIELDS, $post_params);
    curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);   //get result
    $result = curl_exec($cu);
    curl_close($cu);
    return $result;
    }
    $telsite = "https://api.telegram.org/bot"."$your_token_id";
    $sendAudio_url = $telsite."sendAudio";
    $post_parameters = array('chat_id' => $chat_user_id , 'audio' => $dir_of_audio);
    sendmessage($sendAudio_url , $post_parameters);
    
    Login or Signup to reply.
  5. Example using westacks/telebot library:

    <?php
    
    use WeStacksTeleBotTeleBot;
    
    require 'vendor/autoload.php';
    
    $bot = new TeleBot('123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11');
    
    $bot->sendAudio([
        'chat_id' => 1111111111,
        'audio' => 'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3'
    ]);
    
    $bot->sendAudio([
        'chat_id' => 1111111111,
        'audio' => './path/to/local/file.mp3'
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search