WeChat Official Account Admin Platform Message API Guide

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

Back

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  

 

 

 

时间: 2024-08-23 07:52:49

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

WeChat Official Account Admin Platform API Introduction

Keyword: WeChat API Introduction Message and GeneralAuthor: PondBay Studio[WeChat Developer EXPERT] Address: http://www.cnblogs.com/txw1958/p/wechat-api-introduce.html     WeChat Official Account Admin Platform provides 2 API interfaces, Message and

Android API Guide 之 User Interface笔记

  一.Layouts 1  在XML中,标签名对应于代码中的类名,属性名对应于代码中的方法名 2 android:id="@+id/start" @ 让XML解析器知道后面的字符串应该解析为一个 Resource ID + 表明是自己定义的新的ID,不是系统built-in的, 如果使用系统的应该是这样android:id="@android:id/empty" 3 ViewGroup.LayoutParams 这是一个类,只不过是一个内部静态类,不过在这里把它当

(RabbitMQ) Java Client API Guide

本篇翻译的是RabbitMQ官方文档关于API的内容,原文链接:http://www.rabbitmq.com/api-guide.html.博主对其内容进行大体上的翻译,有些许部分会保留英文,个人觉得这样更加有韵味,如果全部翻译成中文,会存在偏差,文不达意(主要是功力浅薄~~).文章也对部分内容进行一定的解释,增强对相关知识点的理解. Overview RabbitMQ java client uses com.rabbitmq.client as its top-level package,

truncate表后恢复方法总结

truncate表后恢复方法总结   1.1  BLOG文档结构图   1.2  前言部分   1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① truncate操作后的恢复方法(重点)     Tips:        ① 若文章代码格式有错乱,推荐使用QQ.搜狗或360浏览器,也可以下载pdf格式的文档来查看,pdf文档下载地址:http://yunpan.cn/cdEQedhCs2kFz (提取码:

【Android平台】 Alljoyn学习笔记四 Android Core API参考

CORE API GUIDE - ANDROID Prerequisites Install dependencies for the Windows platform, or for the Linux platform. A device running Android OS version 2.2 (Froyo) or greater and running a chip based on the ARM 5 (or greater) instruction set. Importing

A look at WeChat security

原文地址:http://blog.emaze.net/2013/09/a-look-at-wechat-security.html TL;DR: Any (unprivileged) application installed on an Android phone can instruct WeChat to send an hash of your password to an external, attacker-controlled server. If you are a WeChat

Android版本和API Level对应关系

  http://developer.android.com/guide/topics/manifest/uses-sdk-element.html    Platform Version               API Level               VERSION_CODE                       Android 6.0 23 M     Android 5.1 22 LOLLIPOP_MR1     Android 5.0 21 LOLLIPOP     A

Get.Join.Domain.Account.Fail错误码什么意思?怎么解决?

阿里云CloudDesktop出现API报错,错误码是:Get.Join.Domain.Account.Fail Error Message: 描述: 获得加域帐户失败! 详情查看: 阿里云API错误中心 https://error-center.aliyun.com/

Account.Arrearage错误码什么意思?怎么解决?

阿里云Ecs出现API报错,错误码是:Account.Arrearage Error Message: Your account has been in arrears. 描述: 账号已经欠费. 详情查看: 阿里云API错误中心 https://error-center.aliyun.com/