When a user actively sends a message to a WeChat public account (including sending a message, clicking on a custom menu click event, subscription event, scanning QR code event, payment success event, and user rights protection), WeChat will push the message data For developers, developers can call the customer service message interface within a period of time (currently 48 hours) and send messages to ordinary users by POSTing a JSON data packet. There is no limit on the number of sending times within 48 hours. This interface is mainly used for functions such as customer service that require manual message processing, so that developers can provide better services.
Customer service interface call request description
The interface description for customer service interface call is as follows (http request method is POST). If you want to obtain the Access Token, please refer to the previous description:
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=[ACCESS_TOKEN]
The customer service interface provides a huge space for imagination, and we can use this For example, develop enterprise customer service solutions. When users send support requests to WeChat public accounts, customer service staff can receive the information through web pages, mobile applications, WeChat, etc. and perform further processing. Customer service staff can also queue up, or Set the user's VIP level to provide different levels of service response speed, etc., and build a very intelligent customer service support system.
Send a text message to the user
When sending a text message to the user, the data format sent is as follows:
{ "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" } }
Among them, the specific parameters of each parameter The description is as follows:
The code for sending text messages to users in Force.com is implemented as follows:
Http h = new Http(); HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setHeader('Accept-Encoding','gzip,deflate'); req.setHeader('Content-Type','text/xml;charset=UTF-8'); req.setHeader('User-Agent','Jakarta Commons-HttpClient/3.1'); String sendMsg = '{ "touser":"ou-37t936RNZEcW0mI75RN2pdxkc", "msgtype":"text", "text": { "content":"测试客服消息" } }'; req.setBody(sendMsg); req.setEndpoint('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=bW3aubvd1GVgDZbrO6zv1WGVYIU0QlEhI1J5x_NpbiouxyBc8eufzQQHHyy8cMoZmuQknz-_iEgSB76CggVTgQ’); String bodyRes = ''; try{ HttpResponse res = h.send(req); bodyRes = res.getBody(); } catch(System.CalloutException e) { System.debug('Callout error: '+ e); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); } );
After executing the above code, the effect is as shown in the figure:
Send picture messages to users
When sending picture and text messages to users, the format of the data sent is as follows:
{ "touser":"OPENID", "msgtype":"image", "image": { "media_id":"MEDIA_ID" } }
Among them, each The specific description of each parameter is as follows:
The media ID of the image needs to be obtained after uploading the image through Tencent's multimedia file upload interface. We will give a specific example of this example later. The "Upload and download multimedia files" example is specifically explained.
Send voice messages to users
When sending music to users, the data format is as follows:
{ "touser":"OPENID", "msgtype":"voice", "voice": { "media_id":"MEDIA_ID" } }
The specific description of each parameter is as follows:
The media ID of the image needs to be obtained after uploading the image through Tencent's multimedia file upload interface. The other codes are no different from sending text messages, so I will not go into details here.
Send a voice message to the user
The message format for sending a video to the user is as follows:
{ "touser":"OPENID", "msgtype":"video", "video": { "media_id":"MEDIA_ID", "thumb_media_id":"MEDIA_ID", "title":"TITLE", "description":"DESCRIPTION" } }
The specific description of each parameter is as follows:
The media ID of the video needs to be obtained after uploading the image through Tencent's multimedia file upload interface. The other codes are no different from sending text messages, so I will not go into details here.
Send music messages to users
The message format for sending music to users is as follows:
{ "touser":"OPENID", "msgtype":"music", "music": { "title":"MUSIC_TITLE", "description":"MUSIC_DESCRIPTION", "musicurl":"MUSIC_URL", "hqmusicurl":"HQ_MUSIC_URL", "thumb_media_id":"THUMB_MEDIA_ID" } }
The specific description of each parameter is as follows:
Send graphic messages to users
Post a code to send graphic messages as follows:
Http h = new Http(); HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setHeader('Accept-Encoding','gzip,deflate'); req.setHeader('Content-Type','text/xml;charset=UTF-8'); req.setHeader('User-Agent','Jakarta Commons-HttpClient/3.1'); String sendMsg = '{"touser":"ou-37t936RNZEcW0mI75RN2pdxkc","msgtype":"news","news":{"articles":[{"title":"HappyDay","description":"IsReallyAHappyDay","url":"http://www.36kr.com/p/212479.html","picurl":"http://a.36krcnd.com/photo/2014/4e3ae0dac4884bb91934a689b72f8f8b.png"}]}}'; req.setBody(sendMsg); req.setEndpoint('https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=bW3aubvd1GVgDZbrO6zv1WGVYIU0QlEhI1J5x_NpbiouxyBc8eufzQQHHyy8cMoZmuQknz-_iEgSB76CggVTgQ’); String bodyRes = ''; try{ HttpResponse res = h.send(req); bodyRes = res.getBody(); } catch(System.CalloutException e) { System.debug('Callout error: '+ e); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); } );
More Force.com WeChat development For articles related to the series of customer service interfaces, please pay attention to the PHP Chinese website!