Browse Source

Sending stickers via bot and getting chosen results

Tigran 4 years ago
parent
commit
04f545af98

+ 54 - 0
CardCollector/Commands/ChosenInlineResult/ChosenInlineResult.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using CardCollector.DataBase.Entity;
+using CardCollector.DataBase.EntityDao;
+using Telegram.Bot.Types;
+
+namespace CardCollector.Commands.ChosenInlineResult
+{
+    public abstract class ChosenInlineResult : UpdateModel
+    {
+        protected readonly string InlineResult = "";
+        
+        private static readonly List<ChosenInlineResult> List = new()
+        {
+            //new StickerInlineResult(),
+        };
+        
+        public static async Task<UpdateModel> Factory(Update update)
+        {
+            try
+            {
+                // Текст команды
+                var command = update.ChosenInlineResult!.ResultId;
+                
+                // Объект пользователя
+                var user = await UserDao.GetUser(update.ChosenInlineResult!.From);
+                
+                // Возвращаем объект, если команда совпала
+                foreach (var item in List.Where(item => item.IsMatches(command)))
+                    if(Activator.CreateInstance(item.GetType(), 
+                        user, update, update.ChosenInlineResult.ResultId) is ChosenInlineResult executor)
+                        if (executor.IsMatches(command)) return executor;
+            
+                // Возвращаем команда не найдена, если код дошел до сюда
+                return new CommandNotFound(user, update, command);
+            }
+            catch (Exception e)
+            {
+                Logs.LogOutError(e);
+                throw;
+            }
+        }
+
+        protected ChosenInlineResult(UserEntity user, Update update, string inlineResult)
+            : base(user, update)
+        {
+            InlineResult = inlineResult;
+        }
+
+        protected ChosenInlineResult() { }
+    }
+}

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

@@ -0,0 +1,33 @@
+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() { }
+    }
+}

+ 53 - 0
CardCollector/Commands/InlineQuery/InlineQuery.cs

@@ -0,0 +1,53 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using CardCollector.DataBase.Entity;
+using CardCollector.DataBase.EntityDao;
+using Telegram.Bot.Types;
+
+namespace CardCollector.Commands.InlineQuery
+{
+    public abstract class InlineQuery : UpdateModel
+    {
+        protected readonly string InlineQueryId = "";
+        
+        private static readonly List<InlineQuery> List = new()
+        {
+            new EmptyInlineQuery(),
+            //new FilteredInlineQuery(),
+        };
+        
+        public static async Task<UpdateModel> Factory(Update update)
+        {
+            try
+            {
+                // Текст команды
+                var command = update.InlineQuery!.Query;
+                
+                // Объект пользователя
+                var user = await UserDao.GetUser(update.InlineQuery!.From);
+                
+                // Возвращаем объект, если команда совпала
+                foreach (var item in List.Where(item => item.IsMatches(command)))
+                    if(Activator.CreateInstance(item.GetType(), user, update, update.InlineQuery.Id) is InlineQuery executor)
+                        if (executor.IsMatches(command)) return executor;
+            
+                // Возвращаем команда не найдена, если код дошел до сюда
+                return new CommandNotFound(user, update, command);
+            }
+            catch (Exception e)
+            {
+                Logs.LogOutError(e);
+                throw;
+            }
+        }
+
+        protected InlineQuery(UserEntity user, Update update, string inlineQueryId) : base(user, update)
+        {
+            InlineQueryId = inlineQueryId;
+        }
+        
+        protected InlineQuery() { }
+    }
+}

+ 11 - 0
CardCollector/Controllers/MessageController.cs

@@ -3,15 +3,19 @@ using System.Collections.Generic;
 using System.Threading;
 using System.Threading.Tasks;
 using CardCollector.DataBase.Entity;
+using CardCollector.Resources;
 using Telegram.Bot;
 using Telegram.Bot.Exceptions;
 using Telegram.Bot.Types;
 using Telegram.Bot.Types.Enums;
+using Telegram.Bot.Types.InlineQueryResults;
 using Telegram.Bot.Types.InputFiles;
 using Telegram.Bot.Types.ReplyMarkups;
 using Message = CardCollector.Commands.Message.Message;
 using CallBackQuery = CardCollector.Commands.CallbackQuery.CallbackQuery;
 using MyChatMember = CardCollector.Commands.MyChatMember.MyChatMember;
+using InlineQuery = CardCollector.Commands.InlineQuery.InlineQuery;
+using ChosenInlineResult = CardCollector.Commands.ChosenInlineResult.ChosenInlineResult;
 using TgMessage = Telegram.Bot.Types.Message;
 
 namespace CardCollector.Controllers
@@ -31,6 +35,8 @@ namespace CardCollector.Controllers
                     UpdateType.Message => await Message.Factory(update),
                     UpdateType.CallbackQuery => await CallBackQuery.Factory(update),
                     UpdateType.MyChatMember => await MyChatMember.Factory(update),
+                    UpdateType.InlineQuery => await InlineQuery.Factory(update),
+                    UpdateType.ChosenInlineResult => await ChosenInlineResult.Factory(update),
                     _ => throw new ArgumentOutOfRangeException()
                 };
                 // var message =
@@ -191,5 +197,10 @@ namespace CardCollector.Controllers
             }
             return new TgMessage();
         }
+
+        public static async Task AnswerInlineQuery(string queryId, IEnumerable<InlineQueryResult> results, string offset = null)
+        {
+            await Bot.Client.AnswerInlineQueryAsync(queryId, results, isPersonal: true, nextOffset: offset, cacheTime: Constants.INLINE_RESULTS_CACHE_TIME);
+        }
     }
 }

+ 1 - 0
CardCollector/Resources/Constants.cs

@@ -6,5 +6,6 @@ namespace CardCollector.Resources
 
         public const int MEMORY_CLEANER_INTERVAL = 60 * 1000;
         public const double SAVING_CHANGES_INTERVAL = DEBUG ? 10 * 1000 : 5 * 60 * 1000;
+        public const int INLINE_RESULTS_CACHE_TIME = 5;
     }
 }