Пример #1
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            // Set BaseURL
            context.UserData.TryGetValue <string>("CurrentBaseURL", out strBaseURL);

            //var activity = await result as Activity;
            var    activity     = await result as Activity;
            string message      = activity.Text;
            string lowerMessage = activity.Text.ToLower();

            if (lowerMessage.StartsWith("#>"))
            {
                Guest  _guest = new Guest(context, lowerMessage);
                QnABot _bot   = new QnABot();
                await _bot.AnswerAsync(_guest);

                //await _guest.ExcuteAsync();
            }
            else
            {
                QnABot _bot = new QnABot(context, message);
                if (lowerMessage == "#where")
                {
                    await _bot.TalkAsync(activity.AsMessageActivity().PrintAllProperties());
                }
                //else if (lowerMessage.StartsWith("#Translate".ToLower())
                //    || lowerMessage.StartsWith("#Trans".ToLower()))
                //{
                //    await _bot.Talk(activity.AsMessageActivity());
                //}
                else if (lowerMessage.StartsWith("#Update Email".ToLower()))
                {
                    await context.Forward(
                        new ProfileDialog(),
                        this.AfterGameDialogCompleted,
                        message,
                        CancellationToken.None);
                }
                //else if (lowerMessage.StartsWith("#Search".ToLower()))
                //{
                //    string _keyword = activity.Text.Replace("#Search", "");
                //    var _result = new GoogleServices().Search(_keyword);
                //    foreach (var item in _result.Items)
                //    {
                //        if (!item.Title.IsNullOrEmptyOrWhiteSpace())
                //            await BotTalk(context, item.Title);
                //        if (!item.Link.IsNullOrEmptyOrWhiteSpace())
                //            await BotTalk(context, item.Link);
                //    }
                //}
                else if (lowerMessage.StartsWith("#USD->VND".ToLower()))
                {
                    string  _amountStr = activity.Text.Replace("#USD->VND ", "");
                    decimal _amount    = decimal.Parse(_amountStr);
                    string  _value     = FinanceService.CurrencyConvert(_amount, "USD", "VND");

                    await _bot.TalkAsync(_value);
                }
                else if (lowerMessage.StartsWith("#Users".ToLower()))
                {
                    using (Models.TiTiBotDataContext dataContext = new Models.TiTiBotDataContext())
                    {
                        var _users = dataContext.Users;
                        foreach (var _user in _users)
                        {
                            await _bot.TalkAsync(_user.UserName);
                        }
                    }
                }
                //else if (lowerMessage.StartsWith("#View Profile".ToLower()))
                //{
                //    using (Models.TiTiBotDataContext dataContext = new Models.TiTiBotDataContext())
                //    {
                //        var newActivity = Mapper.Map<IMessageActivity, Models.ActivityBo>(activity);
                //        var _user = dataContext.Users.Where(t => t.UserName == newActivity.FromName).FirstOrDefault();
                //        if (_user != null)
                //        {
                //            try
                //            {
                //                await BotTalk(context, _user.PrintAllProperties());
                //                await BotTalk(context, newActivity.PrintAllProperties());
                //            }
                //            catch (Exception ex)
                //            {
                //                await BotTalk(context, ex.Message);
                //            }
                //        }
                //    }
                //}
                //http://vietlott.vn/vi/trung-thuong/ket-qua-trung-thuong/mega-6-45/winning-numbers/
                //else if (lowerMessage.StartsWith("#vietlott"))
                //{
                //    string _url = "http://vietlott.vn/vi/trung-thuong/ket-qua-trung-thuong/mega-6-45/winning-numbers/";
                //    await BotTalk(context, "Now i'm reading from (url): " + _url);
                //    WebPage _page = new WebPage(_url.Trim());
                //    if (_page.Rows != null && _page.Rows.Count > 0)
                //    {
                //        var _printRows = _page.Rows.Take(6).ToArray();
                //        for(int i = 1; i <= 6; i++)
                //        {
                //            await BotTalk(context, _printRows[i]);
                //        }
                //    }
                //}
                //else if (lowerMessage.StartsWith("#read") && lowerMessage.IsContainsUrl())
                //{
                //    UrlMessage _message = new UrlMessage(lowerMessage);
                //    _message.context = context;

                //    if (_message == null)
                //    {
                //        await BotTalkFinished(context);
                //    }
                //    await BotTalkStart(context, _message);
                //    await _message.ExcuteAsync();
                //    await BotTalkEnd(context, _message);
                //}
                //else if (lowerMessage.IsMathExpression())
                //{
                //    var _expr = MathEngine.ToMathExpr(lowerMessage);
                //    var _result = MathEngine.GetResult(_expr);
                //    //await BotTalk(context, _result);
                //}
                //else if (lowerMessage.StartsWith("#HotGirl".ToLower()))
                //{
                //    await context.Forward(
                //        new GameDialog(),
                //        this.AfterGameDialogCompleted,
                //        message,
                //        CancellationToken.None);
                //}
                else
                {
                    await context.Forward(
                        new QnADialog(),
                        this.ResumeAfterDoctorDialog,
                        message,
                        CancellationToken.None);

                    //context.Wait(MessageReceivedAsync);
                }
            }
        }