Skip to content

Commit

Permalink
send new webhook message if required
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed Jun 7, 2024
1 parent 78e414d commit 3f852ec
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoFramework.Linq;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
Expand Down Expand Up @@ -104,14 +103,28 @@ CancellationToken cancellationToken

Message sentMessage = new();

var lastMessageId = await chatSettingRepository.LastWebhookMessageBetterEdit(webhookChat.ChatId, webhookSource, webhookHeader.Event);

try
{
sentMessage = await botClient.SendTextMessageAsync(
chatId: webhookChat.ChatId,
text: webhookResponse.FormattedHtml,
messageThreadId: webhookChat.MessageThreadId,
parseMode: ParseMode.Html,
disableWebPagePreview: true, cancellationToken: cancellationToken);
if (lastMessageId != 0)
{
sentMessage = await botClient.EditMessageTextAsync(
chatId: webhookChat.ChatId,
messageId: lastMessageId,
text: webhookResponse.FormattedHtml,
parseMode: ParseMode.Html,
disableWebPagePreview: true, cancellationToken: cancellationToken);
}
else
{
sentMessage = await botClient.SendTextMessageAsync(
chatId: webhookChat.ChatId,
text: webhookResponse.FormattedHtml,
messageThreadId: webhookChat.MessageThreadId,
parseMode: ParseMode.Html,
disableWebPagePreview: true, cancellationToken: cancellationToken);
}
}
catch (Exception exception)
{
Expand All @@ -128,20 +141,24 @@ CancellationToken cancellationToken

mongoDbContextBase.WebhookHistory.Add(new WebhookHistoryEntity {
RouteId = webhookChat.RouteId,
TransactionId = $"{request.TransactionId}",
TransactionId = request.TransactionId,
CreatedDate = default,
UpdatedDate = default,
ChatId = webhookChat.ChatId,
MessageId = sentMessage.MessageId,
MessageThreadId = 0,
WebhookSource = WebhookSource.GitHub,
Elapsed = stopwatch.Elapsed,
Payload = request.IsDebug ? content : string.Empty,
Header = request.IsDebug ? webhookHeader : default,
EventName = webhookHeader.Event,
Status = (int)EventStatus.Complete
});

await mongoDbContextBase.SaveChangesAsync(cancellationToken);

mongoDbContextBase.ChatActivity.Add(new ChatActivityEntity {
ActivityType = ChatActivityType.BotSentWebHook,
ActivityType = lastMessageId == 0 ? ChatActivityType.BotSendWebHook : ChatActivityType.BotEditMessage,
ChatId = webhookChat.ChatId,
Chat = sentMessage.Chat,
User = sentMessage.From,
Expand Down
4 changes: 3 additions & 1 deletion backend/ZiziBot.Contracts/Enums/ChatActivityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public enum ChatActivityType

BotSendMessage,
BotEditMessage,
BotSentWebHook,

BotSendWebHook,
BotEditWebHook,

UserSendMessage,
UserEditMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class WebhookHistoryEntity : EntityBase
public int MessageId { get; set; }
public int MessageThreadId { get; set; }
public WebhookSource WebhookSource { get; set; }
public TimeSpan Elapsed { get; set; }
public WebhookHeader? Header { get; set; }
public string EventName { get; set; }
public string? Payload { get; set; }
public TimeSpan Elapsed { get; set; }
}
26 changes: 26 additions & 0 deletions backend/ZiziBot.DataSource/Repository/ChatSettingRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,30 @@ public async Task<bool> MeasureActivity(ChatActivityDto dto)

return chatActivityEntities.Count > ValueConst.RAID_WINDOW_LIMIT;
}

public async Task<int> LastWebhookMessageBetterEdit(long chatId, WebhookSource webhookSource, string eventName)
{
if (eventName.Like("push"))
return default;

var lastWebhookHistory = await _mongoDbContext.WebhookHistory.AsNoTracking()
.Where(x => x.ChatId == chatId)
.Where(x => x.WebhookSource == webhookSource)
.Where(x => x.EventName == eventName)
.Where(x => x.Status == (int)EventStatus.Complete)
.OrderByDescending(o => o.CreatedDate)
.FirstOrDefaultAsync();

var lastChatActivity = await _mongoDbContext.ChatActivity.AsNoTracking()
.Where(x => x.Status == (int)EventStatus.Complete)
.Where(x => x.ChatId == chatId)
.OrderByDescending(o => o.CreatedDate)
.FirstOrDefaultAsync();

if (lastChatActivity.MessageId != lastWebhookHistory.MessageId)
return default;

_logger.LogDebug("Last Webhook Message for Better Edit: {MessageId}", lastWebhookHistory.MessageId);
return lastWebhookHistory.MessageId;
}
}

0 comments on commit 3f852ec

Please sign in to comment.