コード例 #1
0
ファイル: Program.cs プロジェクト: AnnZanevskaya/SOLID
        private static async Task Run()
        {
            TelegramBot bot = new TelegramBot(new Api("172034659:AAEh0DUUKUjNhoLX6LChwafGcWFB7AgSuPY"));
            var me = await bot.BotApi.GetMe();
            CommandsDictionary dictionary = new CommandsDictionary(bot);
            ArtIntCommandsDictionary aiDictionary = new ArtIntCommandsDictionary();
          

            dictionary.AddCommand("/weather", new WeatherCommand<WeatherEntity>(bot, new WeatherService(), new WeatherBuilder()));
            dictionary.AddCommand("/currency", new CurrencyCommand<CurrencyEntity>(bot, new CurrencyService(), new CurrencyBuilder()));
            dictionary.AddCommand("/ai", new ArtIntCommand<ArtIntEntity>(bot, new ArtIntBuilder(), new ArtIntService(aiDictionary)));
            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await bot.BotApi.GetUpdates(offset);

                foreach (var update in updates)
                {
                    ICommand command = dictionary.GetCommandIfExist(update.Message.Text.GetFirstWord());
                    command.Execute(update);
                    
                    offset = update.Id + 1;
                }
            }
            await Task.Delay(1000);
        }
コード例 #2
0
 /// <summary>
 /// Tries to find and execute command from CommandsDictionry
 /// </summary>
 /// <param name="command">Command name, command parameters</param>
 public void HandleCommand(string command)
 {
     if (command != "execute")
     {
         if (CommandsDictionary.ContainsKey(command))
         {
             commandsList.Add(CommandsDictionary[command]);
         }
         else if (!TryToCreateWithArguments(command))
         {
             Console.WriteLine("Wrong command.");
         }
     }
     else
     {
         foreach (ICommand Command in commandsList)
         {
             Command.Execute();
         }
         commandsList.Clear();
     }
 }