Don’t want to listen to my bullshit, directly look at the official: wechat official documents: mp.weixin.qq.com/wiki?t=reso…
Note before development: we need to use StreamReader
Using system.io;
Now let’s talk about development
Here are some of the rules wechat gave us
We are now going to use these rules to get what we want.
Step 1: Get that string of XML. This is the most important, because without this XML, we have nothing. (Our source of information is this XML.)
Here I use StreamReader to get Request.Body
Because XML is mostly in Request.Body
// Declare an XML convenience call
string xml="";
// Get our stream
var reader = new StreamReader(Request.Body);
// Read the XML from the stream.
var contentFromBody = reader.ReadToEnd();
// Save it into our string for later use
xml = contentFromBody;
Copy the code
We read the information in the XML and turn it into our wechat message object.
Here? I borrowed the code from a big guy:
Several classes that specialize in handling wechat messages
The first class: 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
}
}
Copy the code
The second class:
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;
}
}
}
Copy the code
With these two classes, we have a lot of work to do. If you find any errors, it’s because of the string extension class. Leave the mailbox if you need it. This thing is too long. It is not good to send, but we should all understand, their own write a set.
Use the method in the second class above to convert: XmlToMessageFromWeiXin
We get our MSG object
We have msgType in this object, and the type enumeration is for each event.
We’ll write a switch to separate them. Different events are processed in different branches.
/// <summary>
///Receive messages from wechat
/// </summary>
[HttpPost]
[Route("api/WeChat")]
public string Post(string xml)
{
System.Console.WriteLine("Enter message processing");
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 = "
+ textMsg.FromUserName +
"]]>
+ textMsg.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
+
textMsg.Content + "" The little girl does not understand for the time being. I will reply to you when she is learned in the future. ] ]> ";
break;
case MessageFromWeiXinType.image:
var imgMsg = (ImageMessageFromWeiXin)msg;
System.Console.WriteLine($"{imgMsg.PicUrl}");
Response = "
+ imgMsg.FromUserName +
"]]>
+ imgMsg.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
;
break;
case MessageFromWeiXinType.voice:
var voiceMsg = (VoiceMessageFromWeiXin)msg;
// If voice Recognition is enabled, we can use this field: Recognition
System.Console.WriteLine($" Audio to text:{voiceMsg.Recognition}");
// To download this audio string, we need to use this field: MediaId. According to this field and the multimedia download interface provided by wechat, we can download our audio file. Remember to download it in time
System.Console.WriteLine($"{voiceMsg.MediaId}");
Response = "
+ voiceMsg.FromUserName +
"]]>
+ voiceMsg.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
"
;
break;
case MessageFromWeiXinType.shortvideo:
var shortvideo = (ShortVideoMessageFromWeiXin)msg;
// To download this audio string, we need to use this field: MediaId. According to this field and the multimedia download interface provided by wechat, we can download our video file. Remember to download it in time
System.Console.WriteLine($"小 video download address:{shortvideo.MediaId}");
Response = "
+ shortvideo.FromUserName +
"]]>
+ shortvideo.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
";
break;
case MessageFromWeiXinType.video:
var video = (VideoMessageFromWeiXin)msg;
// To download this audio string, we need to use this field: MediaId. According to this field and the multimedia download interface provided by wechat, we can download our video file. Remember to download it in time
System.Console.WriteLine($"{video.MediaId}");
Response = "
+ video.FromUserName +
"]]>
+ video.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
"
;
break;
case MessageFromWeiXinType.location:
var location = (LocationMessageFromWeiXin)msg;
// Our location is latitude Location_X; Geographic location longitude Location_Y; Map zoom Scale; Location information Label
System.Console.WriteLine($" latitude:{location.Location_X}\ n longitude:{location.Location_Y}\n Map zoom size:{location.Scale}\n Location information:{location.Label}\n");
Response = "
+ location.FromUserName +
"]]>
+ location.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
"
;
break;
case MessageFromWeiXinType.link:
var link = (LinkMessageFromWeiXin)msg;
// Our link is mainly the message Title; Message Description; Message link Url
System.Console.WriteLine($" Message title:{link.Title}\n Message description:{link.Description}\n Message link:{link.Url}\n");
Response = "
+ link.FromUserName +
"]]>
+ link.ToUserName +
"]]></FromUserName> <CreateTime>" +
ResponseClient.ConvertDateTimeToInt(DateTime.Now) +
"
+ link.Title + Address:" + link.Url + ]>
;
break;
}
return Response;
// return "success";
}
catch (Exception ex)
{
return "error"; }}Copy the code
If you don’t understand or need code, please leave your email address
Welcome to qq group communication: 704028989