Ejemplo n.º 1
0
        public virtual async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity != null)
            {
                using (MathBotDataContext dbContext = new MathBotDataContext())
                {
                    ActivityModel model = new ActivityModel();
                    model.Activity = activity.ToJson();
                    dbContext.Activities.Add(model);
                    await dbContext.SaveChangesAsync();
                }

                string message   = activity.Text;
                string _fullText = activity.Text;
                var    _tokens   = _fullText.GetHasTags();
                var    _emails   = _fullText.GetEmails();

                if (activity.Type == ActivityTypes.Message)
                {
                    StateClient sc       = activity.GetStateClient();
                    BotData     userData = sc.BotState.GetPrivateConversationData(activity.ChannelId,
                                                                                  activity.Conversation.Id, activity.From.Id);
                    await Conversation.SendAsync(activity, () => new RootDialog());
                }
                else
                {
                    HandleSystemMessage(activity);
                }
            }
            return(new HttpResponseMessage(System.Net.HttpStatusCode.Accepted));
        }
Ejemplo n.º 2
0
        public bool AddAnswer(Answer model)
        {
            db.Answers.Add(model);
            var _value = db.SaveChangesAsync();

            if (_value.Result >= 0)
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 3
0
        public bool AddQuestion(Question model)
        {
            db.Questions.Add(model);
            var _value = db.SaveChangesAsync();

            if (_value.Result >= 0)
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        public async System.Threading.Tasks.Task <bool> AddUser(UserProfile model)
        {
            var _emails = db.Users.Select(t => t.Email);

            if (!_emails.Contains(model.Email))
            {
                db.Users.Add(model);
                var _value = await db.SaveChangesAsync();

                if (_value >= 0)
                {
                    return(true);
                }
                return(false);
            }
            return(false);
        }
Ejemplo n.º 5
0
        public async System.Threading.Tasks.Task <bool> AddFunctionAsync(CodeFunction model)
        {
            var _functions = db.CodeFunctions.Select(t => t.Name.ToLower());

            if (!_functions.Contains(model.Name.ToLower()))
            {
                db.CodeFunctions.Add(model);
                var _value = await db.SaveChangesAsync();

                if (_value >= 0)
                {
                    return(true);
                }
                return(false);
            }
            return(false);
        }
Ejemplo n.º 6
0
        public async System.Threading.Tasks.Task <string> AddUrlAsync(Url url)
        {
            var _myUrls = db.Urls.Where(t => t.CreatedBy == url.CreatedBy).Select(t => t.Address);

            if (!_myUrls.Contains(url.Address))
            {
                db.Urls.Add(url);
                try
                {
                    await db.SaveChangesAsync();

                    return("Đã thêm vào danh sách Urls.");
                }
                catch (Exception ex)
                {
                    return("Có lỗi khi thêm Url. Mã lỗi: " + ex.Message);
                }
            }
            else
            {
                return("Url đã tồn tại trong danh sách của bạn.");
            }
        }
Ejemplo n.º 7
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);
            }
        }