public static string ButtonListConstructor(ButtonListTemplate buttons)
        {
            string model = string.Empty;

            foreach (var item in buttons.Buttons)
            {
                model = model + "<a class='btn btn-primary chatButton' href=" + item.HrefLink + ">" + item.Text + "</a>";
            }

            return(model);
        }
Example #2
0
        public async Task <JsonResult> MessageChatAsync(string msg, bool isInit, bool isValid, string actionPayload)
        {
            try
            {
                string _attachment = null;

                if (_credentials == null)
                {
                    //IT-ORVAL=>Test-aje-jg->Aje group Paris
                    _credentials = new WatsonCredentials()
                    {
                        workspaceID = "ac6889fe-7f09-4f71-ac0e-4d8850b72d2f",
                        username    = "******",
                        password    = "******"
                    };
                }

                if (isInit)
                {
                    context = new Context();
                }

                if (!string.IsNullOrEmpty(actionPayload))
                {
                    if (context == null)
                    {
                        context = new Context();
                    }

                    context.Action = actionPayload;
                }

                if (context != null)
                {
                    context.Valid = isValid;

                    switch (context.Action)
                    {
                    case "emailToValidate":

                        if (!context.Valid)
                        {
                            break;
                        }

                        var userEmailToValidate = await _userManager.FindByEmailAsync(msg);

                        if (userEmailToValidate?.EmailConfirmed == false || userEmailToValidate == null)
                        {
                            context.Valid = false;
                            break;
                        }

                        MyGoogleUserInfo _userinfo = GetGoogleUserInfo(msg);

                        //if (!GoogleUser.IsEmailRegistered(msg))
                        //{
                        //    context.Valid = false;
                        //}

                        if (_userinfo == null || _userinfo.Suspended == true)
                        {
                            context.Valid = false;
                            //context = null;
                        }
                        else
                        {
                            context.UserName = _userinfo.GivenName;
                            context.Email    = msg;
                            context.Valid    = true;

                            //_attachment = "<a class='btn btn-default' href=javascript:getGoogleUserInfo('" +
                            //  msg + "');>Ver datos</a>";
                        }

                        break;

                    case "secretToValidate":

                        if (string.IsNullOrEmpty(context.Email))
                        {
                            break;
                        }

                        var user = await _userManager.FindByEmailAsync(context.Email);

                        var _decrypt = Helpers.Helpers.DecryptString(user?.SecretResponse, _keyEncode);

                        //Default if not encrypted secret response previously
                        if (string.IsNullOrEmpty(_decrypt))
                        {
                            _decrypt = user?.SecretResponse;
                        }

                        if (msg == _decrypt)
                        {
                            context.Valid = true;
                            //context.Action = null;
                        }
                        else
                        {
                            context.Valid = false;
                            //context.Action = "emailToValidate";
                        }

                        break;

                    case "confirmationToValidate":
                        if (context.Valid == true && context.Password != null)
                        {
                            string goog = RunPasswordReset(context.Email, context.Password);

                            if (goog.Contains("Error") || goog.Contains("forbidden"))
                            {
                                context.Action   = null;
                                context.Valid    = false;
                                context.Password = string.Empty;


                                if (goog.Contains("forbidden"))
                                {
                                    context.Error = "No tengo los privilegios suficientes para modificar la contraseña de un Administrador";
                                }
                                else
                                {
                                    context.Error = "Ha sucedido un error al momento de intentar actualizar la contraseña";
                                }


                                //if (goog.Contains("forbidden"))
                                //{
                                //	goog = "No tengo los privilegios suficientes para modificar la contraseña de un Administrador";
                                //}
                                //else
                                //{
                                //	goog = "Ha sucedido un error al momento de intentar actualizar la contraseña";
                                //}

                                //MessageRequest _msgGoog = new MessageRequest()
                                //{
                                //	Output = new OutputData()
                                //	{
                                //		Text = new List<string>()
                                //		{
                                //			goog
                                //		}
                                //	},
                                //	Context = context
                                //};

                                //return Json(JsonConvert.SerializeObject(_msgGoog));
                            }
                        }

                        break;

                    case "ListPerfiles":
                        List <string> _listPerfiles = await SOAPservice.GetListPerfilesAsync();

                        context.Action = null;

                        MessageRequest _message = new MessageRequest()
                        {
                            Output = new OutputData()
                            {
                                Text = _listPerfiles
                            },
                            Context = context
                        };

                        var obj = JsonConvert.SerializeObject(_message);

                        return(Json(obj));

                    case "AddServiceCall":

                        //Test
                        //if (string.IsNullOrEmpty(context.Email))
                        //{
                        //    context.Email = "*****@*****.**";
                        //}

                        if (string.IsNullOrEmpty(context.Email))
                        {
                            context.Email = "*****@*****.**";
                        }


                        SOAPservice.ArandaUser _user = await SOAPservice.GetArandaUserInfo(context.Email);

                        SOAPservice.ArandaTicket ticket = await SOAPservice.SetArandaNewTicketAsync(_user);

                        string msgTicket;

                        if (string.IsNullOrEmpty(ticket.TicketNumber))
                        {
                            msgTicket = "Correo electrónico no registrado en Aranda. No se pudo crear ticket.";
                        }
                        else
                        {
                            msgTicket = string.Format("El ticket {0} ha sido creado con exito!", ticket.TicketNumber);
                        }

                        context.Action = null;

                        MessageRequest _msgAddTicket = new MessageRequest()
                        {
                            Output = new OutputData()
                            {
                                Text = new List <string>()
                                {
                                    msgTicket
                                }
                            },
                            Context = context
                        };

                        var objAdTicket = JsonConvert.SerializeObject(_msgAddTicket);

                        return(Json(objAdTicket));

                    default:
                        break;
                    }
                }


                MessageRequest result = Message(msg, context, _credentials);

                context.Error = null;

                if (result.Intents != null)
                {
                    string myIntent = result.Intents[0].IntentDescription;
                    string myAction = context?.Action;

                    switch (myIntent)
                    {
                    case "clima":
                        string _forecast = await CallWeatherAsync(null, null);

                        if (!string.IsNullOrEmpty(_forecast))
                        {
                            result.Output.Text.Add(_forecast);
                        }

                        break;

                    case "menu":
                        ButtonListTemplate _menu = new ButtonListTemplate()
                        {
                            Buttons = new List <ButtonTemplate>()
                            {
                                new ButtonTemplate()
                                {
                                    HrefLink = "javascript:sendRequest(false,'ListPerfiles',true);", Text = "Listado de Perfiles"
                                },
                                new ButtonTemplate()
                                {
                                    HrefLink = "javascript:sendRequest(false,'AddServiceCall',true);", Text = "Crear Ticket"
                                },
                            }
                        };

                        _attachment = ButtonListConstructor(_menu);

                        break;

                    case "productos":

                        _attachment = CarouselConstructor(GetCarouselList());

                        break;

                    default:
                        break;
                    }
                }


                context = result.Context;

                switch (context.Action)
                {
                case "secretToValidate":

                    context.Valid = false;

                    if (string.IsNullOrEmpty(context.Email) || !IsValidEmail(context.Email))
                    {
                        break;
                    }

                    var user = await _userManager.FindByEmailAsync(context.Email);

                    var _decrypt = Helpers.Helpers.DecryptString(user?.SecretResponse, _keyEncode);

                    //Default if not encrypted secret response previously
                    if (string.IsNullOrEmpty(_decrypt))
                    {
                        _decrypt = user?.SecretResponse;
                    }

                    if (msg == _decrypt)
                    {
                        context.Valid = true;
                        break;
                    }

                    if (user != null)
                    {
                        result.Output.Text.Add(user.SecretQuestion);
                    }
                    else
                    {
                        result.Output = new OutputData()
                        {
                            Text = new List <string>()
                            {
                                "Debe registrarse primero en la aplicación"
                            }
                        };

                        //context.Valid = false;
                        context = new Context();
                    }



                    break;

                case "success":

                    //_attachment = "<a class='btn btn-default' href=javascript:getGoogleUserInfo('" +
                    // context.Email + "');>Ver datos</a>";
                    //_attachment = _attachment + "<br />";
                    //_attachment = _attachment + "<a class='btn btn-default' href=javascript:getGoogleTokens('" +
                    //       context.Email + "');>Generar Tokens</a>";

                    SOAPservice.ArandaUser _user = await SOAPservice.GetArandaUserInfo(context.Email);

                    SOAPservice.ArandaTicket ticket = await SOAPservice.SetArandaNewTicketAsync(_user);

                    string msgTicket;

                    if (string.IsNullOrEmpty(ticket.TicketNumber))
                    {
                        msgTicket = "Correo electrónico no registrado en Aranda. No se pudo crear ticket.";
                    }
                    else
                    {
                        string _IdbyProject = await SOAPservice.GetArandaProjectTicketAsync(ticket.TicketNumber);

                        msgTicket = string.Format("El ticket {0} ha sido creado con exito!", _IdbyProject);
                    }

                    context = new Context();
                    result.Output.Text.Add(msgTicket);

                    break;

                default:
                    break;
                }


                //if (context.Action == "success")
                //{
                //    _attachment = "<a class='btn btn-default' href=javascript:getGoogleUserInfo('" +
                //          context.Email + "');>Ver datos</a>";
                //    _attachment = _attachment + "<br />";
                //    _attachment = _attachment + "<a class='btn btn-default' href=javascript:getGoogleTokens('" +
                //           context.Email + "');>Generar Tokens</a>";
                //}

                if (!string.IsNullOrEmpty(_attachment))
                {
                    result.Output.Text.Add(_attachment);
                }

                var json = JsonConvert.SerializeObject(result);

                return(Json(json));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return(null);
            }
        }