sendDocument method

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

Use this method to send general files

On success, the sent Message is returned.

Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.

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

Implementation

Future<Message> sendDocument(dynamic chatId, dynamic document,
    {int? messageThreadId,
    dynamic thumbnail,
    String? caption,
    String? parseMode,
    List<MessageEntity>? captionEntities,
    bool? disableContentTypeDetection,
    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('sendDocument');
  var body = <String, dynamic>{
    'chat_id': chatId,
    'message_thread_id': messageThreadId,
    'caption': caption,
    'parse_mode': parseMode,
    'caption_entities':
        captionEntities == null ? null : jsonEncode(captionEntities),
    'disable_content_type_detection': disableContentTypeDetection,
    '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 (document is io.File) {
    multiPartFiles.add(HttpClient.toMultiPartFile(document, 'document'));
  } else if (document is String) {
    body.addAll({'document': document});
  } else {
    return Future.error(TelegramException(
        'Attribute \'document\' 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));
}