static void Main() { Logger.Info($"HousewifeBot started: {Assembly.GetEntryAssembly().Location}"); if (!LoadSettings()) { return; } TelegramApi tg = new TelegramApi(_token); try { Logger.Debug("Executing GetMe"); var botUser = tg.GetMe(); Logger.Debug($"GetMe returned {botUser}"); } catch (Exception e) { Logger.Error("GetMe failed"); Logger.Error(e); return; } Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, DAL.Migrations.Configuration>()); Notifier notifier = new Notifier(tg); var updateNotificationsTask = new Task( () => { while (true) { notifier.UpdateNotifications(); Thread.Sleep(_updateNotificationsInterval); } } ); updateNotificationsTask.Start(); var sendEpisodesNotificationsTask = new Task( () => { while (true) { notifier.SendEpisodesNotifications(); Thread.Sleep(_sendNotificationsInterval); } } ); var sendShowsNotificationsTask = new Task( () => { while (true) { notifier.SendShowsNotifications(); Thread.Sleep(_sendShowNotificationsInterval); } }); sendEpisodesNotificationsTask.Start(); sendShowsNotificationsTask.Start(); var processingCommandUsers = new ConcurrentDictionary<User, bool>(); Regex commandRegex = new Regex(@"(/\w+)\s*"); StartPolling(tg); while (true) { foreach (var update in tg.Updates) { if (processingCommandUsers.ContainsKey(update.Key) && processingCommandUsers[update.Key]) { continue; } if (update.Value.Count == 0) { continue; } Message message; update.Value.TryDequeue(out message); Logger.Debug($"Received message '{message.Text}' from " + $"{message.From}"); string commandTitle; try { commandTitle = commandRegex.Match(message.Text).Groups[1].Value; } catch (Exception e) { Logger.Error(e, "An error occurred while parsing command title"); continue; } Logger.Debug($"Creating command object for '{message.Text}'"); var command = Command.CreateCommand(commandTitle); Logger.Info($"Received {command.GetType().Name} from " + $"{message.From}"); command.TelegramApi = tg; command.Message = message; Logger.Debug($"Executing {command.GetType().Name}"); processingCommandUsers[update.Key] = true; Task commandTask = Task.Run(() => { try { command.Execute(); } catch (Exception e) { Logger.Error($"An error occurred while executing {command.GetType().Name}.\n" + $"Message: {command.Message.Text}\n" + $"Arguments: {command.Arguments}\n" + $"User: {command.Message.From}"); Logger.Error(e); } } ); commandTask.ContinueWith(task => { processingCommandUsers[update.Key] = false; Logger.Debug($"{command.GetType().Name} from " + $"{message.From} {(command.Status ? "succeeded" : "failed")}"); }); } Thread.Sleep(200); } }
private static void StartSendShowsNotificationsTask(Notifier notifier) { sendShowsNotificationsTask = new Task( () => { while (true) { notifier.SendShowsNotifications(); Thread.Sleep(sendShowNotificationsInterval); } }); sendShowsNotificationsTask.Start(); }
private static void StartUpdateNotificationsTask(Notifier notifier) { updateNotificationsTask = new Task( () => { while (true) { notifier.UpdateNotifications(); Thread.Sleep(updateNotificationsInterval); } }); updateNotificationsTask.Start(); }
private static void Main() { Logger.Info($"HousewifeBot started: {Assembly.GetEntryAssembly().Location}"); if (!LoadSettings()) { return; } TelegramApi tg = new TelegramApi(token); try { Logger.Debug("Executing GetMe"); var botUser = tg.GetMe(); Logger.Debug($"GetMe returned {botUser}"); } catch (Exception e) { Logger.Error("GetMe failed"); Logger.Error(e); return; } Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, DAL.Migrations.Configuration>()); Notifier notifier = new Notifier(tg); StartUpdateNotificationsTask(notifier); StartSendEpisodesNotificationsTask(notifier); StartSendShowsNotificationsTask(notifier); var processingCommandUsers = new ConcurrentDictionary<User, bool>(); StartPolling(tg); while (true) { foreach (var update in tg.Updates) { if (processingCommandUsers.ContainsKey(update.Key) && processingCommandUsers[update.Key]) { continue; } if (update.Value.Count == 0) { continue; } Message message; update.Value.TryDequeue(out message); Logger.Debug($"Received message '{message.Text}' from {message.From}"); var command = GetCommand(tg, message); if (command == null) { continue; } Logger.Debug($"Executing {command.GetType().Name}"); processingCommandUsers[update.Key] = true; var commandTask = StartCommandTask(command); commandTask.ContinueWith(task => { processingCommandUsers[update.Key] = false; Logger.Debug($"{command.GetType().Name} from {message.From} {(command.Status ? "succeeded" : "failed")}"); }); } Thread.Sleep(200); } }