Ejemplo n.º 1
0
 protected virtual WechatBaseMessage OnHandleUnknownMessage(WechatBaseMessage arg, string rawXml)
 {
     using (WechatMessageHandlerSvc.WechatSvcClient client = new WechatMessageHandlerSvc.WechatSvcClient())
     {
         return(client.HandleUnknownMessage(arg, rawXml));
     }
 }
Ejemplo n.º 2
0
        private void HandleWechatRequest()
        {
            string method = Request.HttpMethod.ToUpper();

            WechatLib.WriteLog(method + "|" + Request.Url, DateTime.UtcNow);

            switch (method)
            {
            case "GET":
                // 微信加密签名
                string signature = Request.QueryString["signature"];
                // 时间戳
                string timestamp = Request.QueryString["timestamp"];
                // 随机数
                string nonce = Request.QueryString["nonce"];
                // 随机字符串
                string echostr = Request.QueryString["echostr"];
                if (WechatLib.CheckSignature(signature, timestamp, nonce))
                {
                    Response.ClearContent();
                    Response.ContentType = "text/plain";
                    Response.Write(echostr);
                    WechatLib.WriteLog("Echo:" + echostr, DateTime.UtcNow);
                }
                Response.End();
                break;

            case "POST":
                Response.ContentType = "application/xml";
                StreamReader      stream = new StreamReader(Request.InputStream);
                string            xml    = stream.ReadToEnd();
                WechatBaseMessage msg    = WechatBaseMessage.FromXml <WechatBaseMessage>(xml);
                WechatLib.WriteMessageLog(msg, WechatLib.MessageDirection.Inbound, xml);

                Response.ClearContent();

                WechatBaseMessage responseMsg = WechatLib.RespondMessage(msg, xml);

                if (responseMsg != null)
                {
                    responseMsg.CreateTime   = DateTime.UtcNow;
                    responseMsg.ToUserName   = msg.FromUserName;
                    responseMsg.FromUserName = msg.ToUserName;

                    string responseText = responseMsg.ToXml();
                    Response.Write(responseText);
                    WechatLib.WriteMessageLog(responseMsg, WechatLib.MessageDirection.Outbound, responseText);
                }
                Response.End();
                break;
            }
        }
Ejemplo n.º 3
0
 public static void WriteMessageLog(WechatBaseMessage message, MessageDirection direction, string rawData)
 {
     using (WechatDBDataContext db = new WechatDBDataContext(Settings.Default.WechatDBConnectionString))
     {
         WechatMessage data = new WechatMessage();
         data.CreateTime   = message.CreateTime;
         data.FromUserName = message.FromUserName;
         data.MsgType      = message.MsgType;
         data.Direction    = direction.ToString();
         data.ToUserName   = message.ToUserName;
         data.RawData      = rawData;
         db.WechatMessages.InsertOnSubmit(data);
         db.SubmitChanges();
     }
 }
Ejemplo n.º 4
0
        public static WechatBaseMessage RespondMessage(WechatBaseMessage inboundMessage, string rawXml)
        {
            WechatBaseMessage outboundMessage;

            //Create message object of specific type based on MsgType value
            switch (inboundMessage.MsgType.ToLower())
            {
            case "text":
                WechatTextMessage itm = XmlTypedSerializer <WechatTextMessage> .Deserialize(rawXml);

                WriteLog("text message received: " + itm.Content, DateTime.UtcNow);
                outboundMessage = TextMessageHandler(itm, rawXml);
                break;

            case "event":
                WechatEventMessage iem = XmlTypedSerializer <WechatEventMessage> .Deserialize(rawXml);

                WriteLog(string.Format("event message received: {0}|{1}", iem.EventType, iem.EventKey), DateTime.UtcNow);
                outboundMessage = EventMessageHandler(iem, rawXml);
                break;

            case "image":
                WechatImageMessage iim = XmlTypedSerializer <WechatImageMessage> .Deserialize(rawXml);

                WriteLog(string.Format("image message received: {0}", iim.PicUr), DateTime.UtcNow);
                outboundMessage = ImageMessageHandler(iim, rawXml);
                break;

            default:
                WriteLog(string.Format("Unsupported message type: {0}", inboundMessage.MsgType), DateTime.UtcNow);
                outboundMessage = UnknownMessageHandler(inboundMessage, rawXml);
                break;
            }

            return(outboundMessage);
        }