Esempio n. 1
0
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            this.ProcessMessage(message, service, new Queue <IWeChatMessageHandler>(this.m_handlers), result, true);
            return(result);
        }
Esempio n. 2
0
        private void ProcessMessage(IWeChatMessage message, IOfficialAccountService service, Queue <IWeChatMessageHandler> queue, AsyncResult <IWeChatMessageResult> result, bool completedSynchronously)
        {
            if (queue.Count == 0)
            {
                result.MarkCompleted(null, completedSynchronously, null);
                return;
            }

            IWeChatMessageHandler handler = queue.Dequeue();

            handler.BeginProcessMessage(message, service, (ar) => {
                Exception error = null;
                IWeChatMessageResult messageResult = null;
                bool synchronously = completedSynchronously && ar.CompletedSynchronously;

                try {
                    messageResult = handler.EndProcessMessage(ar);
                } catch (Exception ex) {
                    error = ex;
                }

                if (error != null)
                {
                    result.MarkCompleted(error, synchronously, null);
                }
                else if (messageResult != null)
                {
                    result.MarkCompleted(null, synchronously, messageResult);
                }
                else
                {
                    this.ProcessMessage(message, service, queue, result, synchronously);
                }
            }, null);
        }
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            IWeChatMessageResult messageResult = new WeChatTextReply {
                ToUserName   = message.FromUserName,
                FromUserName = message.ToUserName,
                CreateTime   = message.CreateTime,
                Content      = "你好,欢迎关注南阳仙草药业公众号,<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4c8496aa5431914e&redirect_uri=http%3A%2F%2Fweixin.xphter.com%2Fhome%2Fbinding&response_type=code&scope=snsapi_base&state=123#wechat_redirect\">点击链接</a>绑定微信号,随时了解您的订单动态。发送\"订单提醒\"消息,系统将向您发送订单提醒。",
                //Content = "你好,欢迎关注南阳仙草药业公众号,<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4c8496aa5431914e&redirect_uri=http%3A%2F%2Fxphter.gicp.net%2FXphter.Framework.WeChat.OfficialAccountsTest.Portal%2Fhome%2Fbinding&response_type=code&scope=snsapi_base&state=123#wechat_redirect\">点击链接</a>绑定微信号,随时了解您的订单动态。发送\"订单提醒\"消息,系统将向您发送订单提醒。",
            };

            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            result.MarkCompleted(null, true, messageResult);
            return(result);
        }
        public IAsyncResult BeginProcessMessage(byte[] data, AsyncCallback callback, object userState)
        {
            IWeChatMessage        message             = null;
            IWeChatMessageHandler handler             = null;
            string postContent                        = Encoding.UTF8.GetString(data);
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);

            try {
                message = this.m_messageFactory.GetMessage(postContent, this);
            } catch (Exception ex) {
                result.MarkCompleted(ex, true, null);
                return(result);
            }

            if (message != null)
            {
                try {
                    handler = this.m_handlerFactory.GetHandler(message, this);
                } catch (Exception ex) {
                    result.MarkCompleted(ex, true, null);
                    return(result);
                }

                if (handler != null && handler.Priority != WeChatMessageHandlerPriority.Disabled)
                {
                    handler.BeginProcessMessage(message, this, (ar) => {
                        Exception error = null;
                        IWeChatMessageResult messageResult = null;
                        try {
                            messageResult = handler.EndProcessMessage(ar);
                        } catch (Exception ex) {
                            error = ex;
                        }
                        result.MarkCompleted(error, ar.CompletedSynchronously, messageResult ?? WeChatEmptyMessageResult.Instance);
                    }, null);
                }
                else
                {
                    result.MarkCompleted(null, true, WeChatEmptyMessageResult.Instance);
                }
            }
            else
            {
                result.MarkCompleted(null, true, WeChatEmptyMessageResult.Instance);
            }

            return(result);
        }
        public IWeChatMessageHandler GetHandler(IWeChatMessage message, IOfficialAccountService service)
        {
            IWeChatMessageHandler handler = null;

            switch (message.MsgType)
            {
            case WeChatMessageTypes.EVENT:
                IWeChatEventMessage eventMessage = (IWeChatEventMessage)message;
                if (this.m_eventHandlers.ContainsKey(eventMessage.Event))
                {
                    handler = this.m_eventHandlers[eventMessage.Event];
                }
                break;

            default:
                if (this.m_messageHandlers.ContainsKey(message.MsgType))
                {
                    handler = this.m_messageHandlers[message.MsgType];
                }
                break;
            }

            return(handler);
        }
