Esempio n. 1
0
        public void ExecuteCommand(Result result, ref IStorage storage, ref TelegramBot bot, ref EventViews viewer)
        {
            DevByParser  parser     = new DevByParser();
            List <Event> currEvents = parser.GetEvents(2);

            storage.SaveNewEventsToStorage(currEvents);
            storage.SaveUserCheckDate(result.message.from.ID.ToString());
            foreach (var sub in currEvents)
            {
                bot.SendMessageMDCustom(viewer.ToMdFormat(sub), result.message.chat.Id);
            }
        }
        public void Run()
        {
            try
            {
                Timer timer = new Timer(6000);
                timer.AutoReset = true;
                bool timerFlag = false;
                timer.Elapsed += (x, y) => timerFlag = true;
                timer.Start();
                Console.OutputEncoding = Encoding.UTF8;
                while (true)
                {
                    TelegramResponse ResponseFromTelegram = telegramBot.GetUpdate(storage.GetLastUpdateTelegramFromStorage());
                    if (ResponseFromTelegram.result.Count > 0)
                    {
                        TelegramActions operation = new TelegramActions();

                        foreach (var result in ResponseFromTelegram.result)
                        {
                            loger.WriteLog($"Message from {result.message.from.username}\n" +
                                           $"Message Text {result.message.text} \n{DateTime.Now.ToShortTimeString()}");
                            operation.GetCommandFromMessage(result.message.text).ExecuteCommand(result, ref storage, ref telegramBot, ref viewer);
                        }
                        int MaxUpdate = ResponseFromTelegram.result.Max(X => X.update_id);
                        storage.SaveTelegramUpdateToStorage((MaxUpdate + 1).ToString());
                    }
                    if (timerFlag)
                    {
                        DevByParser  parser     = new DevByParser();
                        List <Event> currEvents = parser.GetEvents(2);
                        storage.SaveNewEventsToStorage(currEvents);
                        List <Event> newEvents = storage.GetNewEventsFromStorageForUser("0");
                        if (newEvents.Count > 0)
                        {
                            foreach (var @event in newEvents)
                            {
                                telegramBot.SendMessageMDCustom(viewer.ToMdFormat(@event),
                                                                int.Parse(ConfigurationManager.AppSettings.Get("ChatGeneral ID")));
                            }
                        }
                        timerFlag = false;
                    }
                }
            }
            catch (AggregateException ex)
            {
                foreach (var x in ex.InnerExceptions)
                {
                    loger.WriteLog(x.ToString());
                    telegramBot.SendMessageMe(x.ToString());
                }
            }
        }
Esempio n. 3
0
        public void ExecuteCommand(Result result, ref IStorage storage, ref TelegramBot bot, ref EventViews viewer)
        {
            DevByParser  parser     = new DevByParser();
            List <Event> currEvents = parser.GetEvents(2);

            storage.SaveNewEventsToStorage(currEvents);
            List <Event> newEvents = storage.GetNewEventsFromStorageForUser(result.message.from.ID.ToString());

            storage.SaveUserCheckDate(result.message.from.ID.ToString());
            if (newEvents.Capacity > 0)
            {
                foreach (var sub in newEvents)
                {
                    bot.SendMessageMDCustom(viewer.ToMdFormat(sub), result.message.chat.Id);
                }
            }
            else
            {
                bot.SendMessageMDCustom($"*No new events for you, {result.message.from.first_name}!*", result.message.chat.Id);
            }
        }