Home  >  Article  >  WeChat Applet  >  Detailed explanation of asp.net WeChat development message response method

Detailed explanation of asp.net WeChat development message response method

高洛峰
高洛峰Original
2017-03-24 14:43:131705browse

When an ordinary WeChat user sends a message to a public account, the WeChat server will POST the XML data packet of the message to the URL filled in by the developer.

Please note:

1、关于重试的消息排重,推荐使用msgid排重。
2、微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次。假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试。详情请见“发送消息-被动回复消息”。
3、为了保证更高的安全保障,开发者可以在公众平台官网的开发者中心处设置消息加密。开启加密后,用户发来的消息会被加密,公众号被动回复用户的消息也需要加密(但开发者通过客服接口等API调用形式向用户发送消息,则不受影响)。关于消息加解密的详细说明,请见“消息加解密说明”。

The push XML data packet structure of each message type is as follows:

Text message

 <xml>
 <tousername></tousername>
 <fromusername></fromusername> 
 <createtime>1348831860</createtime>
 <msgtype></msgtype>
 <content></content>
 <msgid>1234567890123456</msgid>
 </xml>
##MsgTypetextContentText message contentMsgIdMessage id, 64-bit integer
Parameters Description
ToUserName DeveloperWeChat ID
FromUserName Sender account (an OpenID)
CreateTime Message creation time (integer)

Picture message
 <xml>
 <tousername></tousername>
 <fromusername></fromusername>
 <createtime>1348831860</createtime>
 <msgtype></msgtype>
 <picurl></picurl>
 <mediaid></mediaid>
 <msgid>1234567890123456</msgid>
 </xml>

ParametersDescription ToUserNameFromUserNameSender account (an OpenID)CreateTimeMessage creation time (integer)MsgTypeimagePicUrlPicture linkMediaIdPicture message media id, you can call the multimedia file download interface to pull data. MsgIdMessage id, 64-bit integer
DeveloperWeChat ID

Voice message
<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<format></format>
<msgid>1234567890123456</msgid>
</xml>

ParametersDescriptionToUserNameFromUserNameSender account (an OpenID)CreateTimeMessage creation time (Integer type) MsgTypeVoice is voiceMediaIdVoice message media id, You can call the multimedia file download interface to pull data. FormatVoice format, such as amr, speex, etc.MsgIDMessage id, 64 Bit integer
Developer WeChat ID
Please note that after enabling voice recognition, every time the user sends a voice to the official account, WeChat will add a Recongnition field (Note: Due to client caching, developers can turn on or off the speech recognition function, which will take effect immediately for new followers and take 24 hours for already followed users. Developers can re-follow this account for testing). The voice XML data packet after enabling voice recognition is as follows:

<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<format></format>
<recognition></recognition>
<msgid>1234567890123456</msgid>
</xml>
In the extra fields, Format is the voice format, usually amr, and Recognition is the voice recognition result, using UTF8 encoding.

Video Message
<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<thumbmediaid></thumbmediaid>
<msgid>1234567890123456</msgid>
</xml>

ParametersDescription ToUserNameFromUserNameSender account (an OpenID)CreateTimeMessage creation time (integer)MsgTypeThe video is videoMediaIdVideo message media id, you can call the multimedia file download interface to pull data. ThumbMediaIdThe media id of the video message thumbnail, you can call the multimedia file download interface to pull the data. MsgIdMessage id, 64-bit integer type
DeveloperWeChat ID

Small video message
<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<thumbmediaid></thumbmediaid>
<msgid>1234567890123456</msgid>
</xml>

ParametersDescriptionToUserNameFromUserNameSender account (an OpenID)CreateTimeMessage creation Time (integer type)MsgTypeThe short video is shortvideoMediaIdVideo message media id, you can call the multimedia file download interface to pull data. ThumbMediaIdThe media id of the video message thumbnail, you can call the multimedia file download interface to pull the data. MsgIdMessage id, 64-bit integer

地理位置消息

<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1351776360</createtime>
<msgtype></msgtype>
<location_x>23.134521</location_x>
<location_y>113.358803</location_y>
<scale>20</scale>
<label></label>
<msgid>1234567890123456</msgid>
</xml>
Developer微信
参数 描述
ToUserName 开发者微信号
FromUserName 发送方帐号(一个OpenID)
CreateTime 消息创建时间 (整型)
MsgType location
Location_X 地理位置维度
Location_Y 地理位置经度
Scale 地图缩放大小
Label 地理位置信息
MsgId 消息id,64位整型

