sendVoice method
Future<Message>
sendVoice(
- dynamic chat_id,
- dynamic voice, {
- String? caption,
- String? parse_mode,
- List<
MessageEntity> ? caption_entities, - int? duration,
- bool? disable_notification,
- int? reply_to_message_id,
- bool? allow_sending_without_reply,
- ReplyMarkup? reply_markup,
inherited
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.
Implementation
Future<Message> sendVoice(dynamic chat_id, dynamic voice,
{String? caption,
String? parse_mode,
List<MessageEntity>? caption_entities,
int? duration,
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('sendVoice');
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,
'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 (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 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));
}