Example #1
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                LuisInfo luisInfo = await LuisMovieClient.ParseUserInput(activity.Text);

                string responseMessage;
                if (luisInfo.intents.Count() > 0)
                {
                    //intents[0] is the highest probability of intent.
                    switch (luisInfo.intents[0].intent)
                    {
                    case "GetMovieYear":
                        if (luisInfo.entities.Count() > 0)
                        {
                            responseMessage = await MovieUtilities.GetMovieYear(luisInfo.entities[0].entity);
                        }
                        else
                        {
                            responseMessage = "Sorry, I don't understand which movie you want.";
                        }
                        break;

                    case "GetMovieDirector":
                        if (luisInfo.entities.Count() > 0)
                        {
                            responseMessage = await MovieUtilities.GetMovieDirector(luisInfo.entities[0].entity);
                        }
                        else
                        {
                            responseMessage = "Sorry, I don't understand which movie you want.";
                        }
                        break;

                    default:
                        responseMessage = "Sorry, I don't know how to " + luisInfo.intents[0].intent;
                        break;
                    }
                }
                else
                {
                    responseMessage = "Sorry, I'm not sure what you want.";
                }

                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                // return our reply to the user
                Activity reply = activity.CreateReply(responseMessage);
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }
Example #2
0
        //A method "ParseUserInput" that will take the user input and parse it using Luis.
        public static async Task <LuisInfo> ParseUserInput(string strInput)
        {
            string strRet     = string.Empty;
            string strEscaped = Uri.EscapeDataString(strInput);

            using (var client = new HttpClient())
            {
                //URI is copied from our Luis Application.
                //Make sure to include "&q" at the end so that what the user types in (strEscaped) will be accepted as a query.
                string uri = "https://api.projectoxford.ai/luis/v1/application?id=27eb0729-4391-4880-a04f-dae911162fe1&subscription-key=a0794768387a459da34bab6f49878c1e&q=" + strEscaped;
                HttpResponseMessage msg = await client.GetAsync(uri);

                if (msg.IsSuccessStatusCode)
                {
                    string jsonResponse = await msg.Content.ReadAsStringAsync();

                    LuisInfo _Data = JsonConvert.DeserializeObject <LuisInfo>(jsonResponse);
                    return(_Data);
                }
            }
            return(null);
        }