sendMessage method

Future<List<MessageModel>> sendMessage({
  1. List<int>? peerIds,
  2. String? message,
  3. int? randomId,
  4. String? domain,
  5. int? guid,
  6. int? lat,
  7. int? long,
  8. List? attachment,
  9. int? replyTo,
  10. List<int>? forwardMessages,
  11. Map<String, dynamic>? forward,
  12. int? stickerId,
  13. VkDartKeyboard? keyboard,
  14. Map<String, dynamic>? template,
  15. String? payload,
  16. Map<String, dynamic>? contentSource,
  17. bool? dontParseLinks,
  18. bool? disableMentions,
  19. String? intent,
  20. int? subscribeId,
})

Sends a message to the dialog from which the event came. If you want to send to multiple dialogs, specify the peerIds argument.

One of the message or attachment arguments must be specified, otherwise an ArgumentError error will be thrown.

If the randomId argument is not specified, the function itself will send this parameter to the request (any number in the range 2 ^ 31).

Sample example:

vkdart
  .onMessage()
  .where((event) => event.isInbox)
  .listen((event) => event.sendMessage(message: 'Hello world!'));

Implementation

Future<List<MessageModel>> sendMessage(
    {List<int>? peerIds,
    String? message,
    int? randomId,
    String? domain,
    int? guid,
    int? lat,
    int? long,
    List<dynamic>? attachment,
    int? replyTo,
    List<int>? forwardMessages,
    Map<String, dynamic>? forward,
    int? stickerId,
    VkDartKeyboard? keyboard,
    Map<String, dynamic>? template,
    String? payload,
    Map<String, dynamic>? contentSource,
    bool? dontParseLinks,
    bool? disableMentions,
    String? intent,
    int? subscribeId}) {
  if (message == null && attachment == null) {
    throw ArgumentError('Message or attachment must be not null');
  }

  final body = {
    'peer_ids': peerIds?.join(',') ?? peerId,
    'message': message,
    'random_id': randomId ?? Random().nextInt(2 ^ 31),
    'domain': domain,
    'guid': guid,
    'lat': lat,
    'long': long,
    'attachment': attachment?.join(','),
    'reply_to': replyTo,
    'forward_messages': forwardMessages?.join(','),
    'forward': forward,
    'sticker_id': stickerId,
    'keyboard': keyboard?.toJson(),
    'template': template,
    'payload': payload,
    'content_source': contentSource,
    'dont_parse_links':
        dontParseLinks != null ? (dontParseLinks ? 1 : 0) : null,
    'disable_mentions':
        disableMentions != null ? (disableMentions ? 1 : 0) : null,
    'intent': intent,
    'subscribe_id': subscribeId
  }..removeWhere((key, value) => value == null);

  return _vkontakte
      .request('messages.send', body)
      .then((value) => (value as List).map((element) {
            final payload = {
              'client_info': clientInfo,
              ...(element as Map).cast<String, dynamic>()
            };
            return MessageModel(payload);
          }).toList());
}