asp.net微信开发(消息应答)(3)

private void ResponseMsg(RequestXML requestXML) { string MsgType = requestXML.MsgType; try { //根据消息类型判断发送何种类型消息 switch (MsgType) { case "text": SendTextCase(requestXML);//发送文本消息 break; case "event"://发送事件消息 if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe")) { SendWelComeMsg(requestXML);//关注时返回的图文消息 } else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK")) { SendEventMsg(requestXML);//发送事件消息 } break;          case "voice": SendVoiceMsg(requestXML);//发送语音消息 break; case "location"://发送位置消息 SendMapMsg(requestXML); break; default: break; } } catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); } }

先来关注发送文本消息,SendTextCase(requestXML);//发送文本消息

/// <summary> /// 发送文本 /// </summary> /// <param></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方法制定格式

/// <summary> /// 返回格式化的Xml格式内容 /// </summary> /// <param>公众号</param> /// <param>用户号</param> /// <param>回复内容</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>"; }

这样就能实现消息的应答,如果用户点击的按钮,如下代码:

case "event"://发送事件消息 if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("subscribe")) { SendWelComeMsg(requestXML);//关注时返回的图文消息 } else if (!string.IsNullOrWhiteSpace(requestXML.EventName) && requestXML.EventName.ToString().Trim().Equals("CLICK")) { SendEventMsg(requestXML);//发送事件消息 } break; /// <summary> /// 发送响应事件消息 /// </summary> /// <param></param> private void SendEventMsg(RequestXML requestXML) { string keyStr = requestXML.EventKey.ToString(); switch (keyStr) { case "mypay": SendPayDetails(requestXML);//发送薪资账单 break; case "tianqiyubao": SendWeaterMessage(requestXML);//发送天气预报 break; case "kaixinyixiao": SendKaiXinMessage(requestXML);//发送开心一笑结果集 break; case "updateMessage": SendUpdateMessage(requestXML);//发送修改信息链接 break; case "yuangonghuodong": SendYuanGongHuoDong(requestXML);//发送学生活动 break; case "yuangongtongzhi": SendYuanGongTongZhi(requestXML);//发送员工通知 break; case "youwenbida": SendWenti(requestXML);//发送员工提交问题链接 break; case "mywen": SendWentiList(requestXML);//发送问题列表链接 break; case "PhoneSerices": SendKeFuMessage(requestXML);//接入客服 break; default: String responseContent = String.Empty; responseContent = FormatTextXML(requestXML.FromUserName, requestXML.ToUserName,"此功能暂未开放!敬请期待!"); HttpContext.Current.Response.ContentType = "text/xml"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(responseContent); HttpContext.Current.Response.End(); break; } }

 SendWelComeMsg(requestXML);//关注时返回的图文消息

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwjdf.html