Beispiel #1
0
        /// <summary>
        /// 执行NeuChar判断过程,获取响应消息
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <param name="messageHandler"></param>
        /// <param name="accessTokenOrApi"></param>
        /// <returns></returns>
        public IResponseMessageBase Execute(IRequestMessageBase requestMessage, IMessageHandlerEnlighten messageHandler, string accessTokenOrApi)
        {
            //SenparcTrace.SendCustomLog("neuchar trace", "2");

            if (accessTokenOrApi == null)
            {
                throw new ArgumentNullException(nameof(accessTokenOrApi));
            }

            IResponseMessageBase responseMessage = null;

            //SenparcTrace.SendCustomLog("neuchar trace", "3");

            switch (requestMessage.MsgType)
            {
            case RequestMsgType.Text:
            {
                try
                {
                    //SenparcTrace.SendCustomLog("neuchar trace", "3.1");

                    var textRequestMessage = requestMessage as IRequestMessageText;
                    //遍历所有的消息设置
                    foreach (var messagePair in Config.MessagePair.Where(z => z.Request.Type == RequestMsgType.Text))
                    {
                        //遍历每一个消息设置中的关键词
                        foreach (var keyword in messagePair.Request.Keywords)
                        {
                            if (keyword.Equals(textRequestMessage.Content, StringComparison.OrdinalIgnoreCase))        //TODO:加入大小写敏感设计
                            {
                                responseMessage = GetResponseMessage(requestMessage, messagePair.Response, messageHandler.MessageEntityEnlighten);
                                ExecuteApi(messagePair, requestMessage, messageHandler.ApiEnlighten, accessTokenOrApi, requestMessage.FromUserName);
                                break;
                            }
                        }
                        if (responseMessage != null)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    SenparcTrace.SendCustomLog("neuchar text error", ex.Message + "\r\n|||\r\n" + (ex.InnerException != null ? ex.InnerException.ToString() : ""));
                }
            }
            break;

            case RequestMsgType.Image:
            {
                var imageRequestMessage = requestMessage as IRequestMessageImage;
                //遍历所有的消息设置

                foreach (var messagePair in Config.MessagePair.Where(z => z.Request.Type == RequestMsgType.Image))
                {
                    responseMessage = GetResponseMessage(requestMessage, messagePair.Response, messageHandler.MessageEntityEnlighten);
                    ExecuteApi(messagePair, requestMessage, messageHandler.ApiEnlighten, accessTokenOrApi, requestMessage.FromUserName);

                    if (responseMessage != null)
                    {
                        break;
                    }
                }
            }
            break;

            default:
                //不作处理

                //throw new UnknownRequestMsgTypeException("NeuChar未支持的的MsgType请求类型:"+ requestMessage.MsgType, null);
                break;
            }
            //SenparcTrace.SendCustomLog("neuchar trace", "4");

            return(responseMessage);
        }
Beispiel #2
0
        /// <summary>
        /// 执行NeuChar判断过程,获取响应消息
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <param name="messageHandler"></param>
        /// <param name="accessTokenOrApi"></param>
        /// <returns></returns>
        public IResponseMessageBase Execute(IRequestMessageBase requestMessage, IMessageHandlerEnlighten messageHandler, string accessTokenOrApi)
        {
            if (accessTokenOrApi == null)
            {
                throw new ArgumentNullException(nameof(accessTokenOrApi));
            }

            IResponseMessageBase responseMessage = null;

            switch (requestMessage.MsgType)
            {
            case RequestMsgType.Text:
            {
                var textRequestMessage = requestMessage as IRequestMessageText;
                //遍历所有的消息设置
                foreach (var messagePair in Config.MessagePair.Where(z => z.Request.Type == RequestMsgType.Text))
                {
                    //遍历每一个消息设置中的关键词
                    foreach (var keyword in messagePair.Request.Keywords)
                    {
                        if (keyword.Equals(textRequestMessage.Content, StringComparison.OrdinalIgnoreCase))        //TODO:加入大小写敏感设计
                        {
                            responseMessage = GetResponseMessage(requestMessage, messagePair.Response, messageHandler.MessageEntityEnlighten);
                            ExecuteApi(messagePair, requestMessage, messageHandler.ApiEnlighten, accessTokenOrApi, requestMessage.FromUserName);
                            break;
                        }
                    }
                    if (responseMessage != null)
                    {
                        break;
                    }
                }
            }
            break;

            case RequestMsgType.Image:
            {
                var imageRequestMessage = requestMessage as IRequestMessageImage;
                //遍历所有的消息设置

                foreach (var messagePair in Config.MessagePair.Where(z => z.Request.Type == RequestMsgType.Image))
                {
                    responseMessage = GetResponseMessage(requestMessage, messagePair.Response, messageHandler.MessageEntityEnlighten);
                    ExecuteApi(messagePair, requestMessage, messageHandler.ApiEnlighten, accessTokenOrApi, requestMessage.FromUserName);

                    if (responseMessage != null)
                    {
                        break;
                    }
                }
            }
            break;

            default:
                //不作处理

                //throw new UnknownRequestMsgTypeException("NeuChar未支持的的MsgType请求类型:"+ requestMessage.MsgType, null);
                break;
            }
            return(responseMessage);
        }
Beispiel #3
0
 /// <summary>
 /// 执行NeuChar判断过程,获取响应消息
 /// </summary>
 /// <param name="requestMessage"></param>
 /// <returns></returns>
 public async Task <IResponseMessageBase> ExecuteAsync(IRequestMessageBase requestMessage, IMessageHandlerEnlighten messageHandler, string accessTokenOrApi)
 {
     SenparcTrace.SendCustomLog("neuchar trace", "1");
     return(await Task.Run(() => Execute(requestMessage, messageHandler, accessTokenOrApi)));
 }
Beispiel #4
0
 /// <summary>
 /// 执行NeuChar判断过程,获取响应消息
 /// </summary>
 /// <param name="requestMessage"></param>
 /// <returns></returns>
 public async Task <IResponseMessageBase> ExecuteAsync(IRequestMessageBase requestMessage, IMessageHandlerEnlighten messageHandler, string accessTokenOrApi)
 {
     return(await Task.Run(() => Execute(requestMessage, messageHandler, accessTokenOrApi)));
 }