コード例 #1
0
        /// <summary>
        /// 获取请求消息和实体之间的映射结果
        /// </summary>
        /// <param name="requestMsgType"></param>
        /// <returns></returns>
        public override IRequestMessageBase GetRequestEntityMappingResult(RequestMsgType requestMsgType, XDocument doc = null)
        {
            RequestMessageBase requestMessage;

            switch (requestMsgType)
            {
            case RequestMsgType.Text:
                requestMessage = new RequestMessageText();
                break;

            case RequestMsgType.Image:
                requestMessage = new RequestMessageImage();
                break;

            case RequestMsgType.MiniProgramPage:    //小程序页面
                requestMessage = new RequestMessageMiniProgramPage();
                break;

            case RequestMsgType.NeuChar:
                requestMessage = new RequestMessageNeuChar();
                break;

            case RequestMsgType.Event:
                //判断Event类型
                switch (doc.Root.Element("Event").Value.ToUpper())
                {
                case "USER_ENTER_TEMPSESSION":        //进入客服会话
                    requestMessage = new RequestMessageEvent_UserEnterTempSession();
                    break;

                case "WEAPP_AUDIT_SUCCESS":         //小程序审核成功
                    requestMessage = new RequestMessageEvent_WeAppAuditSuccess();
                    break;

                case "WEAPP_AUDIT_FAIL":         //小程序审核失败
                    requestMessage = new RequestMessageEvent_WeAppAuditFail();
                    break;

                case "WEAPP_AUDIT_DELAY":         //小程序审核延后
                    requestMessage = new RequestMessageEvent_WeAppAuditDelay();
                    break;

                case "WXA_NICKNAME_AUDIT":         //名称审核结果
                    requestMessage = new RequestMessageEvent_NicknameAudit();
                    break;

                default:        //其他意外类型(也可以选择抛出异常)
                    requestMessage = new RequestMessageEventBase();
                    break;
                }
                break;

            default:
                throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!", requestMsgType), new ArgumentOutOfRangeException());    //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
            }
            return(requestMessage);
        }
コード例 #2
0
        public override IResponseMessageBase OnEvent_UserEnterTempSessionRequest(RequestMessageEvent_UserEnterTempSession requestMessage)
        {
            //进入客服
            var msg = "欢迎您!这条消息来自Senparc.Weixin进入客服事件。";

            Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendText(appId, WeixinOpenId, msg);

            return(DefaultResponseMessage(requestMessage));
        }
