sendPhoto method

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

Use this method to send photos

On success, the sent Message is returned.

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

Implementation

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