/// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            IdentifyLanguage();


            if (activity.Type == ActivityTypes.Message)
            {
                if (activity.Text.Contains("CANCELAR") || activity.Text.Contains("CANCEL"))
                {
                    IdentifyLanguage();
                    ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                    string          response1 = ChatResponse.Cancel;
                    string          response2 = ChatResponse.Step5Else;
                    Activity        reply     = activity.CreateReply(response1);
                    Activity        reply2    = activity.CreateReply(response2);
                    await connector.Conversations.ReplyToActivityAsync(reply);

                    await connector.Conversations.ReplyToActivityAsync(reply2);

                    //Reinicia la conversación
                    activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id);
                }

                else
                {
                    activity.Text = TranslatorHandler.DetectAndTranslate(activity);
                    await Conversation.SendAsync(activity, MakeRoot);
                }
            }
            else
            {
                await HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }
Beispiel #2
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            IdentifyLanguage();
            #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

            if (activity.Type == ActivityTypes.Message)
            {
                IMessageActivity mensaje = activity.AsMessageActivity();

                Activity msj = (Activity)mensaje;
                msj.Recipient = msj.Recipient;
                msj.Type      = "Message";


                ConnectionDB.Chatbot_PGBEntities1 DBTop         = new ConnectionDB.Chatbot_PGBEntities1();
                ConnectionDB.UserLogin            NewUserLogTop = new ConnectionDB.UserLogin();
                NewUserLogTop.Channel  = msj.ChannelId;
                NewUserLogTop.UserID   = msj.From.Id;
                NewUserLogTop.UserName = msj.From.Name;
                NewUserLogTop.Created  = DateTime.UtcNow;
                NewUserLogTop.Message  = msj.Text;
                DBTop.UserLogins.Add(NewUserLogTop);
                DBTop.SaveChanges();
                //DBTop.Dispose();

                if (activity.Text.Contains("CANCELAR") || activity.Text.Contains("CANCEL"))
                {
                    IdentifyLanguage();
                    ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                    string          response1 = ChatResponse.Cancel;
                    string          response2 = ChatResponse.Step5Else;
                    Activity        reply     = activity.CreateReply(response1);
                    Activity        reply2    = activity.CreateReply(response2);
                    await connector.Conversations.ReplyToActivityAsync(reply);

                    await connector.Conversations.ReplyToActivityAsync(reply2);

                    //Reinicia la conversación
                    activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id);
                }
                //else
                //{
                //    if (activity.Text.Contains("Adiós") || activity.Text.Contains("Adios"))
                //    {
                //        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                //        string response1 = ChatResponse.Goodbye;
                //        //string response2 = ChatResponse.Step5Else;
                //        Activity reply = activity.CreateReply(response1);
                //        //Activity reply2 = activity.CreateReply(response2);
                //        await connector.Conversations.ReplyToActivityAsync(reply);
                //        //await connector.Conversations.ReplyToActivityAsync(reply2);
                //        //Reinicia la conversación
                //        activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id);
                //    }
                //}
                else
                {
                    activity.Text = TranslatorHandler.DetectAndTranslate(activity);
                    await Conversation.SendAsync(activity, MakeRoot);
                }
            }
            else
            {
                await HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return(response);
        }