公眾號第三方平台開發 教程五 代公眾號處理消息和事件


公眾號第三方平台開發 教程一 創建公眾號第三方平台

公眾號第三方平台開發 教程二 component_verify_ticket和accessToken的獲取

公眾號第三方平台開發 教程三 微信公眾號授權第三方平台

公眾號第三方平台開發 教程四 代公眾號發起網頁授權說明

公眾號第三方平台開發 教程五 代公眾號處理消息和事件

公眾號第三方平台開發 教程六 代公眾號使用JS SDK說明

另,感謝一下這個大蝦的博客,這幾篇東西都是在他的博文基礎上完成的,他的博客里也有一些接口代碼可以下載
微信開發系列教程

 當粉絲給公眾號信息的時候,服務器會給公眾號消息與事件接收URL發送信息

 下面直接上處理函數,處理完成之后直接在頁面上輸出加密后的消息即可。注意要把前台的html代碼刪除

 public void ResponseMessage()
    {
        WXBizMsgCrypt wxcpt = new WXBizMsgCrypt();


        string sReqData = GetPost();
        string respnseContent = "";
        string sResponse = "";  //加密之后的回復文本


        string sMsg = "";  //解析之后的明文
        int ret = 0;
        ret = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData, ref sMsg);


        LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 接收到的信息:{0}", sReqData, pageUrl));

        LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 解析后的消息:{0}", sMsg, pageUrl));


        var xDoc = XDocument.Parse(sMsg);

        List<XElement> q = (from c in xDoc.Elements() select c).ToList();

        var model = new
        {
            ToUserName = q.Elements("ToUserName").First().Value,
            FromUserName = q.Elements("FromUserName").First().Value,
            CreateTime = q.Elements("CreateTime").First().Value,

            MsgType = q.Elements("MsgType").First().Value,
            Content = ("" + q.Elements("Content").First().Value).Trim(),
            MsgId = q.Elements("MsgId").First().Value
        };


        if (false == string.IsNullOrEmpty(model.Content))
        {
            switch (model.Content.ToLower())
            {
                case "幫助":
                case "help":
                case "hello2bizuser": //被關注時自動回復  
                    respnseContent = "使用幫助說明:\r\n------------------------\r\n"
                        + "常用命令:\r\n"
                        + "1、查看案例;\r\n"
                        + "2、聯系方式;\r\n"
                        + "3、幫助或help;\r\n";                
                    break;

                case "查看案例":
                    respnseContent = "查看案例:\r\n------------------------\r\n";
                    break;

                case "聯系方式":
                    respnseContent = "聯系方式:\r\n------------------------\r\n";
                    break;

                default:
                    break;
            }


            var textTpl = "<xml>"
                              + "<ToUserName><![CDATA[{0}]]></ToUserName>"
                              + "<FromUserName><![CDATA[{1}]]></FromUserName>"
                              + "<CreateTime>{2}</CreateTime>"
                              + "<MsgType><![CDATA[{3}]]></MsgType>"
                              + "<Content><![CDATA[{4}]]></Content>"
                              + "<FuncFlag>0</FuncFlag>"
                              + "</xml>";


            int enRet = wxcpt.EncryptMsg(string.Format(textTpl, model.FromUserName, model.ToUserName, ConvertDateTimeInt(DateTime.Now), "text", respnseContent), sReqTimeStamp, sReqNonce, ref sResponse);

            LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 加密前的消息:{0}", string.Format(textTpl, model.FromUserName, model.ToUserName, ConvertDateTimeInt(DateTime.Now), "text", respnseContent), pageUrl));
            LogTool.LogToDirDest(filePath, LogType.Debug, string.Format("{1} 加密后的消息:{0}", sResponse, pageUrl));

            Response.Write(sResponse);
            Response.End();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM