Skip to content

Commit a90a2b6

Browse files
committed
feat: impl bot heartbeat
1 parent 093f171 commit a90a2b6

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

CameraCaptureBot.Core/Worker.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ await _httpClient.PostAsync(botOptions.Value.NotificationConfig.WebhookUrl,
215215
botCtx.Invoker.OnBotOfflineEvent += async (bot, @event) =>
216216
{
217217
logger.LogError("Bot offline.");
218-
218+
219219
if (!botOptions.Value.NotificationConfig.NotifyWebhookOnHeartbeat) return;
220-
220+
221221
logger.LogWarning("{option} set true, send HTTP POST to webhook.",
222222
nameof(botOptions.Value.NotificationConfig.NotifyWebhookOnHeartbeat));
223223
await _httpClient.PostAsync(botOptions.Value.NotificationConfig.WebhookUrl,
@@ -242,17 +242,45 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
242242
ConfigureEvents();
243243
await StartUp.LoginAsync(botCtx, isoStorage, logger, botOptions.Value, stoppingToken);
244244

245-
while (botOptions.Value.NotificationConfig.NotifyWebhookOnHeartbeat
246-
&& !stoppingToken.IsCancellationRequested)
245+
while (!stoppingToken.IsCancellationRequested
246+
&& botOptions.Value.NotificationConfig.NotifyWebhookOnHeartbeat
247+
|| botOptions.Value.NotificationConfig.NotifyAdminOnHeartbeat)
247248
{
248-
var url = botOptions.Value.NotificationConfig.WebhookUrl;
249-
var headers = botOptions.Value.NotificationConfig.WebhookHeaders;
250-
251249
await Task.Delay(TimeSpan.FromHours(botOptions.Value.NotificationConfig.HeartbeatIntervalHour), stoppingToken);
252250

253-
if (url is not null)
251+
if (botOptions.Value.NotificationConfig is
252+
{ NotifyWebhookOnHeartbeat: true, WebhookUrl: not null })
254253
{
255-
await _httpClient.PostAsync(url, new StringContent($@"Time: `{DateTime.Now:s}`, Msg: {nameof(CameraCaptureBot)} alive\."), stoppingToken);
254+
var url = botOptions.Value.NotificationConfig.WebhookUrl!;
255+
256+
var headers = botOptions.Value.NotificationConfig.WebhookHeaders;
257+
var resp = await _httpClient
258+
.PostAsync(url, new StringContent($@"Time: `{DateTime.Now:s}`, {nameof(CameraCaptureBot)} alive\."), stoppingToken);
259+
if (!resp.IsSuccessStatusCode)
260+
{
261+
logger.LogInformation("Webhook heartbeat invoked, {code} {msg}.", resp.StatusCode, resp.ReasonPhrase);
262+
}
263+
else
264+
{
265+
logger.LogWarning("Webhook heartbeat invoked failed, {code} {msg}.", resp.StatusCode, resp.ReasonPhrase);
266+
}
267+
}
268+
269+
if (botOptions.Value.NotificationConfig.NotifyAdminOnHeartbeat)
270+
{
271+
try
272+
{
273+
var message = MessageBuilder
274+
.Friend(botOptions.Value.AdminAccounts[0])
275+
.Text($"Time: {DateTime.Now:s}, {nameof(CameraCaptureBot)} alive.")
276+
.Build();
277+
await botCtx.SendMessage(message);
278+
logger.LogInformation("Bot heartbeat invoked.");
279+
}
280+
catch (Exception e)
281+
{
282+
logger.LogError(e, "Bot heartbeat invoked failed, {msg}.", e.Message);
283+
}
256284
}
257285
}
258286
}

0 commit comments

Comments
 (0)