接收 / 返回文本消息
①接收/返回文本消息原理說明
當普通微信用戶向公眾賬號發消息時,微信服務器將post消息的xml數據包到開發者填寫的url上,著手開發之前先行閱讀微信公眾平臺接收普通消息微信開發文檔,對微信的這種消息處理機制有一定了解之后再著手開發(微信開發接收普通消息開發文檔)
注意點:
1、關于重試的消息排重,推薦使用msgid排重。
2、微信服務器在五秒內收不到響應會斷掉連接,并且重新發起請求,總共重試三次。假如服務器無法保證在五秒內處理并回復,可以直接回復空串,微信服務器不會對此作任何處理,并且不會發起重試。詳情請見“”。
3、為了保證更高的安全保障,開發者可以在公眾平臺官網的開發者中心處設置消息加密。開啟加密后,用戶發來的消息會被加密,公眾號被動回復用戶的消息也需要加密(但開發者通過客服接口等api調用形式向用戶發送消息,則不受影響)。關于消息加解密的詳細說明,請見“”。
post到開發者服務器上邊的xml格式為:
1
2
3
4
5
6
7
8
|
< 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 > |
接收消息數據包參數說明:
返回文本消息的xml格式:
1
2
3
4
5
6
7
|
< xml > < tousername > <![cdata[touser]]> </ tousername > < fromusername > <![cdata[fromuser]]> </ fromusername > < createtime >12345678</ createtime > < msgtype > <![cdata[text]]> </ msgtype > < content > <![cdata[你好]]> </ content > </ xml > |
返回文本消息數據包參數說明:
②接收/返回文本消息代碼實現
開發者在自己服務器上邊接收微信服務器post過來的xml數據包接收代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
if (ispostback) { //*********************************自動應答代碼塊********************************* string poststring = string .empty; using (stream stream = httpcontext.current.request.inputstream) { byte [] postbytes = new byte [stream.length]; stream.read(postbytes, 0, (int32)stream.length); //接收的消息為gbk格式 poststring = encoding.getencoding( "gbk" ).getstring(postbytes); string responsecontent = help.returnmessage(poststring ); //返回的消息為utf-8格式 httpcontext.current.response.contentencoding = encoding.utf8; httpcontext.current.response.write(responsecontent); } //********************************自動應答代碼塊end******************************* } |
注意:接收消息的時候要將消息格式轉化為“gbk”格式,否則后邊進行消息解析的時候沒辦法進行有效解析。
returnmessage()處理方法代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/// <summary> /// 統一全局返回消息處理方法 /// </summary> /// <param name="poststr"></param> /// <returns></returns> public string returnmessage( string poststr) { string responsecontent = "" ; xmldocument xmldoc = new xmldocument(); xmldoc.load( new system.io.memorystream(system.text.encoding.getencoding( "gb2312" ).getbytes(poststr))); xmlnode msgtype = xmldoc.selectsinglenode( "/xml/msgtype" ); if (msgtype != null ) { switch (msgtype.innertext) { case "event" : responsecontent = eventhandle(xmldoc); //菜單事件處理 break ; case "text" : responsecontent = texthandle(xmldoc); //文本消息處理 break ; default : break ; } } return responsecontent; } |
texthandle(xmldoc)處理方法代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/// <summary> /// 接受文本消息并回復自定義消息 /// </summary> /// <param name="xmldoc"></param> /// <returns></returns> public string texthandle(xmldocument xmldoc) { string responsecontent = "" ; xmlnode tousername = xmldoc.selectsinglenode( "/xml/tousername" ); xmlnode fromusername = xmldoc.selectsinglenode( "/xml/fromusername" ); xmlnode content = xmldoc.selectsinglenode( "/xml/content" ); if (content != null ) { if (content.innertext == "指定回復消息的自定義文本" ) { responsecontent = string .format(xmltemplate.message_text, fromusername.innertext, tousername.innertext, datetime.now.ticks, "自定義回復消息內容" ); } } return responsecontent; } |
到這里實現功能的代碼演示已完畢,后邊其他的消息處理模式也是根據這種方式在做交互,比如:接收/回復文本消息、圖片消息、語音消息、視頻消息、小視頻消息、地理位置消息、鏈接消息等都可以參照以上代碼進行功能實現。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/likar/p/5247072.html