asp.net micro-credit development (message response) _ Practical skills

Source: Internet
Author: User

When an ordinary micro-user sends a message to a public account, the micro-mail server sends the XML packet of the post message to the URL that the developer fills in.
Please note:

    • 1, about the retry of the message row weight, the recommended use of MsgId row weight.
    • 2, the micro-trust server in five seconds to receive a response will break the connection, and the request again, a total of three retries. If the server is not guaranteed to process and reply within five seconds, you can reply to the empty string directly, the micro-trust server will not do any processing, and will not initiate a retry. See "Send a message-passive reply message" for details.
    • 3. To ensure higher security, developers can set up message encryption at the Developer center in the public Platform's official website. When the encryption is turned on, the message sent by the user is encrypted, and the message of the public number responding to the user also needs to be encrypted (but the developer sends the message to the user through an API call such as the customer service interface, it is not affected). For a detailed explanation of the message plus decryption, see "message plus decryption instructions."

The push XML packet structure for each message type is as follows:
Text messages

 <xml>
 <tousername><![ Cdata[touser]]></tousername>
 <fromusername><![ cdata[fromuser]]></fromusername> 
 <CreateTime>1348831860</CreateTime>
 <msgtype ><! [cdata[text]]></msgtype>
 <content><![ Cdata[this is a test]]></content>
 <MsgId>1234567890123456</MsgId>
 </xml>

Picture Message

<xml>
 <tousername><![ Cdata[touser]]></tousername>
 <fromusername><![ cdata[fromuser]]></fromusername>
 <CreateTime>1348831860</CreateTime>
 <msgtype ><! [cdata[image]]></msgtype>
 <picurl><![ Cdata[this is a url]]></picurl>
 <mediaid><![ cdata[media_id]]></mediaid>
 <MsgId>1234567890123456</MsgId>
 </xml>

Voice message

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1357290913</CreateTime>
<msgtype ><! [cdata[voice]]></msgtype>
<mediaid><![ Cdata[media_id]]></mediaid>
<format><![ cdata[format]]></format>
<MsgId>1234567890123456</MsgId>
</xml>

Note that after the speech recognition is opened, the micro-letter adds a recongnition field to the voice message XML packet that is being sent every time the user sends the voice to the public number (note: Because of the client cache, the developer turns on or off the speech recognition function, it immediately takes effect for new followers. , it takes 24 hours to take effect on users who have been concerned. Developers can focus on this account again for testing). The speech XML packets that turn on speech recognition are as follows:

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1357290913</CreateTime>
<msgtype ><! [cdata[voice]]></msgtype>
<mediaid><![ Cdata[media_id]]></mediaid>
<format><![ Cdata[format]]></format>
<recognition><![ cdata[Tencent Micro-trust team]]></recognition>
<MsgId>1234567890123456</MsgId>
</xml>

In the field, the format is a speech format, generally amr,recognition for speech recognition results, using UTF8 encoding.
Video Message

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1357290913</CreateTime>
<msgtype ><! [cdata[video]]></msgtype>
<mediaid><![ Cdata[media_id]]></mediaid>
<thumbmediaid><![ cdata[thumb_media_id]]></thumbmediaid>
<MsgId>1234567890123456</MsgId>
</xml >

Small video message

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1357290913</CreateTime>
<msgtype ><! [cdata[shortvideo]]></msgtype>
<mediaid><![ Cdata[media_id]]></mediaid>
<thumbmediaid><![ cdata[thumb_media_id]]></thumbmediaid>
<MsgId>1234567890123456</MsgId>
</xml >

Location messages

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1351776360</CreateTime>
<msgtype ><! [cdata[location]]></msgtype>
<Location_X>23.134521</Location_X>
<location_y >113.358803</Location_Y>
<Scale>20</Scale>
<label><![ cdata[location information]]></label>
<MsgId>1234567890123456</MsgId>
</xml> 

Link message

<xml>
<tousername><![ Cdata[touser]]></tousername>
<fromusername><![ cdata[fromuser]]></fromusername>
<CreateTime>1351776360</CreateTime>
<msgtype ><! [cdata[link]]></msgtype>
<title><![ cdata[Public Platform website Link]]></title>
<description><![ cdata[Public Platform website Link]]></description>
<url><![ cdata[url]]></url>
<MsgId>1234567890123456</MsgId>
</xml> 

