コード例 #1
0
        //----------------
        private async Task <HttpResponseMessage> ListAllCars(int capacity, string currentBaseURL, Activity activity, ConnectorClient connector)
        //private async Task<bool> ListAllCars(int capacity, string currentBaseURL, Activity activity, ConnectorClient connector)
        {
            // Instantiate the BotData dbContext
            Activity replyToConversation = activity.CreateReply("Vincent Cars");

            replyToConversation.Recipient   = activity.From;
            replyToConversation.Type        = "message";
            replyToConversation.Attachments = new List <Attachment>();
            if (1 == 1)
            {
                CombinedBotDBEntities DB      = new CombinedBotDBEntities();
                List <Vehicle>        carList = DB.Vehicles.ToList();
                foreach (Vehicle car in carList)
                {
                    if (car.Capacity >= capacity)
                    {
                        List <CardImage> cardImages = new List <CardImage>();
                        string           strCarImg  = String.Format(@"{0}/{1}", currentBaseURL, car.Image);
                        cardImages.Add(new CardImage(url: strCarImg));
                        List <CardAction> cardButtons = new List <CardAction>();
                        CardAction        plButton    = new CardAction()
                        {
                            //Value = "http://msa.ms",
                            //Type = "openUrl",
                            //Title = "MSA Website"
                            Type  = "imBack",
                            Title = car.VehicleName.ToString(),
                            Value = car.Id.ToString()
                        };
                        cardButtons.Add(plButton);
                        //List<CardAction> cardButtons = CreateButtons();
                        // Create the Hero Card
                        // Set the image and the buttons
                        //ThumbnailCard plCard = new ThumbnailCard()
                        HeroCard plCard = new HeroCard()
                        {
                            Title    = car.VehicleName,
                            Subtitle = car.Description + ", " + car.Capacity.ToString() + " seats, $" + car.Price.ToString() + "NZD/one day",
                            Images   = cardImages,
                            Buttons  = cardButtons
                        };
                        Attachment plAttachment = plCard.ToAttachment();
                        replyToConversation.Attachments.Add(plAttachment);
                    }
                }
            }
            await connector.Conversations.SendToConversationAsync(replyToConversation);

            //Request.CreateResponse(HttpStatusCode.OK);
            return(Request.CreateResponse(HttpStatusCode.OK));
            //await connector.Conversations.ReplyToActivityAsync(replyToConversation);
            //return true;
        }
