Keyword: WeChat Message API Text Image Location Link Event Music RichMedia
Author: PondBay Studio[WeChat Developer EXPERT]
Address:  http://www.cnblogs.com/txw1958/p/WeChat-Message-API.html

Guide for Message API

Contents

[hide]

Introduction

The WeChat Official Account Admin Platform provides developers with a Message API to manage incoming messages and reply logic.

Apply for Message API

Click Apply and fill in a name, phone and email for a contact person, as well as a URL pointing to your server and a token. The token is used to generate a signature for communication between your app and WeChat.

URL Access

When the application is submitted, a GET request will be sent to the URL provided above with the 4 parameters below:

Parameter Description
signature signature for communication encryption
timestamp time stamp
nonce a random number
echostr a random string

You should check whether the HTTP request is from WeChat by verifying the signature. If the signature is correct, you should return the echostr.

The signature will be generated in the following way using the token (that you provided), timestamp and nonce.

1. Sort the 3 values of token, timestamp and nonce alphabetically.
2. Combine the 3 parameters into one string, encrypt it using SHA-1.
3. Compare the SHA-1 digest string with the signature from the request. If they are the same, the access request is from WeChat.

Pushing Messages

When a WeChat user sends a message to an Official Account, WeChat Official Account Admin Platform will POST it to you via the URL you provided.

Text messages

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
Parameter Description
ToUserName WeChat ID of your app
FromUserName a unique ID for the sender
CreateTime create time of the message
MsgType message type ("text" for text messages)
Content message contents
MsgId a unique ID for the message (64 bit integer)

Image messages

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]></PicUrl>
<MsgId>1234567890123456</MsgId>
</xml>
Parameter Description
ToUserName WeChat ID of your app
FromUserName a unique ID for the sender
CreateTime create time of the message
MsgType message type ("image" for image messages)
PicUrl URL for the image
MsgId a unique ID for the message (64 bit integer)

Location data messages

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1351776360</CreateTime>
<MsgType><![CDATA[location]]></MsgType>
<Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y>
<Scale>20</Scale>
<Label><![CDATA[location]]></Label>
<MsgId>1234567890123456</MsgId>
</xml>
Parameter Description
ToUserName WeChat ID of your app
FromUserName a unique ID for the sender
CreateTime create time of the message
MsgType message type ("location" for location messages)
Location_X latitude of the location
Location_Y longitude of the location
Scale scale of the map
Label location description
MsgId a unique ID for the message (64 bit integer)

Link messages

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType><![CDATA[link]]></MsgType>
<Title><![CDATA[WeChat Official Account Platform portal]]></Title>
<Description><![CDATA[The URL of the portal]]></Description>
<Url><![CDATA[url]]></Url>
<MsgId>1234567890123456</MsgId>
</xml>
Parameter Description
ToUserName WeChat ID of your app
FromUserName a unique ID for the sender
CreateTime create time of the message
MsgType message type ("link" for link messages)
Title title of the message
Description description of the message
Url url which is sent to users
MsgId a unique ID for the message (64 bit integer)

Event messages

 <xml><ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[EVENT]]></Event>
<EventKey><![CDATA[EVENTKEY]]></EventKey>
</xml>
Parameter Description
ToUserName WeChat ID of your app
FromUserName a unique ID for the sender
CreateTime create time of the message
MsgType message type ("event" for event messages)
Event event type, currently we have 3 types: subscribe, unsubscribe, CLICK(coming soon)
EventKey for future usage

Messages Replies

You can reply to incoming messages. Now the platform supports different kinds of messages, including text, image, voice, video and music. You can also do the operation 'add to my favorites'.

If you fail to perform your response within 5 seconds, we will close the connection.

The data structure for a reply message:

Text messages

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[content]]></Content>
<FuncFlag>0</FuncFlag>
</xml>
Parameter Description
ToUserName a unique ID for the receiver, you can get it from the request
FromUserName WeChat ID of your app
CreateTime create time of the message, the type is integer
MsgType message type ("text" for text messages)
Content reply message contents
FuncFlag add a star for the message if the bit (0x0001) is set

Music message

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[TITLE]]></Title>
<Description><![CDATA[DESCRIPTION]]></Description>
<MusicUrl><![CDATA[MUSIC_Url]]></MusicUrl>
<HQMusicUrl><![CDATA[HQ_MUSIC_Url]]></HQMusicUrl>
<ThumbMediaId><![CDATA[media_id]]></ThumbMediaId>
</Music>
<FuncFlag>0</FuncFlag>
</xml>
Parameter Description
ToUserName a unique ID for the receiver, you can get it from the request
FromUserName WeChat ID of your app
CreateTime create time of the message, the type is integer
MsgType message type("music" for music messages)
MusicUrl URL for the music
HQMusicUrl URL for high quality, WeChat will access it when using WiFi
ThumbMediaId OPTIONAL. You will get this ID after you upload the thumb (80*80) by using the API
FuncFlag add a star for the message if the bit (0x0001) is set

