Esempio n. 1
0
        public async Task OnProcessRequest(IBotContext context, MiddlewareSet.NextDelegate next)
        {
            await context.SendActivity("I'm QnAMakerMiddleware");

            if (context.Request.Type == ActivityTypes.Message)
            {
                var messageActivity = context.Request.AsMessageActivity();
                if (!string.IsNullOrEmpty(messageActivity.Text))
                {
                    var results = await qnAMaker.GetAnswers(messageActivity.Text.Trim());

                    if (results.Any())
                    {
                        if (!string.IsNullOrEmpty(qaOptions.DefaultAnswerPrefixMessage))
                        {
                            await context.SendActivity(qaOptions.DefaultAnswerPrefixMessage);
                        }

                        await context.SendActivity(results.First().Answer);

                        if (qaOptions.EndActivityRoutingOnAnswer)
                        {
                            return;
                        }
                    }
                }
            }
            await next();
        }
Esempio n. 2
0
        public RootTopic(IBotContext context) : base(context)
        {
            this.SubTopics.Add("namePrompt", (object[] args) =>
            {
                var namePrompt = new TextPrompt();

                namePrompt.Set
                .OnPrompt("What is your name?")
                .OnSuccess((ctx, value) =>
                {
                    this.ClearActiveTopic();

                    this.State.Name = value;

                    this.OnReceiveActivity(context);
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.SendActivity("I'm sorry I'm having issues understanding you.");

                    this.OnReceiveActivity(context);
                });

                return(namePrompt);
            });

            this.SubTopics.Add("agePrompt", (object[] args) =>
            {
                var agePrompt = new IntPrompt();

                agePrompt.Set
                .OnPrompt("How old are you?")
                .OnSuccess((ctx, value) =>
                {
                    this.ClearActiveTopic();

                    this.State.Age = value;

                    this.OnReceiveActivity(context);
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.SendActivity("I'm sorry I'm having issues understanding you.");

                    this.OnReceiveActivity(context);
                });

                return(agePrompt);
            });
        }
Esempio n. 3
0
        public override Task OnReceiveActivity(IBotContext context)
        {
            if (context.Request.Type == ActivityTypes.Message)
            {
                // Check to see if there is an active topic.
                if (this.HasActiveTopic)
                {
                    // Let the active topic handle this turn by passing context to it's OnReceiveActivity().
                    this.ActiveTopic.OnReceiveActivity(context);
                    return(Task.CompletedTask);
                }

                // If you don't have the state you need, prompt for it
                if (this.State.Name == null)
                {
                    this.SetActiveTopic("namePrompt")
                    .OnReceiveActivity(context);
                    return(Task.CompletedTask);
                }

                if (this.State.Age == null)
                {
                    this.SetActiveTopic("agePrompt")
                    .OnReceiveActivity(context);
                    return(Task.CompletedTask);
                }

                // Now that you have the state you need (age and name), use it!
                context.SendActivity($"Hello { this.State.Name }! You are { this.State.Age } years old.");
                return(Task.CompletedTask);
            }

            return(Task.CompletedTask);
        }
Esempio n. 4
0
 public async Task OnReceiveActivity(IBotContext context)
 {
     if (context.Request.Type is ActivityTypes.Message)
     {
         await context.SendActivity($"Hello world.");
     }
 }
Esempio n. 5
0
        public async Task OnProcessRequest(IBotContext context, MiddlewareSet.NextDelegate next)
        {
            //Check whether message contains images. If it doens't contain, pass process to later middleware.
            Attachment imageAttachment = context.Request.Attachments?.FirstOrDefault(attachment => attachment.ContentType.Contains("image"));

            if (imageAttachment == null)
            {
                await next();
            }
            else
            {
                ImageRecognizeResult result = new ImageRecognizeResult();

                var computerVisionResult = await ComputerVisionRecognizer.DetectImage(await GetImageStream(imageAttachment), this.RecognizeThreshold) as ComputerVisionResult;

                if (computerVisionResult.IsSure)
                {
                    result.RecognizedServiceType = ImageServiceType.ComputerVisionService;
                    result.ComputerVisionResult  = computerVisionResult;
                    context.Set(ImageRecognizerResultKey, result);
                    await context.SendActivity($"Computer Vision: This picture is {result.ComputerVisionResult.Result.Landmarks?[0].Name}");
                    await next();
                }
                else
                {
                    var customVisionResult = await CustomVisionRecognizer.DetectImage(await GetImageStream(imageAttachment), this.RecognizeThreshold) as CustomVisionResult;

                    if (customVisionResult.IsSure)
                    {
                        result.RecognizedServiceType = ImageServiceType.CustomVisionService;
                        result.CustomVisionResult    = customVisionResult;
                        context.Set(ImageRecognizerResultKey, result);
                        await context.SendActivity($"Custom Vision: This picture is {result.CustomVisionResult.PredictionResultModel.Predictions?[0].Tag}");
                        await next();
                    }
                    else
                    {
                        //TODO: Will use bing image recognizer to detect image
                        await next();

                        //await context.SendActivity("Middleware debug: Also used Bing Image Search");
                        //var bingImageResult = await BingImageRecognizer.DetectImage(stream, this.RecognizeThreshold);
                    }
                }
            }
        }
