Merge branch 'feature/usergrouptag' into dev

This commit is contained in:
2022-01-03 23:26:40 +03:00
5 changed files with 143 additions and 0 deletions
+5
View File
@@ -1,4 +1,5 @@
using BotFramework;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.AspNetCore.Builder;
@@ -7,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core
{
@@ -36,6 +38,9 @@ namespace Kruzya.TelegramBot.Core
services.AddDbContext<CoreContext>(ctx =>
ctx.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
// Cache for users.
services.AddSingleton<ICacheStorage<long, User>, MemoryCache<long, User>>();
// Trigger module handlers.
_core.Modules.ConfigureServices(services);
}
+6
View File
@@ -17,6 +17,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Destiny2.WhereIsXur", "modu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatManagement", "modules\ChatManagement\ChatManagement.csproj", "{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserGroupTag", "modules\UserGroupTag\UserGroupTag.csproj", "{5B89E22A-78CF-4037-BE0D-5F88EF47022B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entertainment", "modules\Entertainment\Entertainment.csproj", "{1D62101B-C2B1-4E79-8F7A-690E44E3EE81}"
EndProject
@@ -54,6 +55,10 @@ Global
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.Build.0 = Release|Any CPU
{5B89E22A-78CF-4037-BE0D-5F88EF47022B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B89E22A-78CF-4037-BE0D-5F88EF47022B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B89E22A-78CF-4037-BE0D-5F88EF47022B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B89E22A-78CF-4037-BE0D-5F88EF47022B}.Release|Any CPU.Build.0 = Release|Any CPU
{1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -65,6 +70,7 @@ Global
{B82E3339-3B34-4600-8C59-C5A3640B1982} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{AE782132-BED8-4D1E-98FA-CB768171D332} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{5B89E22A-78CF-4037-BE0D-5F88EF47022B} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
{1D62101B-C2B1-4E79-8F7A-690E44E3EE81} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
EndGlobalSection
EndGlobal
+107
View File
@@ -0,0 +1,107 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace UserGroupTag
{
public class Handler : BotEventHandler
{
private readonly CoreContext _db;
private readonly ICacheStorage<long, User> _userCache;
public Handler(CoreContext db, ICacheStorage<long, User> userCache)
{
_db = db;
_userCache = userCache;
}
[ParametrizedCommand(InChat.Public, "add_group", CommandParseMode.Both)]
public async Task HandleAddGroup(string groupName)
{
// var groupName = group.Text;
var repliedMessage = RawUpdate.Message!.ReplyToMessage;
if (repliedMessage == null || repliedMessage.From!.IsBot || groupName.IsNullOrEmpty())
{
return;
}
var from = repliedMessage.From;
var fromId = from.Id;
var chatId = Chat!.Id;
var userGroupOption = await _db.UserValues.FindOrCreateOption(Chat.Id, "userGroups");
var tagDictionary = userGroupOption.GetValue(new Dictionary<string, List<long>>());
var memberList = tagDictionary.GetValueOrDefault(groupName, new List<long>());
if (memberList.Contains(fromId))
{
await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} уже находится в группе {groupName}",
ParseMode.Html);
return;
}
memberList.Add(repliedMessage.From.Id);
tagDictionary[groupName] = memberList;
userGroupOption.SetValue(tagDictionary);
_db.MarkAsModified(userGroupOption);
_userCache.SetValue(repliedMessage.From.Id, repliedMessage.From, Int32.MaxValue);
await Bot.SendTextMessageAsync(chatId, $"{from.ToHtml()} добавлен в группу {groupName}",
ParseMode.Html);
}
[ParametrizedCommand(InChat.Public, "tag_group", CommandParseMode.Both)]
public async Task HandleTagGroup(string groupName)
{
if (string.IsNullOrEmpty(groupName))
{
return;
}
var userGroupOption = await _db.UserValues.FindOption(Chat.Id, "userGroups");
var userList = userGroupOption?.GetValue<Dictionary<string, List<long>>>()?[groupName];
if (userList == null || userList.Count == 0)
{
return;
}
var msg = new HtmlString()
.Text("Пользователь ")
.UserMention(From)
.Text($" призывает группу пользователей {groupName}: ");
foreach (var userId in userList)
{
msg.UserMention(await GetUser(userId, Chat)).Text(", ");
}
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString().TrimEnd(',', ' '), ParseMode.Html);
}
protected async Task<User> GetUser(long userId, Chat chat)
{
User user;
if (!_userCache.TryGetValue(userId, out user))
{
user = (await Bot.GetChatMemberAsync(Chat, userId)).User;
_userCache.SetValue(userId, user, Int32.MaxValue);
}
return user;
}
}
}
+10
View File
@@ -0,0 +1,10 @@
using System;
using Kruzya.TelegramBot.Core;
namespace UserGroupTag
{
public class UserGroupTag : Module
{
public UserGroupTag(Core core) : base(core) { }
}
}
+15
View File
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Delete Files="$(OutDir)\TelegramBot.dll" />
</Target>
</Project>