sendAudio method

Future<Message> sendAudio(
  1. dynamic chatId,
  2. dynamic audio, {
  3. int? messageThreadId,
  4. String? caption,
  5. String? parseMode,
  6. List<MessageEntity>? captionEntities,
  7. int? duration,
  8. String? performer,
  9. String? title,
  10. dynamic thumbnail,
  11. bool? disableNotification,
  12. bool? protectContent,
  13. int? replyToMessageId,
  14. bool? allowSendingWithoutReply,
  15. ReplyMarkup? replyMarkup,
})

Use this method to send audio files

If you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.

For sending voice messages, use the sendVoice method instead.

https://core.telegram.org/bots/api#sendaudio

Implementation

Future<Message> sendAudio(dynamic chatId, dynamic audio,
    {int? messageThreadId,
    String? caption,
    String? parseMode,
    List<MessageEntity>? captionEntities,
    int? duration,
    String? performer,
    String? title,
    dynamic thumbnail,
    bool? disableNotification,
    bool? protectContent,
    int? replyToMessageId,
    bool? allowSendingWithoutReply,
    ReplyMarkup? replyMarkup}) async {
  if (chatId is! String && chatId is! int) {
    return Future.error(TelegramException(
        'Attribute \'chatId\' can only be either type of String or int'));
  }
  var requestUrl = _apiUri('sendAudio');
  var body = <String, dynamic>{
    'chat_id': chatId,
    'message_thread_id': messageThreadId,
    'caption': caption,
    'parse_mode': parseMode,
    'caption_entities':
        captionEntities == null ? null : jsonEncode(captionEntities),
    'duration': duration,
    'performer': performer,
    'title': title,
    'disable_notification': disableNotification,
    'protect_content': protectContent,
    'reply_to_message_id': replyToMessageId,
    'allow_sending_without_reply': allowSendingWithoutReply,
    'reply_markup': replyMarkup == null ? null : jsonEncode(replyMarkup),
  };

  var multiPartFiles = <MultipartFile>[];

  if (audio is io.File) {
    multiPartFiles.add(HttpClient.toMultiPartFile(audio, 'audio'));
  } else if (audio is String) {
    body.addAll({'audio': audio});
  } else {
    return Future.error(TelegramException(
        'Attribute \'audio\' can only be either io.File or String (Telegram fileId or image url)'));
  }

  if (thumbnail != null) {
    if (thumbnail is io.File) {
      multiPartFiles.add(HttpClient.toMultiPartFile(thumbnail, 'thumbnail'));
    } else if (thumbnail is String) {
      body.addAll({'thumbnail': thumbnail});
    } else {
      return Future.error(TelegramException(
          'Attribute \'thumbnail\' can only be either io.File or String (Telegram fileId or image url)'));
    }
  }

  return multiPartFiles.isEmpty
      ? Message.fromJson(await HttpClient.httpPost(requestUrl, body: body))
      : Message.fromJson(await HttpClient.httpMultipartPost(
          requestUrl, multiPartFiles,
          body: body));
}