Пример #1
0
        public void Excute()
        {
            switch (Command)
            {
            case "Trans":
                Translator translator = new Translator();
                if (Parameters.Count > 0)
                {
                    Result = translator.Trans(Parameters.ToArray()[0]);
                }
                break;

            case "Calc":
                MathEngine mathEngine = new MathEngine();
                if (Parameters.Count > 0)
                {
                    Result = mathEngine.Calc(Parameters.ToArray()[0]);
                }
                break;

            default:
                Result = null;
                break;
            }
            ;
        }
Пример #2
0
        public async Task BotAnswer(IDialogContext context, string question)
        {
            string _answer = "Bạn đã hỏi. Tôi đang tìm câu trả lời";

            if (question.EndsWith("?"))
            {
                question = question.Trim().Remove(question.Length - 1, 1).Trim();
            }
            if (question.EndsWith("="))
            {
                question = question.Trim().Remove(question.Length - 1, 1).Trim();
            }
            MathEngine mathEngine = new MathEngine();

            if (mathEngine.IsMathExpression(question))
            {
                var _expr = mathEngine.Calc(question);
                _answer = _expr;
                await BotTalk(context, _answer);

                BotStories.Add(_answer);
            }
            else
            {
                string _msg = "Xin lỗi. Biểu thức toán học của bạn không đúng. Vui lòng hỏi lại.";
                await BotTalk(context, _msg);

                BotStories.Add(_msg);
            }

            string _functionName = question.GetFromBeginTo("(");
            //string _functionParams = question.GetB
        }
Пример #3
0
        public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var message = await result;

            _manager  = new QuestionManager();
            Questions = new List <Question>();
            Questions = _manager.GenerateRandomQuestions(NumbersOfQuestions);

            if (message.Text.EndsWith("?"))
            {
                await BotAnswer(context, message.Text);
            }
            else if (message.Text.IsNumeric())
            {
                var _lastQuestion = Questions[i].Content;
                var _expr         = mathEngine.Calc(_lastQuestion);
                var _answer       = _expr;

                if (_answer.ToLower() == message.Text.ToLower())
                {
                    await context.PostAsync($"Đúng");
                }
                else
                {
                    await context.PostAsync($"Sai");
                }
                i = i + 1;
                await BotAsk(context, i);
            }
            else if (message.Text.Contains("test"))
            {
                await BotAsk(context, i);
            }
            else
            {
                await new RootDialog().MessageReceivedAsync(context, result);
            }
        }
Пример #4
0
        public async Task ProcessMessageAsync(IDialogContext context, IMessageActivity message)
        {
            UserMessage userText = new UserMessage(message.Text);

            if (userText.Type == UserMessageType.Url)
            {
                UrlModel url = new UrlModel(userText.Text);
                if (url.Ping())
                {
                    using (MathBotDataContext dbContext = new MathBotDataContext())
                    {
                        if (!dbContext.Urls.Select(t => t.Url).Contains(url.Url))
                        {
                            try
                            {
                                dbContext.Urls.Add(url);
                                await dbContext.SaveChangesAsync();
                                await BotTalk(context, MakeBotMessage(userText));
                            }
                            catch (Exception ex)
                            {
                                await BotTalk(context, ex.Message);
                            }
                        }
                        else
                        {
                            await BotTalk(context, url.Url + " is exits");
                        }
                    }
                }
                return;
            }
            else if (userText.Type == UserMessageType.MathExpression)
            {
                MathEngine mathEngine = new MathEngine();
                string     _result    = mathEngine.Calc(userText.Text);
                await BotTalk(context, _result);

                return;
            }
            else if (userText.Type == UserMessageType.DatabaseCommand)
            {
                string command = "";
                string sql     = userText.Text.GetFromEndTo("#");
                string table   = userText.Text.NextWords("#")[0];
                using (MathBotDataContext dbContext = new MathBotDataContext())
                {
                    if (table == "Url")
                    {
                        string lastUrl = dbContext.Urls
                                         .OrderByDescending(t => t.CreatedDate)
                                         .FirstOrDefault().Url;
                        await BotTalk(context, lastUrl);

                        return;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else if (userText.Type == UserMessageType.Translate)
            {
                await BotTranslate(context, userText);

                return;
            }
            else if (userText.Type == UserMessageType.ChangeCurrency)
            {
                if (userText.Text.ToLower().EndsWith("vnd"))
                {
                    string  amount       = userText.Text.GetFromBeginTo("?").Trim();
                    decimal _amount      = decimal.Parse(amount);
                    string  endToken     = userText.Text.GetFromEndTo("?").Trim();
                    var     results      = Convertor.CurrencyConvert(_amount, "USD", "VND");
                    string  replyMessage = results;
                    await BotTalk(context, replyMessage);
                }
                return;
            }
            else if (userText.Text.StartsWith("#vietlott"))
            {
                string _url = "http://vietlott.vn/vi/trung-thuong/ket-qua-trung-thuong/";
                await BotTalk(context, "Now i'm reading from (url): " + _url);

                WebPage _page = new WebPage(_url.Trim());
                if (_page.HtmlTables != null && _page.HtmlTables.Count > 0)
                {
                    foreach (var table in _page.HtmlTables)
                    {
                        foreach (var row in table.Rows)
                        {
                            if (row.IsContainsNumber())
                            {
                                string text    = row.ToHtmlDocument().DocumentNode.InnerText;
                                var    numbers = text.GetNumbers()
                                                 .WordsToString();
                                if (!numbers.IsNullOrEmptyOrWhiteSpace())
                                {
                                    await BotTalk(context, numbers);
                                }
                            }
                        }
                    }
                }
            }
            else if (userText.Type == UserMessageType.Search)
            {
                string command = userText.Text;
                var    results = new GoogleServices()
                                 .Search(command);
                var items = results.Items.TakeMax(5);
                foreach (var item in results.Items)
                {
                    await BotTalk(context, item.HtmlSnippet);
                }
                return;
            }
            else if (userText.Type == UserMessageType.ImageSearch)
            {
                string command = userText.Text;
                var    results = new GoogleServices()
                                 .SearchImage(command);
                var items = results.Items.TakeMax(5);
                foreach (var item in results.Items)
                {
                    await BotTalk(context, item.HtmlSnippet);
                }
                return;
            }
            else if (userText.Type == UserMessageType.SayGoodbye)
            {
                await BotTalk(context, "Goodbye");

                return;
            }
            else if (userText.Type == UserMessageType.SayHello)
            {
                await BotTalk(context, "Hello");

                return;
            }
            else
            {
                await this.SendUnknowMessageAsync(context);
            }
        }