sendMessage method
Send a simple text message to Slack
Implementation
Future<bool> sendMessage(String message, {String? emoji}) async {
try {
_debugLog('Sending message to channel: $channel');
_debugLog('Message: $message');
final url = Uri.parse('https://slack.com/api/chat.postMessage');
final payload = {
'channel': channel,
'text': message,
if (emoji != null) 'icon_emoji': emoji,
};
_debugLog('Payload: ${jsonEncode(payload)}');
final response = await http.post(
url,
headers: {
'Authorization': 'Bearer $botToken',
'Content-Type': 'application/json',
},
body: jsonEncode(payload),
);
_debugLog('Message response status: ${response.statusCode}');
_debugLog('Message response body: ${response.body}');
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
final success = data['ok'] as bool? ?? false;
if (!success) {
final error = data['error'] as String? ?? 'Unknown error';
print('❌ Failed to send Slack message: $error');
// Provide helpful error messages
switch (error) {
case 'channel_not_found':
print('💡 Channel "$channel" not found. Make sure:');
print(' • The channel exists');
print(' • The bot is added to the channel');
print(' • Use channel ID instead of name if private');
break;
case 'not_in_channel':
print(
'💡 Bot is not in channel "$channel". Add the bot to the channel first.');
break;
case 'invalid_auth':
print(
'💡 Invalid bot token. Run "udara_cli setup" to reconfigure.');
break;
}
} else {
_debugLog('✅ Message sent successfully');
}
return success;
}
print('❌ HTTP error sending Slack message: ${response.statusCode}');
return false;
} catch (e) {
_debugLog('❌ Exception sending message: $e');
print('❌ Failed to send Slack message: $e');
return false;
}
}