链接消息

<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1351776360</createtime>
<msgtype></msgtype>
<title></title>
<description></description>
<url></url>
<msgid>1234567890123456</msgid>
</xml>
参数 描述
ToUserName 接收方微信号
FromUserName 发送方微信号,若为普通用户,则是一个OpenID
CreateTime 消息创建时间
MsgType 消息类型,link
Title 消息标题
Description 消息描述
Url 消息链接
MsgId

消息id,64位整型

Detailed explanation of asp.net WeChat development message response method

Continued from the previous article, see ResponseXML(postString); the method is as follows

///


/// Get Messages sent by users
                                                   ’’’’’’’’’’’’’’’’’’’’ out out out through out through out out through out out through out together’’’psy out out out out out out out out out out out out out out's's's's's's' out''‐''‑'''‑'''‐By''‐By‐‐‐‐ paths to to be a Message to to be to the user to to be to account to the user to to be to the user to to to to itself to to to to to to itself to to up to a whirlwind pace to be as high as 10%?
                                                                                                    Element rootElement = xmlDoc.DocumentElement;//Get the root of the document
XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); //Get the text type of the message
RequestXML requestXML = new RequestXML();//Declare the instance, get each attribute and assign a value
requestXML.ToUserName = rootElement.SelectSingleNode ("ToUserName").InnerText;//Public account
requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;//User
requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText ;//Creation time
requestXML.MsgType = MsgType.InnerText;//Message type

 
                                                                                                                                                                                                            tent = rootElement.SelectSingleNode("Content").InnerText;
                                                                                                                                                                              rootElement. Map zoom ratio, geographical location description
requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText;
requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText;
requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText;
requestXML.Label = rootElement.SelectSingleNode("Label").InnerText;
}
if (requestXML.MsgType.Trim().ToLower() = = "event")
                                                                                                                                                                               = rootElement.SelectSingleNode ("EventKey").InnerText;
                                                                                                                                                                                                                                       if (requestXML.MsgType.Trim().ToLower() == "voice") As a result, you must remember to turn on the voice recognition function in the developer mode before assigning, otherwise you will not get
Requestxml.RCOGNITION = rootlement.SelectsingLende ("Recognition"). Innertext;
} SEMSG ( requestXML);

}


The speech recognition function is turned on as follows:



requestXML is one I created separately Class, which declares the commonly used attribute fields in messages, as follows:

///


/// The entity class that receives the message
///

public class RequestXML
{
private String toUserName = String .Empty;
/// & lt; summary & gt;
/// Ben public account
/// & lt;/summary & gt;
PUBLIC String Tousername {get;} ##// /
; /// User WeChat ID
; ///

/ //


; #         public String Content{get;set;}
                                                                                                                    /*The following are the properties unique to event type messages*/
                                                                                                                        /
; string EventKey { get; set; }
                                                                        using   use using it ’         ’ ’ ’ out’s ’ out’s out’s out’s out’s out out out out out way out out way out out out out out out out out out out out out out out out out out back out past ’ s ’ ’s out off out’s’ out out out out out out out out out out out out out out out out out out out out outO s , , , , , , , , , , - , / / summary>
         public string Title { get; set; }
                             ’’’’’’’’’’’’’’’’’’’’’’’’’’ out out’’ out out out out out out’’ out out out out out out out out out out out out out out out out of's to'’’’’ ‑   ‐‐'‐'‐‐‐‐‐ and to ? String description {get; set;}
/// & lt; summary & gt;
/// The link address of graphic message pictures
/// & lt;/summary & gt;
Public string picurl {get ; set; }
///
## /// & lt; Summary & gt;
/// 图 图
/// & lt;/Summary & GT;
Public list; requestxml & gt; Articles {get;}
/ * The following are the properties unique to messages of geographical location type*/
                                            ’’’’’’’’’’’’’’’’’’’’’’’’’’ out out out out Throughmb Through Outceps out right out right out out out out out out out out out out out out out out out out out out out outs’s ; set; }
///
/// Geographical Location
/// & LT;/Summary & GT;
Public String Location_y {get;}
/// & lt;
/// map zoom ratio

                                                                                                       ;
          public String Label { get; set; }                                                                 use using using using ‐                   out out out through ’’s ’         ’’s ’     ’’s ’ ’ ’s ’       through out through out through out through out through through through over over through through through through through through through through‐ through through through through through through through through through through through through through through through‐ through‐over through through right through right through outthrough through through out through through out through through through out through out out through out out out out through Through out through over over over over over''s to come through through's toward's'‐''‐'‐‐‐‐‐ light to be pass be a t; public String Recognition { get; set; }


}


Continue to follow ResponseMsg(requestXML);The method is as follows,

private void ResponseMsg(RequestXML requestXML )

                                                                                       string MsgType =                       MsgType;##                                                                 ‐   ’s ‐ ‐ ‐ ‐ ‐‐‐‐‐‐‐‐‐ ‐‐ ‐ ##                                                   {

Case "text":
SendtextCase (Requestxml); // Send text messages
Break;

Case "EVENT": // Send the event

if (! String.isnullWhitespace (Requestxml. EventName ) && requestXML.EventName.ToString().Trim().Equals("subscribe"))

                                                                                                }

                                                                                                   if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK"))
                                                                 );//Send event message
                                    
                                                                                                                      

     case "voice":
      SendVoiceMsg(requestXML);//Send a voice message                       SendMapMsg(requestXML);
break;
                                 
                                        break; {
    HttpContext.Current.Response.Write(ex.ToString());
                                                                                                                        Send text
                                                                       #            string responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName, requestXML.Content); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8 ;
                HttpContext.Current.Response.Write(responseContent);
                                                                                             

///


                                                                                                                               ’’'' ’'                   to be returned to the formatted                                                                                Public account
; /// User account
; /// Reply content
                                                                                                                                                                                                               ‐                                                                                ;" + DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString() + "1";
            

In this way, the response to the message can be realized. If the user clicks the button, the following code:

case "event"://Send event message
If (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe"))

                                                                                                                                                                                 else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK"))

                                                                                                   }

                                                                                                                                 

///


                                                                                                                                       ’’’’’’’’’ ‐ ’ parameter case "mypay":
             SendPayDetails(requestXML);//Send salary bill
                                                                                                                                  //Send weather forecast
                                                                                                                                                                :
           SendKaiXinMessage(requestXML);//Send a happy smile result set XML);//Send modification information link
                                                                                                                                                                                                                            #             case "yuangonghuodong":
            SendYuanGongHuoDong(requestXML);//Send student activities
                                                                                                                        ##                 SendYuanGongTongZhi(requestXML);//Send employee notification
                                                                                                                                                               SendYuanGongTongZhi(requestXML);//Send employee notification
                                                             ​ 
          case "youwenbida":
          SendWenti(requestXML);//Send employee submission link
                                                                                                                              ​​​​​SendWentiList(requestXML);//Send question list link
                                                                                                                                                     break;                                                                                                                              defaultresponseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName,"This feature is not available yet! Please stay tuned!"); current. Response.ContentEncoding = Encoding.UTF8;
                                                                                                              break;
}
}

SendWelComeMsg(requestXML);//The graphic message returned when paying attention

///

/// The graphic message returned when paying attention

///

          string newdate = DateTime.Now.Subtract(new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds.ToString();

            string PUrlfileName = "http://www.deqiaohr. com.cn/weixin/welcome.jpg";
           responseContent = string.Format(Message_News_Main, requestXML.FromUserName, requestXML.ToUserName, newdate, "1",
              string.Format((Message_News_Item, "Welcome to Deqiao) Employee Service Center", "Suzhou Deqiao Human Resources was founded in 2002...", PUrlfileName, "http://www.deqiaohr.com.cn/weixin/WxGsjianjie.aspx"));
    HttpContext.Current .Response.ContentType = "text/xml";
                                                          current.Response.End ();
}

Message_News_Main and Message_News_Item are graphic and text message formats

  ///


        /// 返回图文消息主体
        ///

        public static string Message_News_Main
        {
            get
            {
                return @"
                           
                           
                            {2}
                            
                             {3}
                            
                             {4}
                          

                          
";
            }
        }
        ///
        /// 返回图文消息项
        ///

        public static string Message_News_Item
        {
            get
            {
                return @"
                           
                           
                           
                           
                           
";
            }
        }

  ///


                                                                                          /param> ToString());
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.Write( responseContent) ;
                                                                                                                                                                    Web page, regarding the issue of adaptive web page images, just set the width of the image to 100%. If you want the web page to adapt to the screen, remember to add


The above is the detailed content of Detailed explanation of asp.net WeChat development message response method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn