sendRichMessage method

Future<bool> sendRichMessage({
  1. required String title,
  2. required String message,
  3. String color = 'good',
  4. Map<String, String>? fields,
  5. String? emoji,
})

Send a rich message with attachments/blocks

Implementation

Future<bool> sendRichMessage({
  required String title,
  required String message,
  String color = 'good', // good, warning, danger, or hex color
  Map<String, String>? fields,
  String? emoji,
}) async {
  final attachment = <String, dynamic>{
    'color': color,
    'title': title,
    'text': message,
    'ts': DateTime.now().millisecondsSinceEpoch ~/ 1000,
  };

  if (fields != null && fields.isNotEmpty) {
    attachment['fields'] = fields.entries
        .map((e) => {
              'title': e.key,
              'value': e.value,
              'short': true,
            })
        .toList();
  }

  final payload = {
    'channel': channel,
    'text': title,
    'attachments': [attachment],
    if (emoji != null) 'icon_emoji': emoji,
  };
  return _postMessage(payload, label: 'rich message');
}