Next, look at the Responsexml (poststring) method as follows

 <summary>///Get the message sent by the user///</summary>///<param name= "poststring" ></param> private Voi
 D responsexml (String poststring) {//Use XmlDocument load information structure XmlDocument xmldoc = new XmlDocument ();

 Xmldoc.loadxml (poststring); XmlElement rootelement = xmldoc.documentelement;//Gets the root of the document XmlNode Msgtype = Rootelement.selectsinglenode ("MsgType"); Gets the text type of the message requestxml RequestXML = new RequestXML ()//declares the instance, gets each property and assigns a value Requestxml.tousername = Rootelement.selectsingle Node ("Tousername"). innertext;//public Number requestxml.fromusername = Rootelement.selectsinglenode ("Fromusername"). innertext;//User Requestxml.createtime = Rootelement.selectsinglenode ("Createtime"). innertext;//creation Time Requestxml.msgtype = msgtype.innertext;//message type///assigning to different types of messages if (Requestxml.msgtype = "text" {///Assignment text information content requestxml.content = Rootelement.selectsinglenode ("Content").

 InnerText; } if (RequestXML.MsgType.Trim () = = "Location") {///assigned geographical latitude, longitude, map scaling, geographical 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" {///Assignment event name and event key value Requestxml.eventname = Rootelement.selectsinglenode ("event").
 InnerText; Requestxml.eventkey = Rootelement.selectsinglenode ("Eventkey").

 InnerText; } if (RequestXML.MsgType.Trim (). ToLower () = = "Voice") {///Assignment speech recognition results, must remember in the developer mode, the speech recognition function to open, otherwise get no requestxml.recognition = Rootelement.selectsinglenode ("Recognition").

 InnerText;

} responsemsg (RequestXML);

 }

The speech recognition feature opens as follows:

