sendRichMessage method
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 {
try {
_debugLog('Sending rich message to channel: $channel');
final url = Uri.parse('https://slack.com/api/chat.postMessage');
final attachment = {
'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,
'attachments': [attachment],
if (emoji != null) 'icon_emoji': emoji,
};
final response = await http.post(
url,
headers: {
'Authorization': 'Bearer $botToken',
'Content-Type': 'application/json',
},
body: jsonEncode(payload),
);
_debugLog('Rich message response: ${response.body}');
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final success = data['ok'] as bool? ?? false;
_channelId ??= data['channel'] as String?;
if (!success) {
print('❌ Failed to send rich Slack message: ${data['error']}');
}
return success;
}
return false;
} catch (e) {
_debugLog('❌ Exception sending rich message: $e');
print('❌ Failed to send rich Slack message: $e');
return false;
}
}