Browse Source

Add Ignoring updates and filter inline commands by chat type

Tigran 4 years ago
parent
commit
925069207d

+ 22 - 0
CardCollector/Commands/IgnoreUpdate.cs

@@ -0,0 +1,22 @@
+using System.Threading.Tasks;
+using CardCollector.Controllers;
+using CardCollector.DataBase.Entity;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+
+namespace CardCollector.Commands
+{
+    public class IgnoreUpdate : UpdateModel
+    {
+        protected override string Command => "";
+        public override async Task<Telegram.Bot.Types.Message> Execute()
+        {
+            if (Update.Message?.Chat.Type is ChatType.Private)
+                await MessageController.DeleteMessage(User, Update.Message.MessageId);
+            return new Telegram.Bot.Types.Message();
+        }
+        
+        public IgnoreUpdate(UserEntity user, Update update) : base(user, update) { }
+        public IgnoreUpdate() { }
+    }
+}

+ 0 - 33
CardCollector/Commands/InlineQuery/EmptyInlineQuery.cs

@@ -1,33 +0,0 @@
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using CardCollector.Controllers;
-using CardCollector.DataBase.Entity;
-using Telegram.Bot.Types;
-using Telegram.Bot.Types.InlineQueryResults;
-
-namespace CardCollector.Commands.InlineQuery
-{
-    public class EmptyInlineQuery : InlineQuery
-    {
-        protected override string Command => "";
-        
-        public override async Task<Telegram.Bot.Types.Message> Execute()
-        {
-            var results = new List<InlineQueryResult>
-            {
-                new InlineQueryResultCachedSticker("sticker=", "CAACAgIAAxkBAAIWs2DuY4vB50ARmyRwsgABs_7o5weDaAAC-g4AAmq4cUtH6M1FoN4bxSAE")
-            };
-            await MessageController.AnswerInlineQuery(InlineQueryId, results);
-            return new();
-        }
-
-        protected override bool IsMatches(string command)
-        {
-            return command == Command;
-        }
-
-        public EmptyInlineQuery(UserEntity user, Update update, string inlineQueryId)
-            : base(user, update, inlineQueryId) { }
-        public EmptyInlineQuery() { }
-    }
-}

+ 2 - 2
CardCollector/Commands/InlineQuery/InlineQuery.cs

@@ -14,7 +14,7 @@ namespace CardCollector.Commands.InlineQuery
         
         
         private static readonly List<InlineQuery> List = new()
         private static readonly List<InlineQuery> List = new()
         {
         {
-            new EmptyInlineQuery(),
+            new ShowStickersInGroup(),
             //new FilteredInlineQuery(),
             //new FilteredInlineQuery(),
         };
         };
         
         
@@ -23,7 +23,7 @@ namespace CardCollector.Commands.InlineQuery
             try
             try
             {
             {
                 // Текст команды
                 // Текст команды
-                var command = update.InlineQuery!.Query;
+                var command = $"{update.InlineQuery!.ChatType}={update.InlineQuery!.Query}";
                 
                 
                 // Объект пользователя
                 // Объект пользователя
                 var user = await UserDao.GetUser(update.InlineQuery!.From);
                 var user = await UserDao.GetUser(update.InlineQuery!.From);

+ 47 - 0
CardCollector/Commands/InlineQuery/ShowStickersInGroup.cs

@@ -0,0 +1,47 @@
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using CardCollector.Controllers;
+using CardCollector.DataBase.Entity;
+using Telegram.Bot.Types;
+using Telegram.Bot.Types.Enums;
+using Telegram.Bot.Types.InlineQueryResults;
+
+namespace CardCollector.Commands.InlineQuery
+{
+    public class ShowStickersInGroup : InlineQuery
+    {
+        protected override string Command => "";
+        
+        public override async Task<Telegram.Bot.Types.Message> Execute()
+        {
+            var filter = Update.InlineQuery!.Id;
+            var stickersList = GetStickersList(filter);
+            
+            await MessageController.AnswerInlineQuery(InlineQueryId, stickersList, "название");
+            return new();
+        }
+
+        private IEnumerable<InlineQueryResult> GetStickersList(string filter)
+        {
+            var results = new List<InlineQueryResult>
+            {
+                new InlineQueryResultCachedSticker("send_sticker=", "CAACAgIAAxkBAAIWs2DuY4vB50ARmyRwsgABs_7o5weDaAAC-g4AAmq4cUtH6M1FoN4bxSAE")
+            };
+            return results;
+        }
+
+        protected override bool IsMatches(string command)
+        {
+            return command.Contains(ChatType.Group.ToString()) ||
+                   command.Contains(ChatType.Supergroup.ToString()) ||
+                   command.Contains(ChatType.Private.ToString());
+        }
+
+        public ShowStickersInGroup(UserEntity user, Update update, string inlineQueryId)
+            : base(user, update, inlineQueryId)
+        {
+            
+        }
+        public ShowStickersInGroup() { }
+    }
+}

+ 6 - 3
CardCollector/Commands/Message/Message.cs

@@ -22,11 +22,14 @@ namespace CardCollector.Commands.Message
         {
         {
             try
             try
             {
             {
+                // Объект пользователя
+                var user = await UserDao.GetUser(update.Message!.From);
+                
+                //Если сообщение не содержит текст
+                if (update.Message!.Text == null) return new IgnoreUpdate(user, update);
+                
                 // Текст команды
                 // Текст команды
                 var command = update.Message!.Text;
                 var command = update.Message!.Text;
-                
-                // Объект пользователя
-                var user = await UserDao.GetUser(update.Message.From);
             
             
                 // Добавляем сообщения пользователя в пул для удаления
                 // Добавляем сообщения пользователя в пул для удаления
                 MessageController.AddNewMessageToPool(user, update.Message.MessageId);
                 MessageController.AddNewMessageToPool(user, update.Message.MessageId);

+ 1 - 0
CardCollector/DataBase/Entity/UserEntity.cs

@@ -1,3 +1,4 @@
+using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.ComponentModel.DataAnnotations.Schema;