Detailed explanation of asp.net WeChat development message response method
高洛峰
Release: 2017-03-24 14:43:13
Original
1656 people have browsed it
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.
The push XML data packet structure of each message type is as follows:
Text message
13488318601234567890123456
Copy after login
Parameters
Description
ToUserName
DeveloperWeChat ID
FromUserName
Sender account (an OpenID)
CreateTime
Message creation time (integer)
##MsgType
text
Content
Text message content
MsgId
Message id, 64-bit integer
Picture message
13488318601234567890123456
Copy after login
Parameters
Description
ToUserName
DeveloperWeChat ID
FromUserName
Sender account (an OpenID)
CreateTime
Message creation time (integer)
MsgType
image
PicUrl
Picture link
MediaId
Picture message media id, you can call the multimedia file download interface to pull data.
MsgId
Message id, 64-bit integer
Voice message
13572909131234567890123456
Copy after login
Parameters
Description
ToUserName
DeveloperWeChat ID
FromUserName
Sender account (an OpenID)
CreateTime
Message creation time (Integer type)
MsgType
Voice is voice
MediaId
Voice message media id, You can call the multimedia file download interface to pull data.
Format
Voice format, such as amr, speex, etc.
MsgID
Message id, 64 Bit integer
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:
13572909131234567890123456
Copy after login
In the extra fields, Format is the voice format, usually amr, and Recognition is the voice recognition result, using UTF8 encoding.
Video Message
13572909131234567890123456
Copy after login
Copy after login
Parameters
Description
ToUserName
DeveloperWeChat ID
FromUserName
Sender account (an OpenID)
CreateTime
Message creation time (integer)
MsgType
The video is video
MediaId
Video message media id, you can call the multimedia file download interface to pull data.
ThumbMediaId
The media id of the video message thumbnail, you can call the multimedia file download interface to pull the data.
MsgId
Message id, 64-bit integer type
Small video message
13572909131234567890123456
Copy after login
Copy after login
地理位置消息
135177636023.134521113.358803201234567890123456
Copy after login
Parameters
Description
ToUserName
Developer微信
FromUserName
Sender account (an OpenID)
CreateTime
Message creation Time (integer type)
MsgType
The short video is shortvideo
MediaId
Video message media id, you can call the multimedia file download interface to pull data.
ThumbMediaId
The media id of the video message thumbnail, you can call the multimedia file download interface to pull the data.
MsgId
Message id, 64-bit integer
参数
描述
ToUserName
开发者微信号
FromUserName
发送方帐号(一个OpenID)
CreateTime
消息创建时间 (整型)
MsgType
location
Location_X
地理位置维度
Location_Y
地理位置经度
Scale
地图缩放大小
Label
地理位置信息
MsgId
消息id,64位整型
链接消息
13517763601234567890123456
Copy after login
参数
描述
ToUserName
接收方微信号
FromUserName
发送方微信号,若为普通用户,则是一个OpenID
CreateTime
消息创建时间
MsgType
消息类型,link
Title
消息标题
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,
’’'' ’' 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 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!
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