Esempio n. 6
0
        public async Task PassTheMessageToUserFromAgent(IBotContext context, string message)
        {
            Debug.WriteLine("************************");
            Debug.WriteLine("PassTheMessageToUserFromAgent!!!!");
            Debug.WriteLine("************************");
            await context.SendActivity(message);

            return;
        }
        private void ShowHelp(IBotContext context)
        {
            var message = "Here's what I can do:\n\n";

            message += "To see your alarms, say 'Show Alarms'.\n\n";
            message += "To add an alarm, say 'Add Alarm'.\n\n";
            message += "To delete an alarm, say 'Delete Alarm'.\n\n";
            message += "To see this again, say 'Help'.";

            context.SendActivity(message);
        }
Esempio n. 8
0
        public Task ok_OnReceiveActivity(IBotContext context)
        {
            if (HasActiveTopic)
            {
                ActiveTopic.OnReceiveActivity(context);
                return(Task.CompletedTask);
            }

            // next feed back prompt
            this.SetActiveTopic(FEEDBACK_PROMPT)
            .OnReceiveActivity(context);
            context.SendActivity($"{HasActiveTopic}");
            return(Task.CompletedTask);
        }
Esempio n. 9
0
        public static void ShowAlarms(IBotContext context, List <Alarm> alarms)
        {
            if ((alarms == null) || (alarms.Count == 0))
            {
                context.SendActivity("You have no alarms.");
                return;
            }

            if (alarms.Count == 1)
            {
                context.SendActivity($"You have one alarm named '{ alarms[0].Title }', set for '{ alarms[0].Time }'.");
                return;
            }

            string message = $"You have { alarms.Count } alarms: \n\n";

            foreach (var alarm in alarms)
            {
                message += $"'{ alarm.Title }' set for '{ alarm.Time }' \n\n";
            }

            context.SendActivity(message);
        }
Esempio n. 10
0
        private async Task <RecognizerResult> playLuis(IBotContext botContext, string utterance)
        {
            await botContext.SendActivity("Start LUIS");


            // finder
            var luisModel = new LuisModel("", "", new System.Uri(""));
            // feedback
            //var luisModel = new LuisModel("", "", new System.Uri(""));

            var luisRecognizer = new LuisRecognizer(luisModel);

            return(await luisRecognizer.Recognize(utterance, CancellationToken.None));
        }
Esempio n. 11
0
        public override Task OnReceiveActivity(IBotContext context)
        {
            if (HasActiveTopic)
            {
                ActiveTopic.OnReceiveActivity(context);
                return(Task.CompletedTask);
            }

            // If there are no alarms to delete...
            if (this.State.Alarms.Count == 0)
            {
                context.SendActivity("There are no alarms to delete.");
                return(Task.CompletedTask);
            }

            if (this.State.AlarmIndex == null)
            {
                // If there is only one alarm to delete, use that index. No need to prompt.
                if (this.State.Alarms.Count == 1)
                {
                    AlarmsView.ShowAlarms(context, this.State.Alarms);

                    this.State.AlarmIndex = 0;
                }
                else
                {
                    this.SetActiveTopic(WHICH_ALARM_PROMPT)
                    .OnReceiveActivity(context);
                    return(Task.CompletedTask);
                }
            }

            this.State.Alarm.Title = this.State.Alarms[(int)this.State.AlarmIndex].Title;

            if (this.State.DeleteConfirmed == null)
            {
                this.SetActiveTopic(CONFIRM_DELETE_PROMPT)
                .OnReceiveActivity(context);
                return(Task.CompletedTask);
            }

            this.OnSuccess(context, new DeleteAlarmTopicValue
            {
                Alarm           = this.State.Alarm,
                AlarmIndex      = (int)this.State.AlarmIndex,
                DeleteConfirmed = (bool)this.State.DeleteConfirmed
            });
            return(Task.CompletedTask);
        }