Esempio n. 6
0
        public IAsyncResult BeginProcessMessage(IWeChatMessage message, IOfficialAccountService service, AsyncCallback callback, object userState)
        {
            AsyncResult <IWeChatMessageResult> result = new AsyncResult <IWeChatMessageResult>(callback, userState);
            IWeChatTextMessage textMessage            = message as IWeChatTextMessage;

            if (string.Equals(textMessage.Content, "模板消息"))
            {
                string content = File.ReadAllText(HostingEnvironment.MapPath("~/order.json"), Encoding.UTF8).Replace("{openid}", textMessage.FromUserName);
                service.BeginSendTemplateMessage(content, (ar) => {
                    IWeChatTemplateMessageReturnValue value = service.EndSendTemplateMessage(ar);
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = "系统随后将向您发送模板消息",
                });
            }
            else if (string.Equals(textMessage.Content, "客服消息"))
            {
                string content = File.ReadAllText(HostingEnvironment.MapPath("~/kf.json"), Encoding.UTF8).Replace("{openid}", textMessage.FromUserName);
                service.BeginSendCustomerServiceMessage(content, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                service.BeginSendCustomerServiceMessage(new WeChatTextCustomerServiceMessage("嘿嘿哈嘿")
                {
                    ToUserName = message.FromUserName,
                }, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                WeChatNewsCustomerServiceMessage news = new WeChatNewsCustomerServiceMessage {
                    ToUserName = message.FromUserName,
                };
                news.News.Items.Add(new WeChatNewsCustomerServiceMessageItem {
                    Title       = "丰满性感尤物洛可可浴室大摆诱人姿势",
                    Description = "丰满性感尤物洛可可浴室大摆诱人姿势",
                    PicUrl      = "http://www.xunmeitu.com/Static/Images/2016/12/24/185706528004254.jpg",
                    Url         = "http://www.xunmeitu.com/a/17798.html",
                });
                news.News.Items.Add(new WeChatNewsCustomerServiceMessageItem {
                    Title       = "性感田妞肤白貌美惹眼上围乳此动人",
                    Description = "性感田妞肤白貌美惹眼上围乳此动人",
                    PicUrl      = "http://www.xunmeitu.com/Static/Images/2016/10/07/153628983003544.jpg",
                    Url         = "http://www.xunmeitu.com/a/9837.html",
                });
                service.BeginSendCustomerServiceMessage(news, (ar) => {
                    service.EndSendCustomerServiceMessage(ar);
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = "系统随后将向您发送客服消息",
                });
            }
            else
            {
                service.BeginGetUserInfo(message.FromUserName, WeChatLanguage.SimplifiedChinese, (ar) => {
                    IWeChatUserInfo userInfo = null;

                    try {
                        userInfo = service.EndGetUserInfo(ar);
                    } catch (Exception) {
                    }
                }, null);

                result.MarkCompleted(null, true, new WeChatTextReply {
                    ToUserName   = message.FromUserName,
                    FromUserName = message.ToUserName,
                    CreateTime   = message.CreateTime,
                    Content      = textMessage.Content,
                });
            }

            return(result);
        }
Esempio n. 7
0
 public static bool ContentEquals(this IWeChatMessage msg, string s)
 {
     return(msg.MsgType == "text" && (msg as TextMessage).Content.Equals(s));
 }
Esempio n. 8
0
 public static bool IsNews(this IWeChatMessage msg)
 {
     return(msg.MsgType == "news");
 }
Esempio n. 9
0
 public static bool IsSubscribe(this IWeChatMessage msg)
 {
     return(msg.MsgType == "event" && (msg as EventMessage).Event == "subscribe");
 }
Esempio n. 10
0
 public static bool IsMusic(this IWeChatMessage msg)
 {
     return(msg.MsgType == "music");
 }
Esempio n. 11
0
 public static bool IsImage(this IWeChatMessage msg)
 {
     return(msg.MsgType == "image");
 }
Esempio n. 12
0
 public static bool IsLocation(this IWeChatMessage msg)
 {
     return(msg.MsgType == "event" && (msg as EventMessage).Event == "location");
 }
Esempio n. 13
0
 public static bool IsLink(this IWeChatMessage msg)
 {
     return(msg.MsgType == "link");
 }
Esempio n. 14
0
 public static bool IsText(this IWeChatMessage msg)
 {
     return(msg.MsgType == "text");
 }
Esempio n. 15
0
 public static bool IsEvent(this IWeChatMessage msg)
 {
     return(msg.MsgType == "event");
 }
Esempio n. 16
0
 public void GetMessageTest_TextMessage()
 {
     DefaultWeChatMessageFactory target = new DefaultWeChatMessageFactory();
     IWeChatMessage message             = target.GetMessage(Xphter.Framework.Test.Properties.Resources.WeChatTextMessage, null);
 }