官网:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1481187827_i0l21
2018.03.15:GitHub下载代码
订阅号每天可以发一条,服务号每个月只能发4条。本篇的内容跟上篇内容相关,请先看上篇再看着篇。 微信开发 ---- 素材管理
截图对图文消息做了测试
①创建我们的API
②上传图文消息内的图片获取URL 列子中没有用到此接口
③上传图文消息素材
调用: 上传我们的临时素材,
④预览接口
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 预览接口 /// 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版。 /// </summary> /// <param name="access_token"></param> /// <param name="openid">接收消息用户对应该公众号的openid</param> /// <param name="content">图文消息,语音,图片,视频:media_id(与根据分组群发中的media_id相同); 文本:文本消息</param> /// <param name="type"></param> /// <returns></returns> public static dynamic Preview(string access_token, string openid, string content, WxArtcleType type) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token={0}", access_token); var client = new HttpClient(); var builder = new StringBuilder(); builder.Append("{") .AppendFormat("\"touser\":\"{0}\"",openid) .Append(","); switch (type) { case WxArtcleType.News: builder.Append("\"mpnews\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"mpnews\""); break; case WxArtcleType.Text: builder.Append("\"text\":") .Append("{") .AppendFormat("\"content\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"text\""); break; case WxArtcleType.Voice: builder.Append("\"voice\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"voice\""); break; case WxArtcleType.Image: builder.Append("\"image\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"image\""); break; case WxArtcleType.Video: builder.Append("\"video\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"video\""); break; } builder.Append("}"); NetLog.WriteTextLog("Pre", builder.ToString(), DateTime.Now); var result = client.PostAsync(url, new StringContent(builder.ToString())).Result; return JsonHelp.ToDynamic(result.Content.ReadAsStringAsync().Result); }
调用:
运行:
预览效果:
总结:由上传接口得到 media_id -----》上传图文消息,返回 media_id -------》传值进行测试
⑤我们也可以结合素材管理来完成发送
原理:我们的media_id可以是临时的也可以是永久的media_id。注意:上传永久的图文消息需要的是永久的缩略图media_id。
预览效果:
⑥根据OpenID列表群发
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// 根据OpenID列表群发【订阅号不可用,服务号认证后可用】 /// </summary> /// <param name="access_token"></param> /// <param name="content">图文消息,语音,图片,视频:media_id; 文本:文本消息</param> /// <param name="type"></param> /// <param name="touser"></param> /// <returns>success:{"errcode":0,"errmsg":"send job submission success","msg_id":34182}</returns> public static dynamic ReplayOpenids(string access_token, string content, WxArtcleType type, IEnumerable<string> touser, string videoTitle, string videoDesc) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}", access_token); var client = new HttpClient(); var builder = new StringBuilder(); builder.Append("{") .Append("\"touser\":") .Append("["); string aa = ""; foreach (var t in touser) { aa += string.Format("\"{0}\",", t); } aa=aa.TrimEnd(','); builder.Append(aa); builder.Append("],"); switch (type) { case WxArtcleType.News: builder.Append("\"mpnews\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"mpnews\",") .Append("\"send_ignore_reprint\":\"0\""); break; case WxArtcleType.Text: builder.Append("\"text\":") .Append("{") .AppendFormat("\"content\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"text\""); break; case WxArtcleType.Voice: builder.Append("\"voice\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"voice\""); break; case WxArtcleType.Image: builder.Append("\"image\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\"", content) .Append("},") .Append("\"msgtype\":\"image\""); break; case WxArtcleType.Video: builder.Append("\"video\":") .Append("{") .AppendFormat("\"media_id\":\"{0}\",", content) .AppendFormat("\"title\":\"{0}\",", videoTitle) .AppendFormat("\"description\":\"{0}\"", videoDesc) .Append("},") .Append("\"msgtype\":\"video\""); break; } builder.Append("}"); NetLog.WriteTextLog("ReplayOpenids", builder.ToString(), DateTime.Now); var result = client.PostAsync(url, new StringContent(builder.ToString())).Result; return JsonHelp.ToDynamic(result.Content.ReadAsStringAsync().Result); }
还有一些这边就没有写了,如果需要可以根据官网拼接字符串进行测试。