In this WeChat public platform advanced interface development tutorial, we will introduce how to develop advanced group messaging interface functions.
This article is divided into the following four parts:
Group content can be text, pictures, voice, video, graphics and text. Mass text only requires text content, and other content needs to obtain the corresponding media_id.
The text content is a paragraph of text, such as: ""
The requirements are as follows:
After you are ready, you need to use the upload and download multimedia file interface to upload it to the WeChat server to obtain .
For the development method of uploading, you can refer to the 80th tutorial of this blog
First prepare the thumbnail, the requirements are as follows:
Similarly, use the upload and download multimedia file interface. After uploading to the WeChat server, get the media_id of the thumbnail.
Then you need to use the interface to upload it to the WeChat server. The interface for
An example of the data to be submitted by POST is as follows:
<span { </span>"articles"<span : [ { </span>"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p"<span , </span>"author":"xxx"<span , </span>"title":"Happy Day"<span , </span>"content_source_url":"www.qq.com"<span , </span>"content":"content"<span , </span>"digest":"digest"<span }, { </span>"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p"<span , </span>"author":"xxx"<span , </span>"title":"Happy Day"<span , </span>"content_source_url":"www.qq.com"<span , </span>"content":"content"<span , </span>"digest":"digest"<span } ] }</span>
The parameter description is as follows:
参数 | 是否必须 | 说明 |
---|---|---|
Articles | 是 | 图文消息,一个图文消息支持1到10条图文 |
thumb_media_id | 是 | 图文消息缩略图的media_id,可以在基础支持-上传多媒体文件接口中获得 |
author | 否 | 图文消息的作者 |
title | 是 | 图文消息的标题 |
content_source_url | 否 | 在图文消息页面点击“阅读原文”后的页面 |
content | 是 | 图文消息页面的内容,支持HTML标签 |
digest | 否 | 图文消息的描述 |
根据上述POST结构,定义图文数组如下:
<span $news</span>[] = <span array</span>("thumb_media_id"=><span $thumb_media_id1</span>, "author"=>"", "title"=>"微信公众平台开发", "content_source_url" =>"", "content" =>"", "digest" =>""<span ); </span><span $news</span>[] = <span array</span>("thumb_media_id"=><span $thumb_media_id2</span>, "author"=>"方倍工作室", "title"=>"微信公众平台开发入门教程", "content_source_url" =>"http://m.cnblogs.com/99079/3153567.html?full=1", "content" =>"<span <div> <p>本教程是微信公众平台的入门教程,它将引导你完成如下任务:</p> <ol> <li>1. 创建新浪云计算平台应用</li> <li>2. 启用微信公众平台开发模式</li> <li>3. 基础接口消息及事件</li> <li>4. 微信公众平台PHP SDK</li> <li>5. 微信公众平台开发模式原理</li> <li>6. 开发天气预报功能</li> </ol> </div></span>", "digest" =>"微信公众平台开发经典的入门教程,学习微信公众平台开发必经之路!"<span ); </span><span $news</span>[] = <span array</span>("thumb_media_id"=><span $thumb_media_id3</span>, "author"=>"方倍工作室", "title"=>"微信公众平台开发最佳实践", "content_source_url" =>"http://m.cnblogs.com/?u=txw1958", "content" =>"<p>本书共分10章,案例程序采用广泛流行的PHP、MySQL、XML、CSS、JavaScript、HTML5等程序语言及数据库实现。系统完整地介绍微信公众平台基础接口、自定义菜单、高级接口、微信支付、分享转发等所有相关技术,以生活类、娱乐类、企业类微信开发为切入点,讲解了30多个功能或应用案例。<br>本书按照从简单到复杂,从基础到实践的方式编排,在讲解过程中注重将原理和实践相结合。初学者可以在了解PHP和MySQL语法之后,从头至尾学习,对于其中难以理解的部分可以查阅相关资料,针对企业功能类的开发还需要具有一定的JavaScript、CSS、HTML等编程基础。<br>本书可以作为微信公众平台开发的教程。对于移动互联网及微信公众平台的相关从业人员,本书也具有极大的参考价值。</p>", "digest" =>"微信公众平台开发含金量最高的书籍"<span );</span>
上传图文消息素材的代码则实现如下:
上传成功后,返回如下,将得到图文消息的media_id
<span { </span>"type":"news", "media_id":"CsEf3ldqkAYJAU6EJeIkStVDSvffUJ54vqbThMgplD-VJXXof6ctX5fI6-aYyUiQ", "created_at":1391857799<span }</span>
群发对象可以是用户组,也可以是OpenID列表。
获得用户分组,需要使用高级接口中的查询所有分组接口,获得相应的group_id,
开发方法,可以参考本博客的第88篇教程 微信公众平台开发(88) 用户分组接口
OpenID列表,是使用高级接口中的获取关注者列表接口来实现的。
开发方法,可以参考本博客的第87篇教程 微信公众平台开发(87) 获取关注者列表
由于群发对象的不同,执行群发也有不同的方式。
对用户组群发的接口如下:
https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
要POST的内容按内容不同组成也不一样。
文本:
<span { </span>"filter"<span :{ </span>"group_id":"2"<span }, </span>"text"<span :{ </span>"content":"CONTENT"<span }, </span>"msgtype":"text"<span }</span>
<span { </span>"filter"<span :{ </span>"group_id":"2"<span }, </span>"voice"<span :{ </span>"media_id":"123dsdajkasd231jhksad"<span }, </span>"msgtype":"voice"<span }</span>
<span { </span>"filter"<span :{ </span>"group_id":"2"<span }, </span>"image"<span :{ </span>"media_id":"123dsdajkasd231jhksad"<span }, </span>"msgtype":"image"<span }</span>
<span { </span>"filter"<span :{ </span>"group_id":"2"<span }, </span>"mpvideo"<span :{ </span>"media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc"<span , }, </span>"msgtype":"mpvideo"<span }</span>
<span { </span>"filter"<span :{ </span>"group_id":"2"<span }, </span>"mpnews"<span :{ </span>"media_id":"123dsdajkasd231jhksad"<span }, </span>"msgtype":"mpnews"<span }</span>
相关参数说明如下:
参数 | 是否必须 | 说明 |
---|---|---|
filter | 是 | 用于设定图文消息的接收者 |
group_id | 是 | 群发到的分组的group_id |
mpnews | 是 | 用于设定即将发送的图文消息 |
media_id | 是 | 用于群发的消息的media_id |
msgtype | 是 | 群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video |
title | 否 | 消息的标题 |
description | 否 | 消息的描述 |
thumb_media_id | 是 | 视频缩略图的媒体ID |
其接口实现代码如下所示:
返回结果如下:
<span array</span>(3<span ) { [</span>"errcode"]=><span int(</span>0<span ) [</span>"errmsg"]=> <span string</span>(27) "send job submission success"<span [</span>"msg_id"]=> <span float</span>(2347614963<span ) }</span>
参数说明
参数 | 说明 |
---|---|
type | 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb),图文消息为news |
errcode | 错误码 |
errmsg | 错误信息 |
msg_id | 消息ID |
下面是向用户组发送图文消息的效果,分别是接收到图文消息,查看图文消息内容后的效果,
接口如下:
POST数据示例如下:
文本:
<span { </span>"touser":<span [ </span>"oR5Gjjl_eiZoUpGozMo7dbBJ362A", "oR5Gjjo5rXlMUocSEXKT7Q5RQ63Q"<span ]</span>, "msgtype": "text", "text":<span { </span>"content": "hello from boxer."<span } }</span>
语音:
<span { </span>"touser":<span [ </span>"OPENID1", "OPENID2"<span ]</span>, "voice":<span { </span>"media_id":"mLxl6paC7z2Tl-NJT64yzJve8T9c8u9K2x-Ai6Ujd4lIH9IBuF6-2r66mamn_gIT"<span }</span>, "msgtype":"voice"<span }</span>
图片:
<span { </span>"touser":<span [ </span>"OPENID1", "OPENID2"<span ]</span>, "image":<span { </span>"media_id":"BTgN0opcW3Y5zV_ZebbsD3NFKRWf6cb7OPswPi9Q83fOJHK2P67dzxn11Cp7THat"<span }</span>, "msgtype":"image"<span }</span>
<span { </span>"touser":<span [ </span>"OPENID1", "OPENID2"<span ]</span>, "video":<span { </span>"media_id":"123dsdajkasd231jhksad", "title":"TITLE", "description":"DESCRIPTION"<span }</span>, "msgtype":"video"<span }</span>
图文消息(注意图文消息的media_id需要通过上述方法来得到):
<span { </span>"touser":<span [ </span>"OPENID1", "OPENID2"<span ]</span>, "mpnews":<span { </span>"media_id":"123dsdajkasd231jhksad"<span }</span>, "msgtype":"mpnews"<span }</span>
参数列表
参数 | 是否必须 | 说明 |
---|---|---|
touser | 是 | 填写图文消息的接收者,一串OpenID列表,OpenID最少个,最多10000个 |
mpnews | 是 | 用于设定即将发送的图文消息 |
media_id | 是 | 用于群发的图文消息的media_id |
msgtype | 是 | 群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video |
title | 否 | 消息的标题 |
description | 否 | 消息的描述 |
thumb_media_id | 是 | 视频缩略图的媒体ID |
返回数据示例(正确时的JSON返回结果):
<span { </span>"errcode":0, "errmsg":"send job submission success", "msg_id":<span>2347614964</span><span }</span>
为了能接收群发结果,需要设置公众号助手,结果将推送到绑定的个人微信账号上。其设置如下
群发任务提交后,群发任务可能在一定时间后才完成,因此,群发接口调用时,仅会给出群发任务是否提交成功的提示,若群发任务提交成功,则在群发任务结束时,会向开发者在公众平台填写的开发者URL(callback URL)推送事件。
推送的XML结构如下(发送成功时):
<span <</span><span xml</span><span ></span> <span <</span><span ToUserName</span><span ></span><span <![CDATA[</span><span gh_3e8adccde292</span><span ]]></span><span </</span><span ToUserName</span><span ></span> <span <</span><span FromUserName</span><span ></span><span <![CDATA[</span><span oR5Gjjl_eiZoUpGozMo7dbBJ362A</span><span ]]></span><span </</span><span FromUserName</span><span ></span> <span <</span><span CreateTime</span><span ></span>1394524295<span </</span><span CreateTime</span><span ></span> <span <</span><span MsgType</span><span ></span><span <![CDATA[</span><span event</span><span ]]></span><span </</span><span MsgType</span><span ></span> <span <</span><span Event</span><span ></span><span <![CDATA[</span><span MASSSENDJOBFINISH</span><span ]]></span><span </</span><span Event</span><span ></span> <span <</span><span MsgID</span><span ></span>1988<span </</span><span MsgID</span><span ></span> <span <</span><span Status</span><span ></span><span <![CDATA[</span><span sendsuccess</span><span ]]></span><span </</span><span Status</span><span ></span> <span <</span><span TotalCount</span><span ></span>100<span </</span><span TotalCount</span><span ></span> <span <</span><span FilterCount</span><span ></span>80<span </</span><span FilterCount</span><span ></span> <span <</span><span SentCount</span><span ></span>75<span </</span><span SentCount</span><span ></span> <span <</span><span ErrorCount</span><span ></span>5<span </</span><span ErrorCount</span><span ></span> <span </</span><span xml</span><span ></span>
参数 | 说明 |
---|---|
ToUserName | 公众号的微信号 |
FromUserName | 公众号群发助手的微信号,为mphelper |
CreateTime | 创建时间的时间戳 |
MsgType | 消息类型,此处为event |
Event | 事件信息,此处为MASSSENDJOBFINISH |
MsgID | 群发的消息ID |
Status | 群发的结构,为“send success”或“send fail”或“err(num)”。但send success时,也有可能因用户拒收公众号的消息、系统错误等原因造成少量用户接收失败。err(num)是审核失败的具体原因,可能的情况如下:
err(10001), //涉嫌广告 err(20001), //涉嫌政治 err(20004), //涉嫌社会 err(20002), //涉嫌色情 err(20006), //涉嫌违法犯罪 err(20008), //涉嫌欺诈 err(20013), //涉嫌版权 err(22000), //涉嫌互推(互相宣传) err(21000), //涉嫌其他 |
TotalCount | group_id下粉丝数;或者openid_list中的粉丝数 |
FilterCount | 过滤(过滤是指特定地区、性别的过滤、用户设置拒收的过滤,用户接收已超4条的过滤)后,准备发送的粉丝数,原则上,FilterCount = SentCount + ErrorCount |
SentCount | 发送成功的粉丝数 |
ErrorCount | 发送失败的粉丝数 |
从上可以看出,这其实是公众号群发助手模拟向公众号发送消息,那么群发结果也是返回给公众号助手。
在微信公众平台PHP SDK中增加该事件消息的处理结果如下:
<span private</span> <span function</span> receiveEvent(<span $object</span><span ) { </span><span $content</span> = ""<span ; </span><span switch</span> (<span $object</span>-><span Event) { </span><span case</span> "subscribe": <span $content</span> = "欢迎关注方倍工作室"<span ; </span><span break</span><span ; </span><span case</span> "MASSSENDJOBFINISH": <span $content</span> = "消息ID:".<span $object</span>->MsgID. "\n结果:".<span $object</span>->Status. "\n粉丝数:".<span $object</span>->TotalCount. "\n过滤:".<span $object</span>->FilterCount. "\n发送成功:".<span $object</span>->SentCount. "\n发送失败:".<span $object</span>-><span ErrorCount; </span><span break</span><span ; </span><span default</span>: <span break</span><span ; } </span><span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span ); </span><span return</span> <span $result</span><span ; }</span>
====================================================================
方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码