


Force.com WeChat development series applies for test accounts and responds to graphic messages
Feb 25, 2017 pm 04:51 PMIn addition to simple text message replies, Force.com can also reply to messages with pictures and texts, reply to music or videos, recognize voices sent by users, collect users' geographical location information and provide corresponding content or Services, etc., this article will explain these skills one by one. Before that, we must first introduce how to apply for a test account with all service account interface functions (although this is not necessary for replying to graphic messages).
Apply for a test account
As an individual developer, you can apply for a subscription account. The subscription account only opens the basic interface, including receiving user messages, replying to users, and Accept events (event push has follow or unfollow, scan QR code with parameters (generating such QR code requires advanced interface), report geographical location (not supported by ordinary subscription account), custom menu (not supported by ordinary subscription account) ) click) push three interfaces, but advanced functions such as custom menus, speech recognition, customer service interfaces, OAuth2.0 web page authorization, obtaining user geographical location information, etc. all require service accounts to support, among which certified subscription accounts Support custom menu. In order to facilitate developers to understand and learn these interfaces of Tencent, like any platform company, Tencent finally opened applications for test accounts late last year. Anyone with a WeChat subscription account can apply (a service account should also be possible, but I haven’t seen what the backend of a service account looks like, so I won’t comment).
The application method is simple and direct. After entering the WeChat backend (https://mp.weixin.qq.com), there is a After clicking on the "Developer Center" link, you will find a link to "Interface Test Application System Click to Enter". After clicking to enter, you can apply according to Tencent's ideas. I will not go into details here.
What the application will look like after successful login is as follows. You can see it here. You can also see a two when scrolling the page. QR code, scan this QR code through WeChat to follow this test account, which supports up to 20 test users. After successful follow-up, there will be an additional account called "WeChat Public Platform Test Account" in the WeChat "Subscription Account" folder , note that although it is in the "Subscription Account" folder, it has the functions of all service accounts:
For the next work, here we first build several key classes and corresponding processing frameworks to facilitate the subsequent addition of more functional support.
public?class?IncomingMsg{ ????public?String?toUserName; ????public?String?fromUserName; ????public?String?msgType; ????public?String?picURL; ????public?String?mediaID; ????public?String?locationX; ????public?String?locationY; ????public?String?URL; ????public?String?content; ????public?String?event; ????public?String?eventKey; ????public?String?recognition; ???? ????public?IncomingMsg(){} ???? ????public?IncomingMsg(String?tUN,?String?fUN,?String?mT,?String?pU,?String?mI,?String?lX,?String?lY,?String?u,?String?c,?String?e,?String?eK,?String?r){ ????????this.toUserName?=?tUN; ????????this.fromUserName?=?fUN; ????????this.msgType?=?mT; ????????this.picURL?=?pU; ????????this.mediaID?=?mI; ????????this.locationX?=?lX; ????????this.locationY?=?lY; ????????this.URL?=?u; ????????this.content?=?c; ????????this.event?=?e; ????????this.eventKey?=?eK; ????????this.recognition?=?r; ????} }The definition code of the WeChatNews class is as follows, including the detailed definition information of a news:
public?class?WeChatNews{ ????public?String?title; ????public?String?description; ????public?String?picUrl; ????public?String?url; ???? ????public?WeChatNews(){} ???? ????public?WeChatNews(String?t,?String?d,?String?p,?String?u){ ????????this.title?=?t; ????????this.description?=?d; ????????this.picUrl?=?p; ????????this.url?=?u; ????} }Next, In the doPost method, we will use the XML parsing code in the last blog post tonight to enable it to parse any type of WeChat XML document. The modified doPost method is as follows:
global?static?void?doPost(){ ????????//Receive?message?from?user; ????????RestRequest?req?=?RestContext.request; ????????RestResponse?res?=?RestContext.response; ????????string?strMsg?=?req.requestBody.toString();?? ????????System.debug('Request?Contents'?+?strMsg); ????????XmlStreamReader?reader?=?new?XmlStreamReader(strMsg); ????????String?toUserName?=?''; ????????String?fromUserName?=?''; ????????String?msgType?=?''; ????????String?picURL?=?''; ????????String?mediaID?=?''; ????????String?locationX?=?''; ????????String?locationY?=?''; ????????String?URL?=?''; ????????String?content?=?''; ????????String?msgID?=?''; ????????String?event?=?''; ????????String?eventKey?=?''; ????????String?recognition?=?''; ???????? ????????while(reader.hasNext()){ ????????????if(reader.getLocalName()?==?'ToUserName'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????toUserName?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'FromUserName'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????fromUserName?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'MsgType'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????msgType?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'PicURL'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????picURL?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'MediaId'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????mediaID?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Location_X'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????locationX?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Location_Y'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????locationY?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Url'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????URL?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'MsgId'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????msgID?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Content'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????content?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Event'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????event?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'EventKey'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????eventKey?=?reader.getText(); ????????????????} ????????????} ????????????else?if(reader.getLocalName()?==?'Recognition'){ ????????????????reader.next(); ????????????????if(String.isNotBlank(reader.getText())){ ????????????????????recognition?=?reader.getText(); ????????????????} ????????????} ????????????reader.next(); ????????} ????????IncomingMsg?inMsg?=?new?IncomingMsg(toUserName,?fromUserName,?msgType,?picURL,?mediaID,?locationX,?locationY,?URL,?content,?event,?eventKey,?recognition?); }In this method, we will parse all types of WeChat XML documents. The fields in the WeChat message XML text are parsed, and the IncomingMsg object is initialized through the parsed value. Next, we will pass this object to call different methods to complete various tasks. Next, we add the following code at the end of the above doPost method:
String?rtnMsg?=?''; //回復(fù)消息 if(msgType.equals('text')){ ???rtnMsg?=?handleText(inMsg); } RestContext.response.addHeader('Content-Type',?'text/plain');???? RestContext.response.responseBody?=?Blob.valueOf(rtnMsg);
This code first defines a String that stores the returned XML text. string, and then determine if the message type sent by the user is a text type, then call a handleText method to process the reply message. The object passed to the handleText method here is the IncomingMsg object we defined earlier. We will discuss the details of this method below. This section will introduce it again. After successfully obtaining the return string of this method, the XML text message can be returned to Tencent WeChat through RestContext, and further returned to the user who sent the message.
Detailed explanation of handleText method for sending images and text
private?static?String?handleText(IncomingMsg?msg){ ????????String?keyword?=?msg.content; ????????String?strReply; ????????String?strResult; ????????if(keyword.equals('文本')){ ????????????strReply?=?'這是個文本消息'; ????????????strResult?=?composeTextReply(msg,?strReply); ????????} ????????else?if(keyword.equals('圖文')?||?keyword.equals('單圖文')){ ????????????WeChatNews?news?=?new?WeChatNews('蘋果WWDC2014召開在即',?'2014?年似乎將成為又一個“蘋果之年”,熱愛和不那么熱愛蘋果的人都對它的一舉一動保持著關(guān)注和揣測——以下是蘋果?WWDC?2014?的13大看點(diǎn):',?'http://a.36krcnd.com/photo/2014/4e3ae0dac4884bb91934a689b72f8f8b.png',?'http://www.36kr.com/p/212479.html'); ????????????List<wechatnews>?newsList?=?new?List<wechatnews>(); ????????????newsList.add(news); ????????????strResult?=?composeNewsReply(msg,?newsList); ????????} ????????else?if(keyword.equals('多圖文')){ ????????????WeChatNews?news1?=?new?WeChatNews('蘋果WWDC2014召開在即',?'2014年似乎將成為又一個蘋果之年,熱愛和不那么熱愛蘋果的人都對它的一舉一動保持著關(guān)注和揣測——以下是蘋果?WWDC?2014?的13大看點(diǎn):',?'http://a.36krcnd.com/photo/2014/4e3ae0dac4884bb91934a689b72f8f8b.png',?'http://www.36kr.com/p/212479.html'); ????????????WeChatNews?news2?=?new?WeChatNews('Facebook?CEO?馬克·扎克伯格再做慈善,為灣區(qū)學(xué)校捐贈?1.2?億美元',?'據(jù)?re/code消息,F(xiàn)acebook?CEO?馬克·扎克伯格與妻子Priscilla?Cha?(中文名陳慧嫻)?計劃向灣區(qū)學(xué)校捐贈?1.2?億美元。',?'http://a.36krcnd.com/photo/2014/e64d647389bfda39131e12fa9d606bb6.jpg',?'http://www.36kr.com/p/212476.html'); ????????????WeChatNews?news3?=?new?WeChatNews('Nokia收購Siri的同門師弟Desti,為自家地圖業(yè)務(wù)HERE融入更多人工智能',?'Nokia最近收購了一家地圖公司Desti,來補(bǔ)強(qiáng)自家的地圖業(yè)務(wù)HERE。',?'http://a.36krcnd.com/photo/2014/25490e2b8e63ced9586f0a432eebb972.jpg',?'http://www.36kr.com/p/212484.html'); ????????????List<wechatnews>?newsList?=?new?List<wechatnews>(); ????????????newsList.add(news1); ????????????newsList.add(news2); ????????????newsList.add(news3); ????????????strResult?=?composeNewsReply(msg,?newsList); ????????} ????????else?if(keyword.equals('音樂')){ ????????????Map<string>?music?=?new?Map<string>(); ????????????music.put('title',?'愛你的宿命'); ????????????music.put('description',?'張信哲'); ????????????music.put('musicUrl',?'http://zhangmenshiting.baidu.com/data2/music/119826740/1197655931401552061128.mp3?xcode=80587c819993d49621a8dce05e5bb8c9e36664380262dc7e&song_id=119765593'); ????????????music.put('musicHQUrl',?'http://zhangmenshiting.baidu.com/data2/music/119826740/1197655931401552061128.mp3?xcode=80587c819993d49621a8dce05e5bb8c9e36664380262dc7e&song_id=119765593'); ????????????strResult?=?composeMusicReply(msg,?music);???????????? ????????} ????????return?strResult; ????}</string></string></wechatnews></wechatnews></wechatnews></wechatnews>
代碼的思路應(yīng)該來說比較直接,從第4行的if開始判斷用戶發(fā)送過來的文本是什么,根據(jù)不同的關(guān)鍵字來確定不同的返回內(nèi)容,第一個if里將返回給用戶單圖文信息,這里先構(gòu)造了一個WeChatNews數(shù)組,當(dāng)然數(shù)組里只有一個WeChatNews對象,將這個數(shù)組交給composeNewsReply來完成最終的XML文構(gòu)建;第一個else if也很類似,只不過這里的WeChatNews數(shù)組里有三條新聞,關(guān)于composeNewsReply方法的細(xì)節(jié)我們稍后介紹;最后一個else if里展示了如何回復(fù)音樂,這里我們構(gòu)建了一個Map對象存儲音樂的詳情,并調(diào)用composeMusicReply方法來完成最終的XML文構(gòu)建,同樣該方法的細(xì)節(jié)稍后就會介紹到。
上面的思路應(yīng)該來說還是比較清楚的,接下來介紹composeNewsReply方法的全部代碼:
private?static?String?composeNewsReply(IncomingMsg?msg,?List<wechatnews>?newsList){ ????????String?strNews?=?''; ????????String?newsTpl?=?'<item><title></title> <description></description><picurl></picurl><url></url></item>'; ????????for(WeChatNews?news?:?newsList){ ????????????String[]?arguments?=?new?String[]{news.title,?news.description,?news.picUrl,?news.url}; ????????????strNews?+=?String.format(newsTpl,?arguments); ????????} ????????String?strTmp?=?'<xml><tousername></tousername><fromusername></fromusername><createtime>1234567890</createtime><msgtype></msgtype><articlecount></articlecount><articles>'?+?strNews?+?'</articles></xml>'; ????????String[]?arguments?=?new?String[]{msg.fromUserName,?msg.toUserName,?String.valueOf(newsList.size())}; ????????String?results?=?String.format(strTmp,?arguments); ????????return?results; }</wechatnews>
?
了解該方法代碼前先要了解回復(fù)圖文信息的XML格式,關(guān)于此點(diǎn)可以參照騰訊公司鏈接:回復(fù)圖文消息 ,與前文介紹到的普通文本消息大同小異,可以留意到里面有個ArticleCount字段用來指定回復(fù)的消息里能有幾條圖文新聞,最大是10,超過10則會無法響應(yīng);另外Article節(jié)點(diǎn)下方每一個item均是一條圖文消息。為此,上述代碼的第3行先構(gòu)造一個每條新聞的模板,接著從第4行開始輪詢新聞列表里的每一條新聞,并構(gòu)造相應(yīng)的XML文。從第8行開始構(gòu)造整個圖文回復(fù)的字符串模板,并在第9、10行通過相應(yīng)參數(shù)將模板轉(zhuǎn)換為最終的XML字符串。
再接下來介紹composeMusicReply,該方法的全部代碼如下:
private?static?String?composeMusicReply(IncomingMsg?msg,?Map<string>?music){ ????????String?strTitle?=?music.get('title'); ????????String?strDesc?=?music.get('description'); ????????String?strURL?=?music.get('musicUrl'); ????????String?strHQURL?=?music.get('musicHQUrl'); ????????String?musicTpl?=?'<xml><tousername></tousername><fromusername></fromusername><createtime>12345678</createtime><msgtype></msgtype><music><title></title> <description></description><musicurl></musicurl><hqmusicurl></hqmusicurl></music></xml>'; ????????String[]?arguments?=?new?String[]{msg.fromUserName,?msg.toUserName,?strTitle,?strDesc,?strURL,?strHQURL}; ????????String?results?=?String.format(musicTpl,?arguments); ????????return?results; }</string>
同樣了解該方法要首先了解回復(fù)音樂信息的XML格式,可以參照騰訊公司鏈接:回復(fù)音樂消息,上面代碼與前面方法比較類似,就不再贅述。(這里的Map對象也許有點(diǎn)多余,可以考慮是否可以和回復(fù)視頻的方法整合到一起,否則不需要額外的Map對象開銷,直接將標(biāo)題、描述、鏈接等信息傳給composeMusicReply方法即可)。
運(yùn)行效果
?
完成后直接保存代碼便可立即生效,回復(fù)圖文、多圖文、音樂的運(yùn)行效果分別如下:
?????????
?????????
更多Force.com WeChat development series applies for test accounts and responds to graphic messages相關(guān)文章請關(guān)注PHP中文網(wǎng)!
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform
