接收到微信用戶發送過來的消息,我們服務器處理並回應。
需要注意一下,微信說明
對於每一個POST請求,開發者在響應包(Get)中返回特定XML結構,對該消息進行響應(現支持回復文本、圖片、圖文、語音、視頻、音樂)。請注意,回復圖片等多媒體消息時需要預先上傳多媒體文件到微信服務器,只支持認證服務號。
微信服務器在五秒內收不到響應會斷掉連接,並且重新發起請求,總共重試三次,如果在調試中,發現用戶無法收到響應的消息,可以檢查是否消息處理超時。
關於重試的消息排重,有msgid的消息推薦使用msgid排重。事件類型消息推薦使用FromUserName + CreateTime 排重。
假如服務器無法保證在五秒內處理並回復,必須直接回復空串(是指回復一個空字符串,而不是一個XML結構體中content字段的內容為空,請切勿誤解),微信服務器不會對此作任何處理,並且不會發起重試。。這種情況下,可以使用客服消息接口進行異步回復。
請開發者注意,一旦遇到以下情況,微信都會在公眾號會話中,向用戶下發系統提示“該公眾號暫時無法提供服務,請稍后再試”:
1、開發者在5秒內未回復任何內容 2、開發者回復了異常數據,比如JSON數據等
我們無法保證我們服務器能在5秒回應的話,推薦使用異步處理,使用客服消息回復用戶(不討論客服消息次數限制等問題)
WeixinServer
1 #region 靜態全局變量 2 private static Token _Token = null; 3 private static Ticket _Ticket = null; 4 private readonly static string appid = "wx123456789000"; 5 private readonly static string secret = "abcdefghijklmnopqrstuvwxyz"; 6 private readonly static string domain = "http://wechat.mydomain.com"; 7 #endregion
1 // 消息處理 2 public void DisposeMsg(Stream stream) 3 { 4 XmlDocument doc = new XmlDocument(); 5 doc.Load(stream); 6 string type = GetMsgType(doc); 7 string openid = GetMsgOpenid(doc); 8 string access_token = AccessToken(); 9 string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token; 10 11 switch (type) 12 { 13 case "event": 14 { 15 string ev = GetMsgEvent(doc); 16 if (ev == "subscribe") 17 { 18 //關注時,回復用戶消息 19 string data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\": [{\"title\":\"歡迎關注 “XXXX“ \",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/face1.png\"},{\"title\":\"“XXXX” 安卓版下載\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"},{\"title\":\"“XXXX” 蘋果版下載\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"}]}}"; 20 HttpHelper.Post(url, data); 21 } 22 } break; 23 case "text": 24 { 25 string content = GetMsgContent(doc); 26 string data = ""; 27 switch (content) 28 { 29 case "tw": 30 { 31 data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"news\",\"news\":{\"articles\": [{\"title\":\"歡迎關注 “XXXX“ \",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/face1.png\"},{\"title\":\" “XXXX” 安卓版下載\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"},{\"title\":\" “XXXX” 蘋果版下載\",\"description\":\"\",\"url\":\"\",\"picurl\":\"" + domain + "/weixin/msg/jiantou.png\"}]}}"; 32 } break; 33 default: 34 { 35 content = "歡迎關注 “XXXX”\n\nAPP下載:<a href=\\\"\\\">Android</a> <a href=\\\"\\\">IOS</a>"; 36 data = "{\"touser\":\"" + openid + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + content + "\"}}"; 37 } break; 38 } 39 HttpHelper.Post(url, data); 40 } break; 41 default: break; 42 } 43 }
Action里面處理
1 [HttpPost] 2 public ActionResult Access() 3 { 4 //異步操作 使用客服消息接口回復用戶 5 Stream stream = Request.InputStream; 6 AsyncManager.OutstandingOperations.Increment(); 7 var task = Task.Factory.StartNew(() => weixin.DisposeMsg(stream)); 8 task.ContinueWith(t => 9 { 10 //AsyncManager.Parameters["model"] = t.Result; 11 AsyncManager.OutstandingOperations.Decrement(); 12 }); 13 return Content(""); 14 }