Example #1
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ru-RU");

            var dbContext      = new NureBotDbContext(false);
            var unityContainer = new UnityContainer();

            dbContext.Database.Initialize(true);
            ContainerBoostraper.RegisterTypes(unityContainer, dbContext, "356520093:AAGKBe8YFpR5_5WIkGfoeRbdTMuOKE2O9GQ");

            Bot               = unityContainer.Resolve <TelegramBotClient>();
            UService          = unityContainer.Resolve <IUserService>();
            RecognitionSystem = unityContainer.Resolve <OscovaBot>();

            Console.Title = Bot.GetMeAsync().Result.Username;

            Bot.OnMessage      += Bot_OnMessage;
            Bot.OnReceiveError += Bot_OnReceiveError;

            RecognitionSystem.Dialogs.Add(new CistDialog());
            RecognitionSystem.Dialogs.Add(new WeatherDialog());

            RecognitionSystem.MainUser.Context.SharedData.Add(Bot);
            RecognitionSystem.MainUser.Context.SharedData.Add(UService);
            RecognitionSystem.Language.Stemmer   = new RussianStemmer();
            RecognitionSystem.Language.StopWords = StopWordsGenerator.GenerateRussianStopWords();

            RecognitionSystem.Trainer.StartTraining();
            RecognitionSystem.Recognizers.Clear();
            RecognitionSystem.Recognizers.Add(new RuDateRecognizer());
            RecognitionSystem.Language.Culture.DateTimeFormat =
                CultureInfo.GetCultureInfo("ru-RU").DateTimeFormat;
            RecognitionSystem.Recognizers.Add(new GroupRecognizer());
            RecognitionSystem.CreateRecognizer("teacherName", new Regex(@"[А-Яа-я]+ [А-Яа-я]\.? [А-Яа-я]\.?"));


            RecognitionSystem.MainUser.ResponseReceived += async(sender, arg) =>
            {
                var msg = JsonConvert.DeserializeObject <MessageTransfer>(arg.Response.Text);

                await Bot.SendTextMessageAsync(msg.ChatId, msg.Message, replyMarkup : msg.ReplyMarkup);
            };


            Bot.SetWebhookAsync();
            Bot.StartReceiving();

            Console.BackgroundColor = ConsoleColor.DarkGreen;
            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine(@"Service started");
            Console.WriteLine(@"Press Enter to Stop Service!");
            Console.ReadLine();
            Bot.StopReceiving();

            dbContext.Dispose();
            unityContainer.Dispose();
        }
        public MainViewModel()
        {
            var dbContext      = new NureBotDbContext(false);
            var unityContainer = new UnityContainer();

            dbContext.Database.Initialize(true);
            ContainerBoostraper.RegisterTypes(unityContainer, dbContext, "356520093:AAGKBe8YFpR5_5WIkGfoeRbdTMuOKE2O9GQ");

            Bot               = unityContainer.Resolve <TelegramBotClient>();
            UService          = unityContainer.Resolve <IUserService>();
            RecognitionSystem = unityContainer.Resolve <OscovaBot>();

            Bot.OnMessage      += Bot_OnMessageAsync;;
            Bot.OnReceiveError += Bot_OnReceiveError;;

            RecognitionSystem.Dialogs.Add(new CistDialog());
            RecognitionSystem.Dialogs.Add(new WeatherDialog());

            RecognitionSystem.MainUser.Context.SharedData.Add(Bot);
            RecognitionSystem.MainUser.Context.SharedData.Add(UService);
            RecognitionSystem.Language.Stemmer   = new RussianStemmer();
            RecognitionSystem.Language.StopWords = StopWordsGenerator.GenerateRussianStopWords();

            RecognitionSystem.Trainer.StartTraining();
            RecognitionSystem.Recognizers.Clear();
            RecognitionSystem.Recognizers.Add(new RuDateRecognizer());
            RecognitionSystem.Language.Culture.DateTimeFormat =
                CultureInfo.GetCultureInfo("ru-RU").DateTimeFormat;
            RecognitionSystem.Recognizers.Add(new GroupRecognizer());
            RecognitionSystem.CreateRecognizer("teacherName", new Regex(@"[А-Яа-я]+ [А-Яа-я]\.? [А-Яа-я]\.?"));


            RecognitionSystem.MainUser.ResponseReceived += async(sender, arg) =>
            {
                var msg = JsonConvert.DeserializeObject <MessageTransfer>(arg.Response.Text);

                await Bot.SendTextMessageAsync(msg.ChatId, msg.Message, replyMarkup : msg.ReplyMarkup);

                SuccessMessage($"Reply sent:{msg.ChatId}");
            };
        }