InlineQuery.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using CardCollector.DataBase.Entity;
  6. using CardCollector.DataBase.EntityDao;
  7. using Telegram.Bot.Types;
  8. namespace CardCollector.Commands.InlineQuery
  9. {
  10. public abstract class InlineQuery : UpdateModel
  11. {
  12. protected readonly string InlineQueryId = "";
  13. private static readonly List<InlineQuery> List = new()
  14. {
  15. new ShowStickersInGroup(),
  16. //new FilteredInlineQuery(),
  17. };
  18. public static async Task<UpdateModel> Factory(Update update)
  19. {
  20. try
  21. {
  22. // Текст команды
  23. var command = $"{update.InlineQuery!.ChatType}={update.InlineQuery!.Query}";
  24. // Объект пользователя
  25. var user = await UserDao.GetUser(update.InlineQuery!.From);
  26. // Возвращаем объект, если команда совпала
  27. foreach (var item in List.Where(item => item.IsMatches(command)))
  28. if(Activator.CreateInstance(item.GetType(), user, update, update.InlineQuery.Id) is InlineQuery executor)
  29. if (executor.IsMatches(command)) return executor;
  30. // Возвращаем команда не найдена, если код дошел до сюда
  31. return new CommandNotFound(user, update, command);
  32. }
  33. catch (Exception e)
  34. {
  35. Logs.LogOutError(e);
  36. throw;
  37. }
  38. }
  39. protected InlineQuery(UserEntity user, Update update, string inlineQueryId) : base(user, update)
  40. {
  41. InlineQueryId = inlineQueryId;
  42. }
  43. protected InlineQuery() { }
  44. }
  45. }