您好,登录后才能下订单哦!
这篇文章主要介绍“android微信公众号开发实例分析”,在日常操作中,相信很多人在android微信公众号开发实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”android微信公众号开发实例分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
开发前说明:
我们需要用到 StreamReader
所以需要引用:using System.IO;
上面讲了一些微信给我们的规则
我们现在要用这些规则获取我们想要的东西。
第一步:获取那串 XML 这个是最重要的,因为没这个XML,我们就相当于啥都没有。(我们的信息来源就是这个XML)
这里我使用 StreamReader 来获取 Request.Body
因为XML主要是 Request.Body 里面的
//声明一个XML方便调用 string xml=""; //获取我们的流 var reader = new StreamReader(Request.Body); //把流里面的读取出来,就是我们这串XML了。 var contentFromBody = reader.ReadToEnd(); //再保存进我们的这个字符串里,方便后期使用 xml = contentFromBody;
我们读取XML中的信息,并将其转成我们的微信信息对象。
这里呢。我借用了一位大佬的代码:
专门处理微信消息的几个类
第一个类:
MessageFromWeiXin:
using System.Collections.Generic; namespace 命名空间(各位懂的) { /// <summary> /// 来自微信的消息请求:基类 /// </summary> public class MessageFromWeiXin { /// <summary> /// 开发者微信号:接收到消息的公众号帐号 /// </summary> public string ToUserName { get; set; } /// <summary> /// 发送方帐号(一个OpenID,引起这条消息的微信用户) /// </summary> public string FromUserName { get; set; } /// <summary> /// 消息创建时间 (整型) /// </summary> public long CreateTime { get; set; } /// <summary> /// 消息id,64位整型 /// </summary> public long MsgId { get; set; } /// <summary> /// 消息类型 /// </summary> public MessageFromWeiXinType MsgType { get; set; } } #region 普通消息 /// <summary> /// 来自微信的【文本】消息请求 /// </summary> public class TextMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 文本消息内容 /// </summary> public string Content { get; set; } } /// <summary> /// 来自微信的【图片】消息请求 /// </summary> public class ImageMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 图片链接(由系统生成) /// </summary> public string PicUrl { get; set; } /// <summary> /// 图片消息媒体id,可以调用多媒体文件下载接口拉取数据。 /// </summary> public string MediaId { get; set; } } /// <summary> /// 来自微信的【语音】消息请求 /// </summary> public class VoiceMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体 /// </summary> public string MediaId { get; set; } /// <summary> /// 语音格式:amr /// </summary> public string Format { get; set; } /// <summary> /// 语音识别结果,UTF8编码(需要开通此服务) /// </summary> public string Recognition { get; set; } } /// <summary> /// 来自微信的【文本】消息请求 /// </summary> public class VideoMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 视频消息媒体id,可以调用多媒体文件下载接口拉取数据。 /// </summary> public string MediaId { get; set; } /// <summary> /// 视频消息缩略图的媒体id,可以调用 /// </summary> public string ThumbMediaId { get; set; } } /// <summary> /// 来自微信的【小视频】消息请求 /// </summary> public class ShortVideoMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 视频消息媒体id,可以调用多媒体文件下载接口拉取数据。 /// </summary> public string MediaId { get; set; } /// <summary> /// 视频消息缩略图的媒体id,可以调用 /// </summary> public string ThumbMediaId { get; set; } } /// <summary> /// 来自微信的【位置】消息请求 /// </summary> public class LocationMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 地理位置维度【纬度】 /// </summary> public float Location_X { get; set; } /// <summary> /// 地理位置经度 /// </summary> public float Location_Y { get; set; } /// <summary> /// 地图缩放大小 /// </summary> public int Scale { get; set; } /// <summary> /// 地理位置信息 /// </summary> public string Label { get; set; } } /// <summary> /// 来自微信的【位置】消息请求 /// </summary> public class LinkMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 消息标题 /// </summary> public string Title { get; set; } /// <summary> /// 消息描述 /// </summary> public string Description { get; set; } /// <summary> /// 消息链接 /// </summary> public string Url { get; set; } } #endregion #region 事件消息 /// <summary> /// 基本的事件消息:关注,取消关注事件 /// </summary> public class EventMessageFromWeiXin : MessageFromWeiXin { /// <summary> /// 事件类型 /// </summary> public EventMessageFromWeiXinType Event { get; set; } } /// <summary> /// 用户扫码事件:已关注用户事件类型为 SCAN,未关注的用户类型为 subscribe /// </summary> public class SanEventMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id,但这里作为字符串 /// </summary> public string EventKey { get; set; } /// <summary> /// 二维码的ticket,可用来换取二维码图片 /// </summary> public string Ticket { get; set; } } /// <summary> /// 上报地理位置事件 /// </summary> public class LocationEventMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 地理位置纬度 /// </summary> public float Latitude { get; set; } /// <summary> /// 地理位置经度 /// </summary> public float Longitude { get; set; } /// <summary> /// 地理位置精度 /// </summary> public float Precision { get; set; } } /// <summary> /// 点击菜单拉取消息事件 /// </summary> public class ClickEventMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 事件KEY值,与自定义菜单接口中KEY值对应 /// </summary> public string EventKey { get; set; } } /// <summary> /// 点击菜单跳转链接事件 /// </summary> public class ViewEventMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 事件KEY值,设置的跳转URL /// </summary> public string EventKey { get; set; } } /// <summary> /// 群发消息完成事件 /// </summary> public class MassSendJobFinishMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 状态 /// </summary> public string Status { get; set; } /// <summary> /// 标签粉丝数,或OpenId列表中粉丝数 /// </summary> public int TotalCount { get; set; } /// <summary> /// 过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数,原则上,FilterCount = SentCount + ErrorCount /// </summary> public int FilterCount { get; set; } /// <summary> /// 发送成功的粉丝数 /// </summary> public int SentCount { get; set; } /// <summary> /// 发送失败的粉丝数 /// </summary> public int ErrorCount { get; set; } /// <summary> /// 原创校验结果 /// </summary> public CopyrightCheckResult CopyrightCheckResult { get; set; } } /// <summary> /// 原创校验结果 /// </summary> public class CopyrightCheckResult { /// <summary> /// 图文个数 /// </summary> public int Count { get; set; } /// <summary> /// 各个单图文校验结果 /// </summary> public List<CopyrightCheckItem> ResultList { get; set; } /// <summary> /// 1-未被判为转载,可以群发,2-被判为转载,可以群发,3-被判为转载,不能群发 /// </summary> public int CheckState { get; set; } } /// <summary> /// 单图文原创校验结果 /// </summary> public class CopyrightCheckItem { /// <summary> /// 群发文章的序号,从1开始 /// </summary> public int ArticleIdx { get; set; } /// <summary> /// 用户声明文章的状态 /// </summary> public int UserDeclareState { get; set; } /// <summary> /// 系统校验的状态 /// </summary> public int AuditState { get; set; } /// <summary> /// 相似原创文的url /// </summary> public string OriginalArticleUrl { get; set; } /// <summary> /// 相似原创文的类型 /// </summary> public int OriginalArticleType { get; set; } /// <summary> /// 是否能转载 /// </summary> public int CanReprint { get; set; } /// <summary> /// 是否需要替换成原创文内容 /// </summary> public int NeedReplaceContent { get; set; } /// <summary> /// 否需要注明转载来源 /// </summary> public int NeedShowReprintSource { get; set; } } /// <summary> /// 模版消息发送完成事件 /// </summary> public class TEMPLATESENDJOBFINISHMessageFromWeiXin : EventMessageFromWeiXin { /// <summary> /// 状态:success 成功;failed:user block 用户拒绝;failed: system failed 非用户拒绝 /// </summary> public string Status { get; set; } } #endregion /// <summary> /// 微信服务器推送过来的消息类型 /// </summary> public enum MessageFromWeiXinType { /// <summary> /// 未知 /// </summary> unknown = 1, /// <summary> /// 文本消息 /// </summary> text = 2, /// <summary> /// 图片消息 /// </summary> image = 3, /// <summary> /// 语音消息 /// </summary> voice = 4, /// <summary> /// 视频消息 /// </summary> video = 5, /// <summary> /// 小视频消息 /// </summary> shortvideo = 6, /// <summary> /// 地里位置消息 /// </summary> location = 7, /// <summary> /// 连接消息 /// </summary> link = 8, /// <summary> /// 事件推送消息 /// </summary> @event = 9 } /// <summary> /// 微信服务器推送过来的【事件】消息的子类型 /// </summary> public enum EventMessageFromWeiXinType { /// <summary> /// 未知 /// </summary> unknown = 1, /// <summary> /// 订阅事件;未关注用户扫码关注也是这个类型,但数据结构与SCAN类型相同,此时相当于两个类型的合并 /// </summary> subscribe = 2, /// <summary> /// 取消订阅事件 /// </summary> unsubscribe = 3, /// <summary> /// 扫描带参数二维码事件 /// </summary> SCAN = 4, /// <summary> /// 上报地理位置事件 /// </summary> LOCATION = 5, /// <summary> /// 点击自定义菜单拉取消息事件 /// </summary> CLICK = 6, /// <summary> /// 点击自定义菜单跳转链接事件 /// </summary> VIEW = 7, /// <summary> /// 群发消息发送结束事件 /// </summary> MASSSENDJOBFINISH = 8, /// <summary> /// 模板消息发送结束事件 /// </summary> TEMPLATESENDJOBFINISH = 9 } }
第二个类:
ResponseClient:
using System.Xml; using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Xml; namespace 命名空间(各位懂的) { /// <summary> /// XML消息识别 /// </summary> public static class ResponseClient { /// <summary> /// 判断消息类型 /// </summary> public static MessageFromWeiXinType GetMsgType(string msg) { var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(msg); if (TryGetFirstTagText(ref xmlDocument, "MsgType", out var txt) && Enum.TryParse(txt, out MessageFromWeiXinType Type)) return Type; return MessageFromWeiXinType.unknown; } /// <summary> /// XML转消息对象 /// </summary> public static MessageFromWeiXin XmlToMessageFromWeiXin(string xml) { xml = xml.Replace("< !", "<!").Replace("] ]","]]"); var xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xml); var txt = string.Empty; var @int = 0; if (TryGetFirstTagText(ref xmlDocument, "MsgType", out txt) && Enum.TryParse(txt, out MessageFromWeiXinType messageType)) { MessageFromWeiXin msg = null; switch (messageType) { case MessageFromWeiXinType.text: //文本消息 msg = new TextMessageFromWeiXin { MsgType = MessageFromWeiXinType.text, Content = TryGetFirstTagText(ref xmlDocument, "Content", out txt) ? txt : "" }; break; case MessageFromWeiXinType.image: //图片消息 msg = new ImageMessageFromWeiXin { MsgType = MessageFromWeiXinType.image, MediaId = TryGetFirstTagText(ref xmlDocument, "MediaId", out txt) ? txt : "", PicUrl = TryGetFirstTagText(ref xmlDocument, "PicUrl", out txt) ? txt : "" }; break; case MessageFromWeiXinType.voice: //语音消息 msg = new VoiceMessageFromWeiXin { MsgType = MessageFromWeiXinType.voice, MediaId = TryGetFirstTagText(ref xmlDocument, "MediaId", out txt) ? txt : "", Recognition = TryGetFirstTagText(ref xmlDocument, "Recognition", out txt) ? txt : "", Format = TryGetFirstTagText(ref xmlDocument, "Format", out txt) ? txt : "" }; break; case MessageFromWeiXinType.video: //视频消息 msg = new VideoMessageFromWeiXin { MsgType = MessageFromWeiXinType.video, MediaId = TryGetFirstTagText(ref xmlDocument, "MediaId", out txt) ? txt : "", ThumbMediaId = TryGetFirstTagText(ref xmlDocument, "ThumbMediaId", out txt) ? txt : "" }; break; case MessageFromWeiXinType.shortvideo: //小视频消息 msg = new ShortVideoMessageFromWeiXin { MsgType = MessageFromWeiXinType.shortvideo, MediaId = TryGetFirstTagText(ref xmlDocument, "MediaId", out txt) ? txt : "", ThumbMediaId = TryGetFirstTagText(ref xmlDocument, "ThumbMediaId", out txt) ? txt : "" }; break; case MessageFromWeiXinType.link: //链接消息 msg = new LinkMessageFromWeiXin { MsgType = MessageFromWeiXinType.link, Description = TryGetFirstTagText(ref xmlDocument, "Description", out txt) ? txt : "", Title = TryGetFirstTagText(ref xmlDocument, "Title", out txt) ? txt : "", Url = TryGetFirstTagText(ref xmlDocument, "Url", out txt) ? txt : "" }; break; case MessageFromWeiXinType.location: //位置消息 msg = new LocationMessageFromWeiXin { MsgType = MessageFromWeiXinType.location, Label = TryGetFirstTagText(ref xmlDocument, "Label", out txt) ? txt : "", Location_X = TryGetFirstTagText(ref xmlDocument, "Location_X", out txt) ? Convert.ToSingle(txt) : 0, Location_Y = TryGetFirstTagText(ref xmlDocument, "Location_Y", out txt) ? Convert.ToSingle(txt) : 0, Scale = TryGetFirstTagText(ref xmlDocument, "Scale", out txt) ? Convert.ToInt32(txt) : 0 }; break; case MessageFromWeiXinType.@event: //事件消息 if (TryGetFirstTagText(ref xmlDocument, "Event", out txt)) { var Event = Enum.TryParse(txt, out EventMessageFromWeiXinType eventMessageFromWeiXinType) ? eventMessageFromWeiXinType : EventMessageFromWeiXinType.unknown; switch (Event) { case EventMessageFromWeiXinType.subscribe: //关注事件 msg = new EventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.subscribe }; break; case EventMessageFromWeiXinType.unsubscribe: //取消关注事件 msg = new EventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.unsubscribe }; break; case EventMessageFromWeiXinType.SCAN: //扫码 msg = new SanEventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.SCAN, EventKey = TryGetFirstTagText(ref xmlDocument, "EventKey", out txt) ? txt : "", Ticket = TryGetFirstTagText(ref xmlDocument, "Ticket", out txt) ? txt : "" }; break; case EventMessageFromWeiXinType.CLICK: //点击自定义菜单等待消息响应 msg = new ClickEventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.CLICK, EventKey = TryGetFirstTagText(ref xmlDocument, "EventKey", out txt) ? txt : "" }; break; case EventMessageFromWeiXinType.VIEW: //点击自定义菜单跳转到页面 msg = new ViewEventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.VIEW, EventKey = TryGetFirstTagText(ref xmlDocument, "EventKey", out txt) ? txt : "" }; break; case EventMessageFromWeiXinType.LOCATION: //授权取得位置事件 msg = new LocationEventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.LOCATION, Latitude = TryGetFirstTagText(ref xmlDocument, "Latitude", out txt) ? Convert.ToSingle(txt) : 0, Longitude = TryGetFirstTagText(ref xmlDocument, "Longitude", out txt) ? Convert.ToSingle(txt) : 0, Precision = TryGetFirstTagText(ref xmlDocument, "Precision", out txt) ? Convert.ToSingle(txt) : 0 }; break; case EventMessageFromWeiXinType.MASSSENDJOBFINISH: //群发消息发送完成事件 var items = xmlDocument.GetElementsByTagName("item"); var itemsList = new List<CopyrightCheckItem>(); foreach (XmlNode i in items) { var item = new CopyrightCheckItem(); foreach (XmlNode j in i.ChildNodes) switch (j.Name) { case "ArticleIdx": item.ArticleIdx = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "UserDeclareState": item.UserDeclareState = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "AuditState": item.AuditState = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "OriginalArticleUrl": item.OriginalArticleUrl = j.InnerText; break; case "OriginalArticleType": item.OriginalArticleType = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "CanReprint": item.CanReprint = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "NeedReplaceContent": item.NeedReplaceContent = int.TryParse(j.InnerText, out @int) ? @int : 0; break; case "NeedShowReprintSource": item.NeedShowReprintSource = int.TryParse(j.InnerText, out @int) ? @int : 0; break; } } msg = new MassSendJobFinishMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.MASSSENDJOBFINISH, Status = TryGetFirstTagText(ref xmlDocument, "Status", out txt) ? txt : "", TotalCount = TryGetFirstTagText(ref xmlDocument, "TotalCount", out txt) ? Convert.ToInt32(txt) : 0, FilterCount = TryGetFirstTagText(ref xmlDocument, "FilterCount", out txt) ? Convert.ToInt32(txt) : 0, SentCount = TryGetFirstTagText(ref xmlDocument, "SentCount", out txt) ? Convert.ToInt32(txt) : 0, ErrorCount = TryGetFirstTagText(ref xmlDocument, "ErrorCount", out txt) ? Convert.ToInt32(txt) : 0, CopyrightCheckResult = new CopyrightCheckResult { Count = TryGetFirstTagText(ref xmlDocument, "Count", out txt) ? Convert.ToInt32(txt) : 0, CheckState = TryGetFirstTagText(ref xmlDocument, "CheckState", out txt) ? Convert.ToInt32(txt) : 0, ResultList = itemsList } }; break; case EventMessageFromWeiXinType.TEMPLATESENDJOBFINISH: //模版消息发送完成事件 msg = new TEMPLATESENDJOBFINISHMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.TEMPLATESENDJOBFINISH, Status = TryGetFirstTagText(ref xmlDocument, "", out txt) ? txt : "" }; break; default: //其他事件 msg = new EventMessageFromWeiXin { MsgType = MessageFromWeiXinType.@event, Event = EventMessageFromWeiXinType.unknown }; break; } } break; default: //未知消息 msg = new MessageFromWeiXin {MsgType = MessageFromWeiXinType.unknown}; break; } if (msg.MsgType != MessageFromWeiXinType.unknown) { if (TryGetFirstTagText(ref xmlDocument, "CreateTime", out txt)) msg.CreateTime = Convert.ToInt32(txt); if (TryGetFirstTagText(ref xmlDocument, "FromUserName", out txt)) msg.FromUserName = txt; if (TryGetFirstTagText(ref xmlDocument, "MsgId", out txt)) msg.MsgId = Convert.ToInt64(txt); if (TryGetFirstTagText(ref xmlDocument, "ToUserName", out txt)) msg.ToUserName = txt; return msg; } } return null; } /// <summary> /// 尝试取得XML中第一个指定标签的文本 /// </summary> /// <param name="xml">xml文档对象</param> /// <param name="tag">指定的标签名</param> /// <param name="txt">取得的文本</param> /// <returns>是否成功取得</returns> public static bool TryGetFirstTagText(ref XmlDocument xml, string tag, out string txt) { txt = string.Empty; var Node = xml.GetElementsByTagName(tag); if (Node.Count > 0) { txt = Node[0].InnerText; if (!string.IsNullOrWhiteSpace(txt)) return true; } return false; } /// <summary> /// 验证微信接口 /// </summary> /// <param name="echostr"></param> /// <param name="nonce"></param> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="token"></param> /// <returns></returns> public static bool VerificationSignature(string echostr, string nonce, string signature, string timestamp, string token) { string[] StrArray = {token, timestamp, nonce}; Array.Sort(StrArray); var ArrayString = new StringBuilder(); for (var i = 0; i < StrArray.Length; i++) ArrayString.Append(StrArray[i]); var strHash = GetSha1Hash(ArrayString.ToString()); return signature == strHash; } /// <summary> /// 哈希1 /// </summary> /// <param name="input"></param> /// <returns></returns> public static string GetSha1Hash(string input) { var inputBytes = Encoding.Default.GetBytes(input); SHA1 sha = new SHA1CryptoServiceProvider(); var result = sha.ComputeHash(inputBytes); var sBuilder = new StringBuilder(); for (var i = 0; i < result.Length; i++) sBuilder.Append(result[i].ToString("x2")); return sBuilder.ToString(); } /// <summary> /// 时间转时间戳 /// </summary> public static long ConvertDateTimeToInt(DateTime dateTime) { var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); var t = (dateTime.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; } } }
有了这两个类,我们的东西就好开展了,如果大家有发现报错的话,是因为string扩展类的问题。需要的留下邮箱,这个东西太长了。不好发,不过大家应该都看的懂,自己写一套也好。
使用上面第二个类中的,这个方法,进行转换 : XmlToMessageFromWeiXin
得到我们 msg 对象
我们这个对象里面有msgtype,这个type枚举呢,就对应了各个事件。
我们写个switch就能将他们分开了。不同事件进入不同分支处理。
/// <summary> /// 接收来自微信的消息 /// </summary> [HttpPost] [Route("api/WeChat")] public string Post(string xml) { System.Console.WriteLine("进入消息处理"); var reader = new StreamReader(Request.Body); var contentFromBody = reader.ReadToEnd(); System.Console.WriteLine($"xml:{contentFromBody}"); xml = contentFromBody; try { var msg = ResponseClient.XmlToMessageFromWeiXin(xml); var Response = ""; switch (msg.MsgType) { case MessageFromWeiXinType.text: var textMsg = (TextMessageFromWeiXin)msg; Response = "<xml> <ToUserName><![CDATA[" + textMsg.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + textMsg.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您说“" + textMsg.Content + "”,小女子暂时还不明白,等小女子日后博学之时再来答复您。]]></Content> </xml>"; break; case MessageFromWeiXinType.image: var imgMsg = (ImageMessageFromWeiXin)msg; System.Console.WriteLine($"图片地址:{imgMsg.PicUrl}"); Response = "<xml> <ToUserName><![CDATA[" + imgMsg.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + imgMsg.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一张 图片,小女子已经帮您保存起来了]]></Content> </xml>"; break; case MessageFromWeiXinType.voice: var voiceMsg = (VoiceMessageFromWeiXin)msg; //如果有开通语音识别的功能,我们可以使用这个字段:Recognition System.Console.WriteLine($"音频转文字:{voiceMsg.Recognition}"); //我们下载这串语音的话就要使用这个字段:MediaId,根据这个字段加上微信提供的多媒体下载接口 ,下载 我们的音频文件。记得及时下载哟 System.Console.WriteLine($"音频下载地址:{voiceMsg.MediaId}"); Response = "<xml> <ToUserName><![CDATA[" + voiceMsg.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + voiceMsg.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一串 语音,小女子已经帮您保存起来了]]></Content> </xml>"; break; case MessageFromWeiXinType.shortvideo: var shortvideo = (ShortVideoMessageFromWeiXin)msg; //我们下载这串语音的话就要使用这个字段:MediaId,根据这个字段加上微信提供的多媒体下载接口 ,下载 我们的视频文件。记得及时下载哟 System.Console.WriteLine($"小视频下载地址:{shortvideo.MediaId}"); Response = "<xml> <ToUserName><![CDATA[" + shortvideo.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + shortvideo.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一个小视频,小女子已经帮您保存起来了]]></Content> </xml>"; break; case MessageFromWeiXinType.video: var video = (VideoMessageFromWeiXin)msg; //我们下载这串语音的话就要使用这个字段:MediaId,根据这个字段加上微信提供的多媒体下载接口 ,下载 我们的视频文件。记得及时下载哟 System.Console.WriteLine($"视频下载地址:{video.MediaId}"); Response = "<xml> <ToUserName><![CDATA[" + video.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + video.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一个视频,小女子已经帮您保存起来了]]></Content> </xml>"; break; case MessageFromWeiXinType.location: var location = (LocationMessageFromWeiXin)msg; //我们的地址呢,主要有 地理位置纬度 Location_X ; 地理位置经度 Location_Y ; 地图缩放大小 Scale; 地理位置信息 Label System.Console.WriteLine($"纬度:{location.Location_X}\n经度:{location.Location_Y}\n地图缩放大小:{location.Scale}\n地理位置信息:{location.Label}\n"); Response = "<xml> <ToUserName><![CDATA[" + location.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + location.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一个 位置信息,小女子已经帮您记录下来了]]></Content> </xml>"; break; case MessageFromWeiXinType.link: var link = (LinkMessageFromWeiXin)msg; //我们的链接呢,主要有 消息标题 Title ;消息描述 Description ;消息链接 Url System.Console.WriteLine($"消息标题:{link.Title}\n消息描述:{link.Description}\n消息链接:{link.Url}\n"); Response = "<xml> <ToUserName><![CDATA[" + link.FromUserName + "]]></ToUserName> <FromUserName><![CDATA[" + link.ToUserName + "]]></FromUserName> <CreateTime>" + ResponseClient.ConvertDateTimeToInt(DateTime.Now) + "</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[您发了一个 链接:标题:" + link.Title + " , 地址: " + link.Url + " ,小女子已经帮您记录下来了]]></Content> </xml>"; break; } return Response; // return "success"; } catch (Exception ex) { return "error"; } }
到此,关于“android微信公众号开发实例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。