sendAudio method

Future<Message> sendAudio(
  1. dynamic chat_id,
  2. dynamic audio, {
  3. String? caption,
  4. String? parse_mode,
  5. List<MessageEntity>? caption_entities,
  6. int? duration,
  7. String? performer,
  8. String? title,
  9. dynamic thumb,
  10. bool? disable_notification,
  11. int? reply_to_message_id,
  12. bool? allow_sending_without_reply,
  13. ReplyMarkup? reply_markup,
})
inherited

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 chat_id, dynamic audio,
    {String? caption,
    String? parse_mode,
    List<MessageEntity>? caption_entities,
    int? duration,
    String? performer,
    String? title,
    dynamic thumb,
    bool? disable_notification,
    int? reply_to_message_id,
    bool? allow_sending_without_reply,
    ReplyMarkup? reply_markup}) async {
  if (chat_id is! String && chat_id is! int) {
    return Future.error(TelegramException(
        'Attribute \'chat_id\' can only be either type of String or int'));
  }
  var requestUrl = _apiUri('sendAudio');
  var body = <String, dynamic>{
    'chat_id': chat_id,
    'caption': caption,
    'parse_mode': parse_mode,
    'caption_entities':
        caption_entities == null ? null : jsonEncode(caption_entities),
    'duration': duration,
    'performer': performer,
    'title': title,
    'disable_notification': disable_notification,
    'reply_to_message_id': reply_to_message_id,
    'allow_sending_without_reply': allow_sending_without_reply,
    'reply_markup': reply_markup == null ? null : jsonEncode(reply_markup),
  };

  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 file_id or image url)'));
  }

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

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