From 3e83488ddd3cfa69867c36b6d786d6a9b57b697c Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Tue, 18 Jan 2022 20:34:21 +0200 Subject: [PATCH 1/4] [rules] Start work on rules command. --- Core/Service/EntityFormatter.cs | 48 +++++++++++++++++++++++++ modules/ChatManagement/Handler/Rules.cs | 48 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Core/Service/EntityFormatter.cs create mode 100644 modules/ChatManagement/Handler/Rules.cs diff --git a/Core/Service/EntityFormatter.cs b/Core/Service/EntityFormatter.cs new file mode 100644 index 0000000..91df79a --- /dev/null +++ b/Core/Service/EntityFormatter.cs @@ -0,0 +1,48 @@ +#nullable enable + +using System.Collections.Generic; +using System.Linq; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; + +namespace Kruzya.TelegramBot.Core.Service; + +internal class EntityFormatter +{ + private static readonly Dictionary HtmlMap = new() + { + { MessageEntityType.Bold, "{0}" }, + { MessageEntityType.Code, "{0}" }, + { MessageEntityType.Italic, "{0}" }, + { MessageEntityType.TextMention, "{0}" }, + { MessageEntityType.Pre, "
{0}
" }, + { MessageEntityType.Underline, "{0}" } + }; + + private static readonly Dictionary MarkdownMap = new() + { + { MessageEntityType.Bold, "*{0}*" }, + { MessageEntityType.Code, "```{0}```" }, + { MessageEntityType.Italic, "_{0}_" }, + { MessageEntityType.TextMention, "[{0}](tg://user?id={1})" }, + { MessageEntityType.Pre, "```{0}```" }, + { MessageEntityType.Underline, "__{0}__" } + }; + + public string? FormatMessage(Message msg) + { + var text = msg.Text; + var entities = msg.Entities; + + if (entities == null || text == null) return text; + + foreach (var entity in entities.Reverse()) + { + var entityText = text.Substring(entity.Offset, entity.Length); + text = text + .Remove(entity.Offset, entity.Length); + } + + return text; + } +} \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Rules.cs b/modules/ChatManagement/Handler/Rules.cs new file mode 100644 index 0000000..c5f7119 --- /dev/null +++ b/modules/ChatManagement/Handler/Rules.cs @@ -0,0 +1,48 @@ +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; +using Kruzya.TelegramBot.Core.Extensions; +using Telegram.Bot; + +namespace West.TelegramBot.ChatManagement.Handler +{ + internal partial class Rules : ManagementHandler + { + [Command(InChat.Public, "setrules", CommandParseMode.Both)] + public async Task HandleSetRules() + { + var replyToMessage = Message?.ReplyToMessage; + var text = replyToMessage?.Text; + if (replyToMessage == null || text == null) + { + return; + } + + //var entities = replyToMessage.Entities; + //if (entities != null) + //{ + // //foreach (var entity in entities) + // //{ + // // var entityText = text.Substring(entity.Offset, entity.Length); + // // text = text + // // .Remove(entity.Offset, entity.Length) + // // .Insert(entity.Offset) + // //} + //} + + + await Bot.SendTextMessageAsync(Chat, "kruzefag@gmail.com"); + } + + [Command(InChat.Public, "rules", CommandParseMode.Both)] + public async Task HandleRules() + { + var opt = Db.UserValues.FindOrCreateOption(Chat.Id, "rules"); + } + + public Rules(CoreContext db) : base(db) + { + } + } +} \ No newline at end of file From 9d2b0e5374cefdb44ca38a24b169355b258f985f Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Tue, 18 Jan 2022 20:41:02 +0200 Subject: [PATCH 2/4] [rules] Upload missing changes. --- modules/ChatManagement/Handler/Rules.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ChatManagement/Handler/Rules.cs b/modules/ChatManagement/Handler/Rules.cs index c5f7119..6a80ce6 100644 --- a/modules/ChatManagement/Handler/Rules.cs +++ b/modules/ChatManagement/Handler/Rules.cs @@ -32,7 +32,7 @@ namespace West.TelegramBot.ChatManagement.Handler //} - await Bot.SendTextMessageAsync(Chat, "kruzefag@gmail.com"); + await Bot.SendTextMessageAsync(Chat, "tut bilo milo kruzi no on protiv"); } [Command(InChat.Public, "rules", CommandParseMode.Both)] From ffbb9dfee83e9d51e9b6450053767e358c53d665 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Tue, 18 Jan 2022 21:36:47 +0200 Subject: [PATCH 3/4] [rules] Implement /rules and /setrules. DIsable build of arm images. --- .github/workflows/build-image.yml | 2 +- modules/ChatManagement/Handler/Rules.cs | 44 ++++++++++++------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml index c66de99..f7ca409 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/build-image.yml @@ -33,7 +33,7 @@ jobs: name: Build and push uses: docker/build-push-action@v2 with: - platforms: linux/amd64,linux/arm64 + platforms: linux/amd64 push: ${{ github.ref == 'refs/heads/dev' }} tags: ${{ secrets.REGISTRY_URL }}/west/telegram-bot:latest, ${{ secrets.REGISTRY_URL }}/west/telegram-bot:${{ steps.commit_sha.outputs.sha_short }} \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Rules.cs b/modules/ChatManagement/Handler/Rules.cs index 6a80ce6..e6bc472 100644 --- a/modules/ChatManagement/Handler/Rules.cs +++ b/modules/ChatManagement/Handler/Rules.cs @@ -4,45 +4,43 @@ using BotFramework.Enums; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Telegram.Bot; +using Telegram.Bot.Exceptions; namespace West.TelegramBot.ChatManagement.Handler { - internal partial class Rules : ManagementHandler + internal class Rules : ManagementHandler { [Command(InChat.Public, "setrules", CommandParseMode.Both)] public async Task HandleSetRules() { var replyToMessage = Message?.ReplyToMessage; - var text = replyToMessage?.Text; - if (replyToMessage == null || text == null) - { - return; - } - //var entities = replyToMessage.Entities; - //if (entities != null) - //{ - // //foreach (var entity in entities) - // //{ - // // var entityText = text.Substring(entity.Offset, entity.Length); - // // text = text - // // .Remove(entity.Offset, entity.Length) - // // .Insert(entity.Offset) - // //} - //} + if (replyToMessage == null || !await CanPunish()) return; - - await Bot.SendTextMessageAsync(Chat, "tut bilo milo kruzi no on protiv"); + var opt = await Db.UserValues.FindOrCreateOption(Chat.Id, "rulesMsgId"); + opt.SetValue(replyToMessage.MessageId); + Db.MarkAsModified(opt); } [Command(InChat.Public, "rules", CommandParseMode.Both)] public async Task HandleRules() { - var opt = Db.UserValues.FindOrCreateOption(Chat.Id, "rules"); + var opt = await Db.UserValues.FindOption(Chat.Id, "rulesMsgId"); + if (opt == null) + { + return; + } + + try + { + await Bot.CopyMessageAsync(Chat.Id, Chat.Id, opt.GetValue()); + } + catch (ApiRequestException) + { + Db.MarkAsDeleted(opt); + } } - public Rules(CoreContext db) : base(db) - { - } + public Rules(CoreContext db) : base(db) {} } } \ No newline at end of file From eb42b5b22b28ec795d7016feb4fd4d98d3edf2e8 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Tue, 18 Jan 2022 21:38:22 +0200 Subject: [PATCH 4/4] Remove entity formatter. --- Core/Service/EntityFormatter.cs | 48 --------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 Core/Service/EntityFormatter.cs diff --git a/Core/Service/EntityFormatter.cs b/Core/Service/EntityFormatter.cs deleted file mode 100644 index 91df79a..0000000 --- a/Core/Service/EntityFormatter.cs +++ /dev/null @@ -1,48 +0,0 @@ -#nullable enable - -using System.Collections.Generic; -using System.Linq; -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; - -namespace Kruzya.TelegramBot.Core.Service; - -internal class EntityFormatter -{ - private static readonly Dictionary HtmlMap = new() - { - { MessageEntityType.Bold, "{0}" }, - { MessageEntityType.Code, "{0}" }, - { MessageEntityType.Italic, "{0}" }, - { MessageEntityType.TextMention, "{0}" }, - { MessageEntityType.Pre, "
{0}
" }, - { MessageEntityType.Underline, "{0}" } - }; - - private static readonly Dictionary MarkdownMap = new() - { - { MessageEntityType.Bold, "*{0}*" }, - { MessageEntityType.Code, "```{0}```" }, - { MessageEntityType.Italic, "_{0}_" }, - { MessageEntityType.TextMention, "[{0}](tg://user?id={1})" }, - { MessageEntityType.Pre, "```{0}```" }, - { MessageEntityType.Underline, "__{0}__" } - }; - - public string? FormatMessage(Message msg) - { - var text = msg.Text; - var entities = msg.Entities; - - if (entities == null || text == null) return text; - - foreach (var entity in entities.Reverse()) - { - var entityText = text.Substring(entity.Offset, entity.Length); - text = text - .Remove(entity.Offset, entity.Length); - } - - return text; - } -} \ No newline at end of file