zoukankan      html  css  js  c++  java
  • 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

    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:

    ParameterDescription
    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>
    
    ParameterDescription
    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>
    
    ParameterDescription
    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> 
    
    ParameterDescription
    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> 
    
    ParameterDescription
    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>
    
    ParameterDescription
    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>
    
    ParameterDescription
    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>
    
    ParameterDescription
    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> 
    
    ParameterDescription
    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  

  • 相关阅读:
    Year Outline stat Detail stat 1987--1996----1999 C:UsersATIDocuments00drmmr v2 tafdrmmr1987-20
    atitit 2010 2010 diary log events memorabilia v3 taf .docx No finish , wait to finish 1.6 yLu
    Atitit 标记语言ML(Markup Language) v4 目录 1. 标记语言ML Markup Language 1 1.1. 简介 1 2. 置标语言置标语言通常可以分为三类:标识性的
    Atitit 2001drmmr v1 t05.docx 1.1shoeho kh majyao n chfe ,bg n rjywel ycyi ,shwa leihaivvei yaopao
    Atitit nlp重要节点 v3 目录 1. 语法分析重点 节点余额365个 1 2. nlp词性表 2 2.1. 词语分类13类 2 2.2. 副词 约20个 3 2.3. 代词30个 3 2
    Atitit 提升语法级别4gl 4.5g 4.9g 5g 目录 1. 语言级别表 1 2. 4.9g实现细节 2 2.1. $dollor前导符 2 2.2. Static变量 2 2.3. S
    Atitit 工程师程序员技术级别对应表与主要特征 P1--p6 说明 类别 职称 对应技术标志 P5 高级工程师 工程师类 一般四五年 P6 资深开发 工程师类 78年经历 P7 P7
    Atitit 自然语言与人工语言的语法构建ast的异同点 目录 1. 语言节点gaishu。。 2 1.1. 节点、函数数量大约200个 2 1.2. 关键词节点 是 有 的 3 1.3. 标识符
    Atitit 编程语言的block概念 目录 1. 匿名block 1 1.1. 函数块 方法快 1 1.2. Sp udf块 1 2. 实现block的方式 1 2.1. 早期的语言大多是采用en
    Atitit 效率提升法细则 v3 t028.docx Atitit 提升效率细则 目录 1. 目标 2 1.1. 配置化增加扩展性 尽可能消除编译 方便增加 调整业务逻辑 2 1.2. 统一接口
  • 原文地址:https://www.cnblogs.com/txw1958/p/WeChat-Message-API.html
Copyright © 2011-2022 走看看