@@ -215,9 +215,9 @@ await _httpClient.PostAsync(botOptions.Value.NotificationConfig.WebhookUrl,
215
215
botCtx . Invoker . OnBotOfflineEvent += async ( bot , @event ) =>
216
216
{
217
217
logger . LogError ( "Bot offline." ) ;
218
-
218
+
219
219
if ( ! botOptions . Value . NotificationConfig . NotifyWebhookOnHeartbeat ) return ;
220
-
220
+
221
221
logger . LogWarning ( "{option} set true, send HTTP POST to webhook." ,
222
222
nameof ( botOptions . Value . NotificationConfig . NotifyWebhookOnHeartbeat ) ) ;
223
223
await _httpClient . PostAsync ( botOptions . Value . NotificationConfig . WebhookUrl ,
@@ -242,17 +242,45 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
242
242
ConfigureEvents ( ) ;
243
243
await StartUp . LoginAsync ( botCtx , isoStorage , logger , botOptions . Value , stoppingToken ) ;
244
244
245
- while ( botOptions . Value . NotificationConfig . NotifyWebhookOnHeartbeat
246
- && ! stoppingToken . IsCancellationRequested )
245
+ while ( ! stoppingToken . IsCancellationRequested
246
+ && botOptions . Value . NotificationConfig . NotifyWebhookOnHeartbeat
247
+ || botOptions . Value . NotificationConfig . NotifyAdminOnHeartbeat )
247
248
{
248
- var url = botOptions . Value . NotificationConfig . WebhookUrl ;
249
- var headers = botOptions . Value . NotificationConfig . WebhookHeaders ;
250
-
251
249
await Task . Delay ( TimeSpan . FromHours ( botOptions . Value . NotificationConfig . HeartbeatIntervalHour ) , stoppingToken ) ;
252
250
253
- if ( url is not null )
251
+ if ( botOptions . Value . NotificationConfig is
252
+ { NotifyWebhookOnHeartbeat : true , WebhookUrl : not null } )
254
253
{
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
+ }
256
284
}
257
285
}
258
286
}
0 commit comments