Esempio n. 12
0
        public override Task OnReceiveActivity(IBotContext context)
        {
            if (context.Request.Type == ActivityTypes.Message)
            {
                // Check to see if there is an active topic.


                // If you don't have the state you need, prompt for it.


                // Now that you have the state you need (age and name), use it!
                context.SendActivity($"Hello { this.State.Name }! You are { this.State.Age } years old.");
                return(Task.CompletedTask);
            }

            return(Task.CompletedTask);
        }
Esempio n. 13
0
        public async override Task OnReceiveActivity(IBotContext context)
        {
            // IMAGE
            var image = context.Get <ImageRecognizeResult>(ImageMiddleware.ImageRecognizerResultKey);

            if (image != null)
            {
                await context.SendActivity("Thaks for sending me a photo!\nLet's see...");

                var keyword = image.PrimaryKeyword();
                var finder  = new Finder();
                var result  = await finder.SearchWithKeywordAsync(keyword);

                if (result != null)
                {
                    SearcherFeedbackState = true;
                    var activity = createReply(context, result);
                    await context.SendActivity(activity);

                    await context.SendActivity("Did you find what you ware looking for?");

                    return;
                }
            }

            if ((context.Request.Type == ActivityTypes.Message) && (context.Request.AsMessageActivity().Text.Length > 0))
            {
                var message  = context.Request.AsMessageActivity();
                var qnamaker = new QnaMaker();
                // If there is an active topic, let it handle this turn until it completes.
                if (HasActiveTopic)
                {
                    await ActiveTopic.OnReceiveActivity(context);

                    return;
                }
                if (!SearcherFeedbackState)
                {
                    await context.SendActivity("Got it!");
                }

                // CHIT
                var chitchater = new ChitChater();
                var answer     = await chitchater.SearchChitChat(message.Text);

                if (answer != null)
                {
                    await context.SendActivity(answer);

                    return;
                }

                // Feedback
                if (SearcherFeedbackState)
                {
                    SearcherFeedbackState = false;
                    var feedbacker = new Feedbacker();
                    var feedback   = await feedbacker.SearchAsync(message.Text);

                    if (feedback == Feedbacker.INTENT.FEEDBACK_NEGATIVE)
                    {
                        await context.SendActivity("Sorry, but could you try agein using another term?");

                        return;
                    }
                    else if (feedback == Feedbacker.INTENT.FEEDBACK_POSITIVE)
                    {
                        await context.SendActivity("No problem!");

                        return;
                    }
                    // Not reterun and continue next line when you get NOEN intent.
                }

                // SelectQuestion
                if (SelectQuestionState)
                {
                    if (int.TryParse(message.Text, out i) && (i < 4))
                    {
                        var selectquestion = questionlist[i];
                        var selectanswer   = await qnamaker.SearchQnaMaker(selectquestion);

                        await context.SendActivity(selectanswer.First().Answer);

                        SelectQuestionState   = false;
                        SearcherFeedbackState = true;
                        return;
                    }
                    else if (turn < maxturn)
                    {
                        await context.SendActivity("Sorry,but please input number(1 - 4)");

                        turn += 1;
                        return;
                    }
                    else
                    {
                        SelectQuestionState = false;
                        await context.SendActivity("too many attempts");

                        await context.SendActivity("OK! You may change your mind.");

                        return;
                    }
                }


                // QnA
                qnamaker = new QnaMaker();
                var queryresults = await qnamaker.SearchQnaMaker(message.Text);

                if (queryresults != null)
                {
                    if (queryresults.First().Questions.Count() == 1)
                    {
                        SearcherFeedbackState = true;
                        await context.SendActivity(queryresults.First().Answer);

                        return;
                    }
                    else
                    {
                        SelectQuestionState   = true;
                        SearcherFeedbackState = true;
                        var messages = "Did you mean? Please input number(1 - 4)";
                        foreach (var q in queryresults.First().Questions.Select((value, index) => new { value, index }))
                        {
                            if (q.index > 2)
                            {
                                messages += "\n\n" + "\n\n" + (q.index + 1) + ".None of adove";
                                questionlist.Add(queryresults.First().Questions[q.index]);
                                break;
                            }
                            messages += "\n\n" + "\n\n" + (q.index + 1) + "." + queryresults.First().Questions[q.index].ToString();
                            questionlist.Add(queryresults.First().Questions[q.index]);
                        }
                        await context.SendActivity(messages);

                        return;
                    }
                }

                // Search
                var finder = new Finder();
                var result = await finder.SearchAsync(message.Text);

                if (result != null)
                {
                    SearcherFeedbackState = true;
                    var activity = createReply(context, result);
                    await context.SendActivity(activity);

                    await context.SendActivity("Did you find what you ware looking for?");

                    return;
                }
                await context.SendActivity("Sorry, but I didn't understand that. Could you try saying that another way?");
            }
        }