コード例 #3
0
        //<?xml version="1.0" encoding="utf-8"?>
        //<xml>
        //  <ToUserName><![CDATA[gh_a96a4a619366]]></ToUserName>
        //  <FromUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></FromUserName>
        //  <CreateTime>1357986928</CreateTime>
        //  <MsgType><![CDATA[text]]></MsgType>
        //  <Content><![CDATA[中文]]></Content>
        //  <MsgId>5832509444155992350</MsgId>
        //</xml>

        /// <summary>
        /// 获取XDocument转换后的IRequestMessageBase实例。
        /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
        /// </summary>
        /// <returns></returns>
        public static IRequestMessageBase GetRequestEntity(XDocument doc, PostModel postModel = null)
        {
            RequestMessageBase requestMessage = null;
            RequestMsgType     msgType;
            int aaa;
            int bbb;

            try
            {
                msgType = MsgTypeHelper.GetRequestMsgType(doc);
                switch (msgType)
                {
                case RequestMsgType.Text:
                    requestMessage = new RequestMessageText();
                    break;

                case RequestMsgType.Image:
                    requestMessage = new RequestMessageImage();
                    break;

                case RequestMsgType.NeuChar:
                    requestMessage = new RequestMessageNeuChar();
                    break;

                case RequestMsgType.Event:
                    //判断Event类型
                    switch (doc.Root.Element("Event").Value.ToUpper())
                    {
                    case "USER_ENTER_TEMPSESSION":        //进入客服会话
                        requestMessage = new RequestMessageEvent_UserEnterTempSession();
                        break;

                    default:        //其他意外类型(也可以选择抛出异常)
                        requestMessage = new RequestMessageEventBase();
                        break;
                    }
                    break;

                default:
                    throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());    //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
                }
                Senparc.NeuChar.Helpers.EntityHelper.FillEntityWithXml(requestMessage, doc);
            }
            catch (ArgumentException ex)
            {
                throw new WeixinException(string.Format("RequestMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex);
            }
            return(requestMessage);
        }
コード例 #4
0
        public override IResponseMessageBase OnEvent_UserEnterTempSessionRequest(RequestMessageEvent_UserEnterTempSession requestMessage)
        {
            //进入客服
            var msg = @"欢迎您!这条消息来自 Senparc.Weixin 进入客服事件。

您可以进行以下测试:
1、发送任意文字,返回上下文消息记录
2、发送图片,返回同样的图片
3、发送文字“link”,返回图文链接
4、发送文字“card”,发送小程序卡片";

            Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendText(appId, OpenId, msg);

            return(DefaultResponseMessage(requestMessage));
        }
コード例 #5
0
        /// <summary>
        /// 获取请求消息和实体之间的映射结果
        /// </summary>
        /// <param name="requestMsgType"></param>
        /// <returns></returns>
        public override IRequestMessageBase GetRequestEntityMappingResult(RequestMsgType requestMsgType, XDocument doc = null)
        {
            RequestMessageBase requestMessage;

            switch (requestMsgType)
            {
            case RequestMsgType.Text:
                requestMessage = new RequestMessageText();
                break;

            case RequestMsgType.Image:
                requestMessage = new RequestMessageImage();
                break;

            case RequestMsgType.NeuChar:
                requestMessage = new RequestMessageNeuChar();
                break;

            case RequestMsgType.Event:
                //判断Event类型
                switch (doc.Root.Element("Event").Value.ToUpper())
                {
                case "USER_ENTER_TEMPSESSION":        //进入客服会话
                    requestMessage = new RequestMessageEvent_UserEnterTempSession();
                    break;

                default:        //其他意外类型(也可以选择抛出异常)
                    requestMessage = new RequestMessageEventBase();
                    break;
                }
                break;

            default:
                throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在RequestMessageFactory中没有对应的处理程序!", requestMsgType), new ArgumentOutOfRangeException());    //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常
            }
            return(requestMessage);
        }
コード例 #6
0
 /// <summary>
 /// 进入客服会话事件
 /// </summary>
 public virtual IResponseMessageBase OnEvent_UserEnterTempSessionRequest(RequestMessageEvent_UserEnterTempSession requestMessage)
 {
     return(DefaultResponseMessage(requestMessage));
 }
コード例 #7
0
        public override async Task <IResponseMessageBase> OnEvent_UserEnterTempSessionRequestAsync(RequestMessageEvent_UserEnterTempSession requestMessage)
        {
            //进入客服
            var msg = @"欢迎您!这条消息来自 Senparc.Weixin 进入客服事件。

您可以进行以下测试:
1、发送任意文字,返回上下文消息记录
2、发送图片,返回同样的图片
3、发送文字“link”,返回图文链接
4、发送文字“card”,发送小程序卡片
5、点击右下角出现的小程序浮窗,发送小程序页面信息";
            await Senparc.Weixin.WxOpen.AdvancedAPIs.CustomApi.SendTextAsync(appId, OpenId, msg);

            return(await DefaultResponseMessageAsync(requestMessage));
        }
コード例 #8
0
 /// <summary>
 /// 【异步方法】进入客服会话事件
 /// </summary>
 public virtual async Task <IResponseMessageBase> OnEvent_UserEnterTempSessionRequestAsync(RequestMessageEvent_UserEnterTempSession requestMessage)
 {
     return(DefaultResponseMessage(requestMessage));
 }
コード例 #9
0
 /// <summary>
 /// 【异步方法】进入客服会话事件
 /// </summary>
 public virtual async Task <IResponseMessageBase> OnEvent_UserEnterTempSessionRequestAsync(RequestMessageEvent_UserEnterTempSession requestMessage)
 {
     return(await DefaultAsyncMethod(requestMessage, () => OnEvent_UserEnterTempSessionRequest(requestMessage)).ConfigureAwait(false));
 }