


Detailed explanation of asp.net WeChat development message response method
Mar 24, 2017 pm 02:43 PMWhen 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、關(guān)于重試的消息排重,推薦使用msgid排重。 2、微信服務(wù)器在五秒內(nèi)收不到響應(yīng)會斷掉連接,并且重新發(fā)起請求,總共重試三次。假如服務(wù)器無法保證在五秒內(nèi)處理并回復(fù),可以直接回復(fù)空串,微信服務(wù)器不會對此作任何處理,并且不會發(fā)起重試。詳情請見“發(fā)送消息-被動(dòng)回復(fù)消息”。 3、為了保證更高的安全保障,開發(fā)者可以在公眾平臺官網(wǎng)的開發(fā)者中心處設(shè)置消息加密。開啟加密后,用戶發(fā)來的消息會被加密,公眾號被動(dòng)回復(fù)用戶的消息也需要加密(但開發(fā)者通過客服接口等API調(diào)用形式向用戶發(fā)送消息,則不受影響)。關(guān)于消息加解密的詳細(xì)說明,請見“消息加解密說明”。
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>
Parameters | Description |
---|---|
ToUserName | DeveloperWeChat ID |
FromUserName | Sender account (an OpenID) |
CreateTime | Message creation time (integer) |
text | |
Text message content | |
Message id, 64-bit integer |
Picture message?<xml>
?<tousername></tousername>
?<fromusername></fromusername>
?<createtime>1348831860</createtime>
?<msgtype></msgtype>
?<picurl></picurl>
?<mediaid></mediaid>
?<msgid>1234567890123456</msgid>
?</xml>
Description | |
---|---|
DeveloperWeChat ID | |
Sender account (an OpenID) | |
Message creation time (integer) | |
image | |
Picture link | |
Picture message media id, you can call the multimedia file download interface to pull data. | |
Message id, 64-bit integer |
Voice message<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<format></format>
<msgid>1234567890123456</msgid>
</xml>
Description | |
---|---|
Developer WeChat ID | |
Sender account (an OpenID) | |
Message creation time (Integer type) | |
Voice is voice | |
Voice message media id, You can call the multimedia file download interface to pull data. | |
Voice format, such as amr, speex, etc. | |
Message id, 64 Bit integer |
<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>
Description | |
---|---|
DeveloperWeChat ID | |
Sender account (an OpenID) | |
Message creation time (integer) | |
The video is video | |
Video message media id, you can call the multimedia file download interface to pull data. | |
The media id of the video message thumbnail, you can call the multimedia file download interface to pull the data. | |
Message id, 64-bit integer type |
Small video message <xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>1357290913</createtime>
<msgtype></msgtype>
<mediaid></mediaid>
<thumbmediaid></thumbmediaid>
<msgid>1234567890123456</msgid>
</xml>
Description | |
---|---|
Developer微信 | |
Sender account (an OpenID) | |
Message creation Time (integer type) | |
The short video is shortvideo | |
Video message media id, you can call the multimedia file download interface to pull data. | |
The media id of the video message thumbnail, you can call the multimedia file download interface to pull the data. | |
Message id, 64-bit integer |
參數(shù) | 描述 |
---|---|
ToUserName | 開發(fā)者微信號 |
FromUserName | 發(fā)送方帳號(一個(gè)OpenID) |
CreateTime | 消息創(chuàng)建時(shí)間 (整型) |
MsgType | location |
Location_X | 地理位置維度 |
Location_Y | 地理位置經(jīng)度 |
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>
參數(shù) | 描述 |
---|---|
ToUserName | 接收方微信號 |
FromUserName | 發(fā)送方微信號,若為普通用戶,則是一個(gè)OpenID |
CreateTime | 消息創(chuàng)建時(shí)間 |
MsgType | 消息類型,link |
Title | 消息標(biāo)題 |
Description | 消息描述 |
Url | 消息鏈接 |
MsgId | 消息id,64位整型 |
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 )
Case "text":
SendtextCase (Requestxml); // Send text messages
Break;
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
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????‐????????????????????????????????????????????????????????????????????????????????;
????????????
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"))
?????????????????????????????????????????????????????????????????????????????????????????????????? }
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ///
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????’’’’’’’’’ ‐ ’ 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
///
///
????????? 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 @"
???????????????????????????
???????????????????????????
???????????????????????????
????????????????????????????
????????????????????????????
????????????????????????????
???????????????????????????? {4}
??????????????????????????
??????????????????????????
??????????? }
??????? }
??????? ///
??????? /// 返回圖文消息項(xiàng)
??????? ///
??????? 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)