【问题标题】:php telegram sendAudiophp电报发送音频
【发布时间】:2016-05-04 02:27:22
【问题描述】:

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

【问题讨论】:

    标签: php audio telegram telegram-bot


    【解决方案1】:

    使用westacks/telebot 库的示例:

    <?php
    
    use WeStacks\TeleBot\TeleBot;
    
    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'
    ]);
    

    【讨论】:

      【解决方案2】:

      您可以使用此代码发送您的音频文件

      函数发送消息($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);
      

      【讨论】:

        【解决方案3】:

        下一个代码对我很有效:

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

          • 如何将超级组中的 mp3 声音下载到我的服务器?
          【解决方案5】:

          您是否尝试过将 sendVoice 用于 ogg 文件而不是 sendAudio?

          【讨论】:

            猜你喜欢
            • 2021-10-25
            • 1970-01-01
            • 1970-01-01
            • 2021-04-19
            • 1970-01-01
            • 2018-02-08
            • 1970-01-01
            • 2017-09-01
            • 1970-01-01
            相关资源
            最近更新 更多