Files
telegram-bot/Core/Handler/GeneralHandler.cs
T

76 lines
2.0 KiB
C#
Raw Normal View History

2020-03-08 01:55:50 +04:00
using System;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
2023-07-20 21:22:35 +03:00
namespace Kruzya.TelegramBot.Core.Handler
2020-03-08 01:55:50 +04:00
{
public class GeneralHandler : BotEventHandler
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class HandleEverythingAttribute : HandlerAttribute
{
protected override bool CanHandle(HandlerParams param)
{
return true;
}
}
protected readonly CoreContext databaseContext;
2023-07-20 21:22:35 +03:00
2020-03-08 01:55:50 +04:00
public GeneralHandler(CoreContext dbCtx)
{
databaseContext = dbCtx;
}
[HandleEverything]
2022-01-23 02:08:31 +03:00
[Priority(short.MaxValue - 10)]
2021-12-25 21:44:39 +04:00
public async Task<bool> Listener()
2020-03-08 01:55:50 +04:00
{
2023-07-20 21:22:35 +03:00
foreach (var message in new Message[] { RawUpdate.Message, RawUpdate.EditedMessage, RawUpdate.ChannelPost, RawUpdate.EditedChannelPost })
2020-03-08 01:55:50 +04:00
{
if (message == null)
{
continue;
}
2021-12-25 20:08:40 +04:00
await VerifyChatPair(message);
2020-03-08 01:55:50 +04:00
}
2021-12-25 21:44:39 +04:00
return true;
2020-03-08 01:55:50 +04:00
}
public async Task VerifyChatPair(Message message)
{
await VerifyChatPair(message.Chat, message.From);
}
public async Task VerifyChatPair(Chat chat, User user)
{
await VerifyChatPair(chat.Id, user.Id);
}
2021-12-22 01:27:56 +04:00
public async Task VerifyChatPair(long chat, long user)
2020-03-08 01:55:50 +04:00
{
var hasEntry = await databaseContext.Users.AnyAsync(e => e.ChatId == chat && e.UserId == user);
if (hasEntry)
{
return;
}
await MakeChatPair(chat, user);
}
2021-12-22 01:27:56 +04:00
public async Task MakeChatPair(long chat, long user)
2020-03-08 01:55:50 +04:00
{
await databaseContext.Users.AddAsync(new BotUser()
{
ChatId = chat,
2022-01-25 23:34:37 +03:00
UserId = user
2020-03-08 01:55:50 +04:00
});
}
}
}