RequestXML is a class that I created individually that declares a common property field in a message, as follows:

 <summary>///entity class///</summary> public class RequestXML {private String tousername = string for receiving messages.

 Empty;

 <summary>///This public number///</summary> publicly String Tousername{get;set;}

 <summary>///User micro-signal///</summary> public String Fromusername{get;set;}

 <summary>///creation time///</summary> public String Createtime{get;set;}

 <summary>///Information Type///</summary> public String Msgtype{get;set;}

 

 <summary>///Information Content///</summary> public String Content{get;set;}
 /* The following message-specific properties for the event type////<summary>///event name///</summary> public String eventname{get;set;

 


 <summary>///Event Value///</summary> public string Eventkey {get; set;}
 /* The following are the message-specific properties of the type////<summary>///The number of messages///</summary> public int Articlecount {get; set;}
 <summary>///Message title///</summary> public string Title {get; set;} ,Introduction to;summary>///message///</summary> public string Description {get; set;}
 <summary>///message Picture link address///</summary> public string Picurl {get; set;}
 <summary>///Message Details link address///</summary> public string Url {get; set;}

 

 <summary>///Message Collection///</summary> public list<requestxml> articles {get; set;}

 /* The following message-specific properties for the geographic type////<summary>///geographical latitude///</summary> public String location_x {get; set;}

 <summary>///Geographical Longitude///</summary> public String location_y {get; set;}

 <summary>///Map Scaling///</summary> public String Scale {get; set;}

   <summary>///Map Location Description///</summary> public String Label {get; set;}


 
 <summary>///Voice Message-specific field///</summary> public String recognition {get; set;}

 }

Continue to follow the Responsemsg (RequestXML) method as follows

 private void Responsemsg (RequestXML requestxml)
 {
 string msgtype = Requestxml.msgtype;

 Try
 {
 //depending on the message type, what type of message
 switch (msgtype)
 {case
  "text" is sent:
  sendtextcase (RequestXML); Send text message break
  ;
  Case "Event"://Send event message
  if (!string. Isnullorwhitespace (requestxml.eventname) && requestXML.EventName.ToString (). Trim (). Equals ("subscribe"))
  {
  sendwelcomemsg (requestxml);//The message that is returned when you are concerned
  }
  else if (!string. Isnullorwhitespace (requestxml.eventname) && requestXML.EventName.ToString (). Trim ().         Equals ("click"))
  {
  sendeventmsg (requestxml);//Send event message
  } break
  ;

Case "Voice":
  sendvoicemsg (RequestXML);//send voice message break
  ;
  Case "Location"://Send location message
  sendmapmsg (requestxml);
  break;
  Default: Break
  ;

 }
 }
 catch (Exception ex)
 {
 HttpContext.Current.Response.Write (ex. ToString ());
 }
 

First focus on sending text messages, Sendtextcase (requestxml);/Send text messages

 <summary>
 ///Send text
 ///</summary>
 ///<param name= "RequestXML" ></param>
 private void Sendtextcase (RequestXML requestxml)
 {
  String responsecontent = Formattextxml (Requestxml.fromusername, Requestxml.tousername, requestxml.content);

  HttpContext.Current.Response.ContentType = "Text/xml";
  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
  HttpContext.Current.Response.Write (responsecontent);
  HttpContext.Current.Response.End ();
 }

Formattextxml method to make a format

 <summary>
 ///returns formatted XML format content
 ///</summary>
 ///<param name= "P1" > Public number </param>
 ///<param name= "P2" > User number </param>
 ///<param name= "P3" > Reply content </param>
 ///< returns></returns>
 private String Formattextxml (String p1, String p2, string p3)
 {return
 < xml><tousername><! [cdata["+ p1 +"]]></tousername><fromusername><! [cdata["+ p2 +"]]></fromusername><createtime> "+ DateTime.Now.Subtract (New DateTime (1970, 1, 1, 8, 0, 0)) . Totalseconds.tostring () + "</createtime><msgtype><! [cdata[text]]></msgtype><content><! [cdata["+ p3 +"]]></content><funcflag>1</funcflag></xml> ";
 }

 

This will enable the message to be answered, if the user clicks on the button, the following code:

 Case "Event"://Send event message if (!string. Isnullorwhitespace (requestxml.eventname) && requestXML.EventName.ToString (). Trim (). Equals ("subscribe")) {sendwelcomemsg (requestxml);//The message that is returned when you are concerned} else if (!string. Isnullorwhitespace (requestxml.eventname) && requestXML.EventName.ToString (). Trim ().

 Equals ("click")) {sendeventmsg (requestxml);//Send event message} break; <summary>///Send response event message///</summary>///<param name= "RequestXML" ></param> private void S

 Endeventmsg (RequestXML requestxml) {string keystr = RequestXML.EventKey.ToString ();
 Switch (KEYSTR) {case "Mypay": Sendpaydetails (RequestXML);/Send payroll Bill break;
 Case "Tianqiyubao": Sendweatermessage (RequestXML);/send weather forecast break;
 Case "Kaixinyixiao": Sendkaixinmessage (RequestXML);/Send a happy smile result set break;
 Case "Updatemessage": Sendupdatemessage (RequestXML);//Send modify information link break;
 Case "Yuangonghuodong": Sendyuangonghuodong (RequestXML);/Send student activity break; Case "Yuangongtongzhi": SEndyuangongtongzhi (requestxml);/send employee notification break;
 Case "Youwenbida": Sendwenti (RequestXML);//Send employee submit question link break;
 Case "Mywen": Sendwentilist (RequestXML);//Send problem list link break;
 Case "Phoneserices": Sendkefumessage (RequestXML);//access Service break;
  default:string responsecontent = String.Empty; Responsecontent = Formattextxml (Requestxml.fromusername, Requestxml.tousername, "This feature is temporarily open!") Please look forward to it!
  ");
  HttpContext.Current.Response.ContentType = "Text/xml";
  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
  HttpContext.Current.Response.Write (responsecontent);
  HttpContext.Current.Response.End ();
 Break

 }
 }

Sendwelcomemsg (RequestXML);//text message returned when you are concerned

 <summary>
 ///Send attention message
 ///</summary>
 ///<param name= "RequestXML" ></param >
 private void Sendwelcomemsg (RequestXML requestxml)
 {
 String responsecontent = String.Empty;

 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 attention to Chong de Qiao Staff Service Center", "Suzhou Chong de Qiao Human Resources was founded in 2002 ...", Purlfilename, "http://www.deqiaohr.com.cn/weixin/ Wxgsjianjie.aspx "));


 HttpContext.Current.Response.ContentType = "Text/xml";
 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
 HttpContext.Current.Response.Write (responsecontent);
 HttpContext.Current.Response.End ();
 }

  Message_news_main and Message_news_item are text message formatting

 <summary>///Returns the text message principal///</summary> public static string Message_news_main {got {return @ " ;xml> <tousername><! [cdata[{0}]]></tousername> <fromusername><! [cdata[{1}]]></fromusername> <CreateTime>{2}</CreateTime> <msgtype><! [cdata[news]]></msgtype> <ArticleCount>{3}</ArticleCount> <Articles> {4} </articles&
  Gt
 </xml> "; }///<summary>///returns the text message entry///</summary> public static string Message_news_item {got {return @ " <item> <title><! [cdata[{0}]]></title> <description><! [cdata[{1}]]></description> <picurl><! [cdata[{2}]]></picurl> <url><!
 [cdata[{3}]]></url> </item> "; }///<summary>///Send response speech recognition results///</summary>///<param name= "RequestXML" ></param> Priva te void sendvoicemsg (RequestXML requestxml{String responsecontent = Formattextxml (Requestxml.fromusername, Requestxml.tousername, "the speech recognition result you just said is:" + requestx ML.
 Recognition.tostring ());
 HttpContext.Current.Response.ContentType = "Text/xml";
 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
 HttpContext.Current.Response.Write (responsecontent);
 HttpContext.Current.Response.End (); }

Wonderful topic sharing: ASP.net micro-credit Development tutorial Summary, Welcome to learn.

The above is about ASP.net micro-letter development of the second, for message response learning, there will be more updates on the ASP.net micro-letter development of the article, I hope you continue to pay attention.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.