コード例 #2
0
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            //------------- added 22 Nov 2016
            #region Set CurrentBaseURL and ChannelAccount
            // Get the base URL that this service is running at
            // This is used to show images
            string currentBaseURL =
                this.Url.Request.RequestUri.AbsoluteUri.Replace(@"api/messages", "");

            // Create an instance of BotData to store data
            BotData objBotData = new BotData();

            // Instantiate a StateClient to save BotData
            StateClient stateClient = activity.GetStateClient();

            // Use stateClient to get current userData
            BotData userData = await stateClient.BotState.GetUserDataAsync(
                activity.ChannelId, activity.From.Id);

            // Update userData by setting CurrentBaseURL and Recipient
            //userData.SetProperty<string>("CurrentBaseURL", currentBaseURL);

            // Save changes to userData
            //await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
            #endregion
            //------------- end of added 22 Nov
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                var userMessage = activity.Text;

                //StateClient stateClient = activity.GetStateClient();
                //BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
                // *************************
                // Log to Database
                // *************************
                // Instantiate the BotData dbContext
                CombinedBotDBEntities DB = new CombinedBotDBEntities();
                // Create a new MessageLog object
                MessageLog NewMsgLog = new MessageLog();
                // Set the properties on the MessageLog object
                NewMsgLog.Channel  = activity.ChannelId;
                NewMsgLog.UserID   = activity.From.Id;
                NewMsgLog.UserName = activity.From.Name;
                NewMsgLog.Created  = DateTime.UtcNow;
                NewMsgLog.Message  = activity.Text.Truncate(500);
                // Add the MessageLog object to MessageLogs
                DB.MessageLogs.Add(NewMsgLog);
                // Save the changes to the database
                DB.SaveChanges();
                //----------------
                string endOutput          = "Hi";
                bool   textAnswer         = true;
                bool   needtoSaveUserData = false;
                bool   sentGreeting       = userData.GetProperty <bool>("SentGreeting");
                int    capacity           = userData.GetProperty <int>("Capacity");
                string hiringDay          = userData.GetProperty <string>("HiringDay");
                string customerAdd        = userData.GetProperty <string>("CustomerAdd");
                int    selectedCarId      = userData.GetProperty <int>("SelectedCarId");
                bool   resetFlag          = false;
                //-------- local variables
                int    carPrice = 0;
                string carName  = "";
                //----------------
                if (userMessage.ToLower().Contains("reset"))
                {
                    sentGreeting  = false;
                    capacity      = 0;
                    hiringDay     = "";
                    customerAdd   = "";
                    selectedCarId = 0;
                    //userData.SetProperty<bool>("SentGreeting", sentGreeting);
                    userData.SetProperty <int>("Capacity", capacity);
                    userData.SetProperty <string>("HiringDay", hiringDay);
                    userData.SetProperty <string>("CustomerAdd", customerAdd);
                    userData.SetProperty <int>("SelectedCarId", selectedCarId);
                    //endOutput = "The conversation has just been restarted from beggining.";
                    needtoSaveUserData = true;
                    resetFlag          = true;
                }
                //sentGreeting = false;
                //resetFlag = true;   // for debugging on server
                if (!resetFlag)
                {
                    //---- try to detect several basic input commands first
                    if (capacity <= 0)
                    {
                        bool result = Int32.TryParse(userMessage, out capacity);
                        if (result)
                        {
                            userData.SetProperty <int>("Capacity", capacity);
                            needtoSaveUserData = true;
                        }
                    }
                    else if (capacity > 0 && selectedCarId <= 0)
                    {
                        bool result = Int32.TryParse(userMessage, out selectedCarId);
                        if (result)
                        {
                            userData.SetProperty <int>("SelectedCarId", selectedCarId);
                            needtoSaveUserData = true;
                        }
                    }
                }
                if (!sentGreeting)
                {
                    sentGreeting = true;
                    endOutput    = "Hello. What can I help you with?";
                    userData.SetProperty <bool>("SentGreeting", sentGreeting);
                    needtoSaveUserData = true;
                    //await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
                }
                else //if (1==2)
                {
                    if (userMessage.ToLower().Contains("hire"))
                    {
                        endOutput = "Great! Thanks for choosing our service.";
                    }
                    if (capacity <= 0)
                    {
                        endOutput += " You need a car for how many people?";
                    }
                    if (capacity > 0 && selectedCarId <= 0)
                    {
                        endOutput = "You need " + capacity.ToString() + " seats. We have above vehicles that match your requirements, please select one.";
                        //list cars here
                        HttpResponseMessage x = await ListAllCars(capacity, currentBaseURL, activity, connector);

                        //bool x = await ListAllCars(capacity, currentBaseURL, activity, connector);
                    }
                    if (selectedCarId > 0)
                    {
                        bool carFound = Common.GetCarInfo(selectedCarId, ref carPrice, ref carName);
                        if (carFound)
                        {
                            endOutput = "You selected the car " + carName + " (ID=" + selectedCarId.ToString() + "). A very smart choice! The hiring fee is $" + carPrice + "NZD per day";
                        }
                        else
                        {
                            endOutput = "Your selected car cannot be found. Please type reset to restart again.";
                        }
                    }
                    Luis question = await GetEntityFromLUIS(userMessage);

                    //endOutput = question.intent;
                    if (question.intent == "currencyconvert" && carPrice > 0)
                    {
                        string convCarPrice = await GetExchange(question.entity, carPrice);

                        endOutput = convCarPrice;
                    }
                    if (question.intent == "carbranch" && (customerAdd == null || customerAdd == "")) //&& question.intent != "customeradd")
                    {
                        endOutput = "We have two branches in the city. Where are you living now?";
                    }
                    if (question.intent == "customeradd" && (customerAdd == null || customerAdd == ""))
                    {
                        customerAdd = userMessage;
                        // send recommendation about branch address
                        endOutput = await BranchRecommendation(customerAdd);
                    }
                }
                if (textAnswer)
                {
                    if (needtoSaveUserData)
                    {
                        await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
                    }
                    // *************************
                    // Log to Database
                    // *************************
                    //endOutput= endOutput+ "-" + activity.ChannelId.ToString() + "-" + activity.From.Id + "-" + activity.From.Name;
                    // return our reply to the user
                    Activity infoReply = activity.CreateReply(endOutput);
                    // Set the properties on the MessageLog object
                    //NewMsgLog.Channel = infoReply.ChannelId;  // This part doesn't work, need to be checked
                    //NewMsgLog.UserID = infoReply.From.Id;
                    //NewMsgLog.UserName = infoReply.From.Name;
                    NewMsgLog.Channel  = activity.ChannelId;
                    NewMsgLog.UserID   = activity.From.Id;
                    NewMsgLog.UserName = activity.From.Name;
                    NewMsgLog.Created  = DateTime.UtcNow;
                    NewMsgLog.Message  = infoReply.Text.Truncate(500);
                    // Add the MessageLog object to MessageLogs
                    DB.MessageLogs.Add(NewMsgLog);
                    // Save the changes to the database
                    DB.SaveChanges();
                    await connector.Conversations.ReplyToActivityAsync(infoReply);
                }
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return(response);
        }