Esempio n. 14
0
 private void ShowDefaultMessage(IBotContext context)
 {
     context.SendActivity("'Show Alarms', 'Add Alarm', 'Delete Alarm', 'Help'.");
 }
Esempio n. 15
0
 private void ShowDefaultMessage(IBotContext context)
 {
     context.SendActivity("'Add Alarm'.");
 }
Esempio n. 16
0
        public override async Task OnReceiveActivity(IBotContext context)
        {
            if (HasActiveTopic)
            {
                await ActiveTopic.OnReceiveActivity(context);

                return;
            }

            var utterance = context.Request.AsMessageActivity().Text;


            if (this.State.Alarm.Title == null)
            {
                // LUIS実行
                await context.SendActivity("got it!");

                var luisResult = await playLuis(context, utterance);


                // TODO: LUISの戻り値に応じて呼び出すAPIを変更する
                var intent         = luisResult.Intents.GetValue("Places.FindPlace");
                var entity         = luisResult.Entities.GetValue("Places_AbsoluteLocation");
                var entity_keyword = entity.First().ToString();
                await context.SendActivity(entity_keyword.ToString());

                /*
                 * if (this.State.Alarm.Title == "Searched") {
                 *  this.SetActiveTopic(SEARCH_PROMPT)
                 *      .OnReceiveActivity(context);
                 *  return Task.CompletedTask;
                 *
                 * }
                 *
                 */
                // LUISの結果でスポット検索
                var service = new Services.SpotSearchService();
                var req     = new SpotsRequest();
                req.keyword = entity_keyword;
                var result = await service.Search(req) as SpotsResult;

                await context.SendActivity(result.spots.First().name);

                // Replyを作成し表示
                var reply = context.Request.CreateReply();
                reply.Attachments = result.Attachments;
                await context.SendActivity(reply);

                // next feed back prompt
                await this.SetActiveTopic(FEEDBACK_PROMPT)
                .OnReceiveActivity(context);

                await context.SendActivity($"{HasActiveTopic}");


                return;
            }

            /*
             * if (this.State.Alarm.Title == null) {// spotの場合
             *  await context.SendActivity("スポット表示");
             *  this.State.Alarm.Title = "Searched";
             *
             *  // APIの戻り値表示
             *
             *  // next feed back prompt
             *  await this.SetActiveTopic(FEEDBACK_PROMPT)
             *      .OnReceiveActivity(context);
             * }
             * else {
             *  await context.SendActivity("スポット表示失敗");
             * }
             * await context.SendActivity("すみません、お役に立てなくて");
             * this.OnSuccess(context, null);
             *
             */
            await context.SendActivity("ここまでくれば終了で抜けます");

            //this.OnSuccess(context, this.State.Alarm);
        }
