send method
Future<NotificationResult>
send(
- NotificationPayload payload, {
- required Map<
String, String> environment, - bool dryRun = false,
override
Dispatches payload to the destination. Network calls MUST be skipped if dryRun is true.
Implementation
@override
Future<NotificationResult> send(
NotificationPayload payload, {
required Map<String, String> environment,
bool dryRun = false,
}) async {
final stopwatch = Stopwatch()..start();
final url = environment['DISCORD_WEBHOOK_URL'];
if (url == null || url.isEmpty) {
return NotificationResult.failure(
name, 'Discord webhook URL is not configured.');
}
int color = 3447003; // Blue info
if (payload.severity == NotificationSeverity.success) {
color = 3066993; // Green
} else if (payload.severity == NotificationSeverity.error) {
color = 15158332; // Red
}
final discordPayload = <String, dynamic>{
'embeds': [
{
'title': payload.title,
'description': '```\n${payload.message}\n```',
'color': color,
'timestamp': payload.timestamp.toIso8601String(),
}
],
};
final response = await _client.postJson(
url,
jsonPayload: discordPayload,
dryRun: dryRun,
);
stopwatch.stop();
if (response.isSuccess) {
return NotificationResult.success(
name,
statusCode: response.statusCode,
durationMs: stopwatch.elapsedMilliseconds,
);
} else {
return NotificationResult.failure(
name,
'Discord notification failed with status code ${response.statusCode}.',
statusCode: response.statusCode,
durationMs: stopwatch.elapsedMilliseconds,
);
}
}