예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            TelegramApi telegram = new TelegramApi(_token);
            try
            {
                Console.WriteLine("Executing GetMe");
                var botUser = telegram.GetMe();
                Console.WriteLine("GetMe returned \n" + botUser.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("GetMe failed");
                Console.WriteLine(e.Message);
                return;
            }

            using (var db = new AppDbContext())
            {
                db.SaveChanges();
                var query = from b in db.Suggestions
                            orderby b.Id
                            select b;
                Console.WriteLine("All suggestions:");
                foreach (var item in query)
                {
                    Console.WriteLine(item.Id + " " + item.Text);
                }
                //Console.ReadKey();
            }

            /*
            ReplyKeyboardMarkup replyKB = new ReplyKeyboardMarkup();
            replyKB.Keyboard = new string[][]
                    {
                        new string[] {"Yes","No"}

                    };
            replyKB.OneTimeKeyboard = false;
            replyKB.ResizeKeyboard = false;
            replyKB.Selective = false;*/

            //string json = JsonConvert.SerializeObject(replyKB);

            StartPolling(telegram);

            var processingCommandUsers = new ConcurrentDictionary<Telegram.User, bool>();
            Regex commandRegex = new Regex(@"(/\w+)\s*");

            while (true)
            {
                foreach (var update in telegram.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.FirstName} {message.From.LastName}");*/
                    string commandTitle = commandRegex.Match(message.Text).Groups[1].Value;

                    /*ReplyKeyboardMarkup replyKeyboard = new ReplyKeyboardMarkup() {Keyboard = new string[][] {
                                                                                       new string[] {"Yes","No"}
                                                                                   },
                                                                                   OneTimeKeyboard = true
                    };*/

                    string testText = message.Text;

                    Console.WriteLine("Received a message: " + testText);

                    //Logger.Debug($"Creating command object for '{commandTitle}'");
                    var command = Command.CreateCommand(commandTitle);
                    //Logger.Info($"Received {command.GetType().Name} from " +
                    //            $"{message.From.FirstName} {message.From.LastName}");

                    command.TelegramApi = telegram;
                    command.Message = message;

                    //Logger.Debug($"Executing {command.GetType().Name}");
                    processingCommandUsers[update.Key] = true;

                    try
                    {
                       // telegram.SendMessage(message.From, testText, replyKeyboard);
                     //   telegram.SendMessage(message.From, " ", replyKeyboard);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }

                    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);*/
                            Console.WriteLine(e.Message);
                        }
                    }
                        );
                    commandTask.ContinueWith(task =>
                    {
                        processingCommandUsers[update.Key] = false;
                        /*Logger.Debug($"{command.GetType().Name} from " +
                                     $"{message.From.FirstName} {message.From.LastName} {(command.Status ? "succeeded" : "failed")}");*/
                    });
                }
                Thread.Sleep(200);
            }
        }