Esempio n. 17
0
        public async Task OnProcessRequest(IBotContext context, MiddlewareSet.NextDelegate next)
        {
            var activity = context.Request.AsMessageActivity();

            if (activity == null)
            {
                await next();
            }

            // Handle the message from functions contains ChatPlus's webhook response
            if (activity?.Type == ActivityTypes.Event)
            {
                Debug.WriteLine("*************************");
                Debug.WriteLine("Got event type message");
                Debug.WriteLine("*************************");
                string userId = "default-user"; // TODO hiroaki-honda remove this line and replace userId used as key to extract ConversationInformation from table storage. ("default-user" is the userId just for Hackfest).
                // string userId = Deserialize<Visitor>("visitor", (Activity)activity).visitor_id;
                ConversationReference conversationReference = await GetConversationReferenceByUserId(userId);

                switch (activity.From.Id)
                {
                case "WebhookStartChat":
                    string messageForSuccessToConnect = "Success to make a connection with call center agent. Please let us know what makes you in trouble.";
                    await SendProactiveMessage(context, conversationReference, messageForSuccessToConnect);

                    break;

                case "WebhookSendMessage":
                    string messageFromAgent = JsonConvert.DeserializeObject <ChatPlusInformation>(activity.Value.ToString()).message.text;
                    await SendProactiveMessage(context, conversationReference, messageFromAgent);

                    break;

                default:
                    throw new Exception("unexpected event type message");
                }
            }

            // Enqueue the message to hook the function which passes the message to agent if "IsConnectedToAgent" is true.
            var userState = context.GetUserState <BotUserState>();

            if (userState != null && userState.IsConnectedToAgent)
            {
                CloudStorageAccount account          = buildStorageAccount();
                CloudQueueClient    cloudQueueClient = account.CreateCloudQueueClient();
                CloudQueue          queue            = cloudQueueClient.GetQueueReference("message-from-user");
                var item = new ConversationInformation()
                {
                    ConversationReference = JsonConvert.SerializeObject(GetConversationReference((Microsoft.Bot.Schema.Activity)activity)),
                    MessageFromUser       = context.Request.Text
                };
                var message = new CloudQueueMessage(JsonConvert.SerializeObject(item));
                await queue.AddMessageAsync(message);

                return;
            }

            // Request to make a connection between user and agent
            if (activity != null &&
                !string.IsNullOrEmpty(activity.Text) &&
                activity.Text.ToLower().Contains(Commands.CommandRequestConnection))
            {
                // Store conversation reference (Use this info when send a proactive message to user after).
                await StoreConversationInformation(context);

                // TODO hiroaki-honda Implement the logic to hook the function which request connection to ChatPlus
                // Status: Ask chatplus to prepare the API which receive the request to connect to agent

                // Set connecting state true
                var state = context.GetUserState <BotUserState>();
                state.IsConnectedToAgent = true;

                await context.SendActivity("Now make a request for connection. Please wait a minutes.");
            }
            else
            {
                await next();
            }
        }
Esempio n. 18
0
        public RootTopic(IBotContext context) : base(context)
        {
            // User state initialization should be done once in the welcome
            //  new user feature. Placing it here until that feature is added.
            if (context.GetUserState <BotUserState>().Alarms == null)
            {
                context.GetUserState <BotUserState>().Alarms = new List <Alarm>();
            }

            this.SubTopics.Add(ADD_ALARM_TOPIC, (object[] args) =>
            {
                var addAlarmTopic = new AddAlarmTopic();

                addAlarmTopic.Set
                .OnSuccess((ctx, alarm) =>
                {
                    this.ClearActiveTopic();

                    ctx.GetUserState <BotUserState>().Alarms.Add(alarm);

                    context.SendActivity($"Added alarm named '{ alarm.Title }' set for '{ alarm.Time }'.");
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.SendActivity("Let's try something else.");

                    this.ShowDefaultMessage(ctx);
                });

                return(addAlarmTopic);
            });

            this.SubTopics.Add(DELETE_ALARM_TOPIC, (object[] args) =>
            {
                var alarms = (args.Length > 0) ? (List <Alarm>)args[0] : null;

                var deleteAlarmTopic = new DeleteAlarmTopic(alarms);

                deleteAlarmTopic.Set
                .OnSuccess((ctx, value) =>
                {
                    this.ClearActiveTopic();

                    if (!value.DeleteConfirmed)
                    {
                        context.SendActivity($"Ok, I won't delete alarm '{ value.Alarm.Title }'.");
                        return;
                    }

                    ctx.GetUserState <BotUserState>().Alarms.RemoveAt(value.AlarmIndex);

                    context.SendActivity($"Done. I've deleted alarm '{ value.Alarm.Title }'.");
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.SendActivity("Let's try something else.");

                    this.ShowDefaultMessage(context);
                });

                return(deleteAlarmTopic);
            });
        }