Rich media messages

 <xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title>
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml>
Parameter Description
ToUserName a unique ID for the receiver, you can get it from the request
FromUserName WeChat ID of your app
CreateTime create time of the message, the type is integer
MsgType message type ("news" for rich media messages)
ArticleCount quantity of rich media messages (no larger than 10)
Articles contents of rich media messages. The first item will be displayed in large image by default.
Title title of the rich media message
Description description of the rich media message
PicUrl URL of images in the rich media message. Domain name of this URL should be the same as the one of URL provided in basic info. Recommended image size: 640*320 (large image); 80*80 (small image)
Url redirection link of the rich media message

Notes

1. One user's unique ID is different for different Official Accounts.

2. Recommend you use port 80 for your app server.

Sample code

PHP:Download

PondBay Studio is the Expert of WeChat Development, and has a lots of cases in China, Japan, Europe and US.  The Founder has served the top companies such as China HuaWei and Israel Company.  You can Contact US via email

WeChat Official Account Admin Platform Message API Guide的更多相关文章

  1. WeChat Official Account Admin Platform API Introduction

    Keyword: WeChat API Introduction Message and GeneralAuthor: PondBay Studio[WeChat Developer EXPERT] ...

  2. Django REST Framework API Guide 08

    1.Filtering 2.Pagination FIltering GenericAPIView的子类筛选queryset的简单方法是重写.get_quueryset()方法. 1.根据当前用户进行 ...

  3. Django REST Framework API Guide 01

    之前按照REST Framework官方文档提供的简介写了一系列的简单的介绍博客,说白了就是翻译了一下简介,而且翻译的很烂.到真正的生产时,就会发现很鸡肋,连熟悉大概知道rest framework都 ...

  4. Story of Jerry Wang's Wechat subscription account

    As an SAP Techinical Ambassador,Jerry is always willing to share his SAP expertise to various social ...

  5. TrineaAndroidCommon API Guide

    android-common-lib 关于我,欢迎关注微博:Trinea    主页:trinea.cn    邮箱:trinea.cn#gmail.com    微信:codek2 主要包括:缓存( ...

  6. Django REST Framework API Guide 03

    本节大纲 1.Routers 2.Parsers 3.Renderers Routers Usage from rest_framework import routers router = route ...

  7. Django REST Framework API Guide 06

    本节大纲 1.Validators 2.Authentication Validators 在REST框架中处理验证的大多数时间,您将仅仅依赖于缺省字段验证,或在序列化器或字段类上编写显式验证方法.但 ...

  8. Django REST Framework API Guide 04

    本节大纲 1.serializers 1.Serializers Serializers允许复杂的数据,像queryset和模型实例转换成源生的Python数据类型.从而可以更简单的被渲染成JSON, ...

  9. Django REST Framework API Guide 07

    本节大纲 1.Permissions 2.Throttling Permissions 权限是用来授权或者拒绝用户访问API的不同部分的不同的类的.基础的权限划分 1.IsAuthenticated ...

随机推荐

  1. iOS 开发中遇到的问题

    1. 关于纠结很久的KVO崩溃问题,其真正原因是,在删除roomItem的KVO之前,将这个对象已经赋值为nil,所以实际上并没有删除他的observer,因此而崩溃:长时间纠结的原因是受.cxx_d ...

  2. 实例学习写Makefile文件

    目录 0. 扫盲 1. 编译,链接 2. Makefile文件执行 3. Makefile书写规则 4. 案例 5. Makefile是如何工作的 6. 拔高,参考 0. 扫盲 Linux 环境下的程 ...

  3. fiddler for mac

    Fiddler 是一免费的web调试工具.并且兼容所有浏览器.系统和平台. Fiddler 是基于微软的 .Net 技术开发的,没办法直接在 Mac/Linux 下使用.本文介绍一些替代方案(这些方案 ...

  4. sdut 2840 Best string Orz~ (dp)

    题目 题意:有n1个o, n2个r, n3个z, n4个~, 求有多少种组合使 组合出来的字符串的任意前缀都满足 o的个数>=r的个数, r的个数>=z的个数 …………………… 思路:递推 ...

  5. JAVA中获取工程路径的方法

    在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...

  6. uva12169 Disgruntled Judge

    扩展欧几里得. 枚举a,根据x1,x3和递推式可得. (a+1)*b-k*mod=f[3]-a*a*b. 通过扩展欧几里得求出b. 带入原式进行计算. #include<cstdio> # ...

  7. MSSQL中把表中的数据导出成Insert

    use master go if exists (select name from sysobjects where name = 'sp_generate_insert_script') begin ...

  8. bzoj2326: [HNOI2011]数学作业

    矩阵快速幂,分1-9,10-99...看黄学长的代码理解...然而他直接把答案保存在最后一行(没有说明...好吧应该是我智障这都不知道... #include<cstdio> #inclu ...

  9. 无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败。

    无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败. 在Asp.Net项目中使用WebClient或HttpWebRequest时出现以上错误 解决方案:把以下代码放在 ...

  10. PhoneGap 安装体验

    npm -v #显示版本,检查npm 是否正确安装. npm install express #安装express模块 npm install -g express #加上 -g 启用global安装 ...