Ejemplo n.º 1
0
        public ResponseModel SendMessage(MessageRequestModel message)
        {
            var _sessionid = _distributedCache.GetString(message.UserId);

            if (string.IsNullOrWhiteSpace(_sessionid))
            {
                _sessionid = _assistantService.CreateSession(_assistantId).Result.SessionId;

                _distributedCache.SetString(message.UserId, _sessionid, new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(00, 4, 00)
                });
            }

            var messageObj = new IBM.Watson.Assistant.v2.Model.MessageInput()
            {
                Text = message.Text
            };

            MessageContextSkills skills = new MessageContextSkills();
            MessageContextSkill  skill  = new MessageContextSkill
            {
                UserDefined = new Dictionary <string, object>()
            };

            if (message.ContextVariables != null)
            {
                foreach (var item in message.ContextVariables)
                {
                    skill.UserDefined.Add(item.Key, item.Value);
                }
            }

            skills.Add("main skill", skill);

            var MessageContext = new IBM.Watson.Assistant.v2.Model.MessageContext()
            {
                Global = new MessageContextGlobal()
                {
                    System = new MessageContextGlobalSystem()
                    {
                        UserId   = message.UserId,
                        Timezone = "GMT3"
                    }
                },
                Skills = skills
            };

            var result = _assistantService.Message(_assistantId, _sessionid, messageObj, MessageContext);

            var response = Newtonsoft.Json.JsonConvert.DeserializeObject <ResponseModel>(result.Response);

            return(response);
        }
Ejemplo n.º 2
0
        public Response SendMessage(string message, string userId)
        {
            var _sessionid = _distributedCache.GetString(userId);

            if (string.IsNullOrWhiteSpace(_sessionid))
            {
                _sessionid = _assistantService.CreateSession(_assistantId).Result.SessionId;

                _distributedCache.SetString(userId, _sessionid, new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(00, 10, 00)
                });
            }
            //var sessionid = _assistantService.CreateSession(_assistantId).Result.SessionId;

            var messageObj = new IBM.Watson.Assistant.v2.Model.MessageInput()
            {
                Text = message
            };

            MessageContextSkills skills = new MessageContextSkills();
            MessageContextSkill  skill  = new MessageContextSkill();

            skill.UserDefined = new Dictionary <string, object>();

            skills.Add("main skill", skill);

            var MessageContext = new IBM.Watson.Assistant.v2.Model.MessageContext()
            {
                Global = new MessageContextGlobal()
                {
                    System = new MessageContextGlobalSystem()
                    {
                        UserId   = userId,
                        Timezone = "GMT3"
                    }
                },
                Skills = skills
            };

            var result = _assistantService.Message(_assistantId, _sessionid, messageObj, MessageContext);

            var response = JsonConvert.DeserializeObject <Response>(result.Response);

            //_assistantService.DeleteSession(_assistantId, sessionid);
            return(response);
        }