sendVoice method

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

Use this method to send audio files

If you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.

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

Implementation

Future<Message> sendVoice(dynamic chatId, dynamic voice,
    {int? messageThreadId,
    String? caption,
    String? parseMode,
    List<MessageEntity>? captionEntities,
    int? duration,
    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('sendVoice');
  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,
    '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 (voice is io.File) {
    multiPartFiles.add(HttpClient.toMultiPartFile(voice, 'voice'));
  } else if (voice is String) {
    body.addAll({'voice': voice});
  } else {
    return Future.error(TelegramException(
        'Attribute \'voice\' 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));
}