การจัดการข่าวการพัฒนาแพลตฟอร์มสาธารณะ Java Wechat คุณต้องอ่านเอกสารอย่างเป็นทางการก่อน
การจัดการข้อความ WeChat แบ่งออกเป็นการรับข้อความธรรมดารับเหตุการณ์การส่งข้อความส่งข้อความ (ตอบกลับแบบพาสซีฟ) ข้อความบริการลูกค้าข้อความจำนวนมากและข้อความเทมเพลต
1. รับข้อความธรรมดา
เมื่อผู้ใช้ WeChat ทั่วไปส่งข้อความไปยังบัญชีสาธารณะเซิร์ฟเวอร์ WeChat จะบรรจุแพ็กเก็ตข้อมูล XML ของข้อความโพสต์ไปยัง URL ที่กรอกโดยนักพัฒนา
เกี่ยวกับ MSGID คำอธิบายอย่างเป็นทางการเทียบเท่ากับรหัสข้อความแต่ละรายการ เกี่ยวกับน้ำหนักข้อความลองแนะนำให้ใช้ msgid กับคิว หากเซิร์ฟเวอร์ WeChat ไม่ได้รับการตอบกลับภายในห้าวินาทีมันจะตัดการเชื่อมต่อและเริ่มต้นคำขออีกครั้งและลองรวมสามครั้งอีกครั้ง
ตัวอย่างเช่นตัวอย่าง XML ของข้อความ
<xml> <tousername> <! [cdata [touser]]> </tousername> <fromusername> <! [cdata [fromuser]]> </fromusername> <Createtime> 1348831860 </createTime> <SgType> ทดสอบ]]> </เนื้อหา> <SGID> 1234567890123456 </sgid> </xml>
ตรวจสอบข้อความอื่น ๆ ในเอกสารอย่างเป็นทางการและเพียงแค่ห่อหุ้มข้อความต่อไปนี้ Abstractmsg.java
แพ็คเกจ com.phil.wechat.msg.model.req; นำเข้า java.io.serializable; / ** * คลาสข้อความพื้นฐาน * * @author Phil * */ บทคัดย่อระดับนามธรรม Abstractmsg ใช้ serializable {ส่วนตัวคงที่สุดท้าย Long SerialVersionuid = -6244277633057415731L; สตริงส่วนตัว tousername; // นักพัฒนา WeChat ID สตริงส่วนตัวจากชื่ออัสยา; // บัญชีผู้ส่ง (openId) สตริงส่วนตัว msgtype = setMsGType (); // ประเภทข้อความตัวอย่างเช่น/ข้อความ/ภาพส่วนตัว Long CreateTime; // เวลาการสร้างข้อความ (จำนวนเต็ม) msgid ยาวส่วนตัว; // รหัสข้อความ, จำนวนเต็ม 64 บิต/ ** * ประเภทข้อความ * * @return */ สตริงนามธรรมสาธารณะ setMsgType (); -ข้อความ textmsg.java
แพ็คเกจ com.phil.wechat.msg.model.req; / *** ข้อความ* @author Phil* @date 30 มิถุนายน 2017**/ คลาสสาธารณะ textmsg ขยายบทคัดย่อ {ส่วนตัวคงที่สุดท้าย Long SerialVersionuid = -1764016801417503409L; เนื้อหาสตริงส่วนตัว // ข้อความ @Override สตริงสาธารณะ setMSGTYPE () {return "text"; -คนอื่นเป็นแบบนี้ ...
2. ตอบกลับข้อความผู้ใช้อย่างอดทน
หลังจากเซิร์ฟเวอร์ WeChat ส่งข้อความของผู้ใช้ไปยังที่อยู่เซิร์ฟเวอร์นักพัฒนา (กำหนดค่าที่ศูนย์นักพัฒนา) ของบัญชีอย่างเป็นทางการเซิร์ฟเวอร์ WeChat จะตัดการเชื่อมต่อหากไม่ได้รับการตอบกลับภายในห้าวินาทีและเริ่มคำขออีกครั้งและลองรวมสามครั้ง หากในระหว่างการดีบักพบว่าผู้ใช้ไม่สามารถรับข้อความตอบกลับได้คุณสามารถตรวจสอบว่าการประมวลผลข้อความหมดเวลาหรือไม่ หากเซิร์ฟเวอร์ไม่สามารถรับประกันได้ว่าจะดำเนินการและตอบกลับภายในห้าวินาทีก็สามารถตอบกลับสตริงว่างได้โดยตรง เซิร์ฟเวอร์ WeChat จะไม่จัดการกับสิ่งนี้และจะไม่เริ่มต้นใหม่
หาก "บัญชีอย่างเป็นทางการนี้ไม่สามารถให้บริการในขณะนี้โปรดลองอีกครั้งในภายหลัง" มีสองเหตุผลสำหรับเรื่องนี้
ตัวอย่างเช่นตัวอย่างข้อความตอบกลับ XML
<xml> <tousername> <! [cdata [touser]]> </tousername> <fromusername> <! [cdata [fromuser]]> </fromusername> <createTime> 12345678 </createTime> <sgtype> <เนื้อหา> <! [CDATA [Hello]]> </SONTENT> </XML>
ในแพ็คเกจง่ายๆ
ตอบข้อความบทคัดย่อคลาสพื้นฐาน resplabstractmsg.java
แพ็คเกจ com.phil.wechat.msg.model.resp; นำเข้า java.io.serializable; / ** * คลาสฐานข้อความ (บัญชีสาธารณะ-> ผู้ใช้ทั่วไป) * * @author Phil * */ Public Abstract Class Respabstractmsg {// บัญชีตัวรับสัญญาณ (ได้รับ openId) สตริงส่วนตัว tousername; // นักพัฒนา WeChat ID สตริงส่วนตัวจากชื่ออัสยา; // เวลาในการสร้างข้อความ (อินทิกรัล) createTime ส่วนตัวยาว; // ประเภทข้อความ (ข้อความ/เพลง/ข่าว) สตริงส่วนตัว msgtype = setMsGType (); // ประเภทข้อความสตริงนามธรรมสาธารณะ setMSGTYPE (); - ตอบข้อความ resptExtMsg.java
แพ็คเกจ com.phil.wechat.msg.model.resp; / ** * ตอบกลับข้อความรูปภาพ * * @author Phil * @data 26 มีนาคม 2017 * */ คลาสสาธารณะ respimagemsg ขยาย respabstractmsg {ภาพส่วนตัว; @Override สตริงสาธารณะ setMSGTYPE () {return "image"; } / ** * * @author Phil * @date 19 กรกฎาคม 2017 * * / รูปภาพคลาสสาธารณะ {// อัปโหลดไฟล์มัลติมีเดียผ่านอินเทอร์เฟซในการจัดการวัสดุเพื่อรับรหัส สตริงส่วนตัว Public String getMediaid () {return medicleid; } โมฆะสาธารณะ setMediaid (String MediAid) {mediaId = mediaId; - ประเภทข้อความอื่น ๆ มีดังนี้ ...
3. การประมวลผลข้อความ
Master XML Parsing
แพ็คเกจ com.phil.wechat.msg.controller; นำเข้า java.io.ioException; นำเข้า Java.io.InputStream; นำเข้า java.util.map; นำเข้า Java.util.Objects; นำเข้า org.apache.commons.lang3.Stringutils; นำเข้า org.dom4j.documentException; นำเข้า org.slf4j.logger; นำเข้า org.slf4j.loggerfactory; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า com.phil.modules.config.wechatconfig; นำเข้า com.phil.modules.util.msgutil; นำเข้า com.phil.modules.util.signatureutil; นำเข้า com.phil.modules.util.xmlutil; นำเข้า com.phil.wechat.base.controller.basecontroller; นำเข้า com.phil.wechat.base.result.wechatresult; นำเข้า com.phil.wechat.msg.model.req.basicmsg; นำเข้า com.phil.wechat.msg.model.resp.respabstractmsg; นำเข้า com.phil.wechat.msg.model.resp.respnewsmsg; นำเข้า com.phil.wechat.msg.model.resp.respnewsmsg; นำเข้า com.phil.wechat.msg.service.wechatmsgservice; /** * @author Phil * @date 19 กันยายน 2017 * */@controller @requestmapping ("/weChat") คลาสสาธารณะ WeChatmsgController ขยาย basecontroller {logger ส่วนตัว logger = loggerFactory.getLogger (this.getClass ()); @autowired ส่วนตัว WeChatMSGSERVICE WECHATMSGSERVICE; /** * ตรวจสอบว่าข้อมูลถูกส่งจากเซิร์ฟเวอร์ weChat และประมวลผลข้อความ * @param out * @throws ioexception */@requestmapping (value = "/handler", method = {requestmethod.get, requestmethod.post}) void processpost () this.getResponse (). setCharacterencoding ("UTF-8"); บูลีน ispost = objects.equals ("โพสต์", this.getRequest (). getMethod (). touppercase ()); ถ้า (isPost) {logger.debug ("การเข้าถึงสำเร็จลอจิกกำลังดำเนินการ"); string respxml = defaultmsgDispose (this.getRequest (). getInputStream ()); // processRequest (คำขอ, การตอบสนอง); if (stringutils.isnotblank (respxml)) {this.getResponse (). getWriter (). เขียน (respxml); }} else {String Signature = this.getRequest (). getParameter ("ลายเซ็น"); // timestamp สตริง timestamp = this.getRequest (). getParameter ("timestamp"); // สตริงหมายเลขสุ่ม nonce = this.getRequest (). getParameter ("nonce"); // ตรวจสอบคำขอโดยตรวจสอบลายเซ็น หากการตรวจสอบประสบความสำเร็จให้ส่งคืน echosttr ตามที่เป็นอยู่แสดงว่าการเข้าถึงประสบความสำเร็จ มิฉะนั้นการเข้าถึงจะล้มเหลวหาก (SignatureUtil.Checksignature (ลายเซ็น, timestamp, nonce)) {// สตริงสตริงสุ่ม echoStr = this.getRequest (). getParameter ("echosttr"); logger.debug ("ป้อนสำเร็จ, echostr {}", echostr); this.getResponse (). getWriter (). เขียน (echostr); }}} / ** * วิธีการประมวลผลเริ่มต้น * @param อินพุต * @return * @throws Exception * @throws documentException * / สตริงส่วนตัว defaultMsgDispose (inputStream inputStream) โยนข้อยกเว้น {สตริงผลลัพธ์ = null; if (inputStream! = null) {map <string, string> params = xmlutil.parsestreamTomap (inputStream); if (params! = null && params.size ()> 0) {masmmsg msginfo = ใหม่ basicmsg (); String createTime = params.get ("createTime"); String msgid = params.get ("msgid"); msginfo.setCreateTime ((createTime! = null &&! "". เท่ากับ (createTime))? integer.parseint (createTime): 0); msginfo.setFromuserName (params.get ("fromusername")); msginfo.setmsgid ((msgid! = null &&! "". เท่ากับ (msgid))? long.parselong (msgid): 0); msginfo.settousername (params.get ("tousername")); wechatresult resultObj = coreHandler (msginfo, params); if (resultObj == null) {// return null; } ความสำเร็จของบูลีน = resultObj.issuccess (); // ถ้าเป็นจริงหมายความว่าไฟล์ XML ถูกส่งคืนและสามารถแปลงได้โดยตรงไม่เช่นนั้นตามประเภทถ้า (ความสำเร็จ) {result = resultObj.getObject (). toString (); } else {int type = resultObj.getType (); // 1 ข้อความกราฟิกถูกระบุไว้ที่นี่มิฉะนั้นจะถูกแปลงโดยตรงถ้า (type == wechatresult.newsmsg) {respnewsmsg newsmsg = (respnewsmsg) resultoBj.getObject (); ผลลัพธ์ = msgutil.newsmsgtoxml (Newsmsg); } else {respabstractmsg basicmsg = (respabstractmsg) resultoBj.getObject (); result = msgutil.msgtoxml (basicmsg); }} else {result = "msg ผิด"; }} ผลการส่งคืน; } / ** * การประมวลผลหลัก * * @param msg * คลาสฐานข้อความ * @param params * xml ข้อมูลที่แยกวิเคราะห์ * @return * / private wechatresult coreHandler (MSG MASG MASG, แผนที่ <สตริง, สตริง> พารามิเตอร์) สตริง msgType = params.get ("msgType"); if (stringUtils.isEmpty (msgtype)) {switch (msgtype) {case wechatconfig.req_message_type_text: // ผลลัพธ์ข้อความ = wechatmsgservice.textmsg (msg, params); หยุดพัก; case wechatconfig.req_message_type_image: // ภาพข้อความภาพ = weChatMsgService.imagemsg (msg, params); หยุดพัก; กรณี wechatconfig.req_message_type_link: // ผลการเชื่อมโยงผลลัพธ์ข้อความ = weChatMsgService.linkmsg (msg, params); หยุดพัก; case wechatconfig.req_message_type_location: // ผลที่ตั้งทางภูมิศาสตร์ = wechatmsgservice.locationmsg (msg, params); หยุดพัก; case wechatconfig.req_message_type_voice: // ผลลัพธ์ข้อความเสียง = wechatmsgservice.voicemsg (msg, params); หยุดพัก; case wechatconfig.req_message_type_shortvideo: // ผลลัพธ์วิดีโอสั้น ๆ ผลลัพธ์ = wechatmsgservice.shortvideo (msg, params); หยุดพัก; case wechatconfig.req_message_type_video: // ผลลัพธ์ข้อความวิดีโอ = weChatMsgService.shortVideo (msg, params); หยุดพัก; case wechatconfig.req_message_type_video: // ผลลัพธ์ข้อความวิดีโอ = weChatMSGService.VIDEOMSG (msg, params); หยุดพัก; กรณี wechatconfig.req_message_type_event: // สตริงข้อความเหตุการณ์ EventType = params.get ("เหตุการณ์"); // ถ้า (EventType! = null &&! "". equals (eventtype)) {switch (eventtype) {case wechatconfig.event_type_subscribe: result = wechatmsgservice.subscribe (msg, params); หยุดพัก; กรณี wechatconfig.event_type_unsubscribe: result = weChatMsgService.unsubscribe (msg, params); หยุดพัก; case wechatconfig.event_type_scan: result = wechatmsgservice.scan (msg, params); หยุดพัก; case wechatconfig.event_type_location: result = weChatMsgService.eventLocation (msg, params); หยุดพัก; กรณี wechatconfig.event_type_click: result = wechatmsgservice.eventclick (msg, params); หยุดพัก; case wechatconfig.event_type_view: result = weChatMsgService.eventView (msg, params); หยุดพัก; case wechatconfig.kf_create_session: result = wechatmsgservice.kfcreatesession (msg, params); หยุดพัก; case wechatconfig.kf_close_session: result = weChatmsgService.kfclosesession (msg, params); หยุดพัก; case wechatconfig.kf_close_session: result = weChatmsgService.kfclosesession (msg, params); หยุดพัก; case wechatconfig.kf_switch_session: result = weChatmsgService.kfswitchsession (msg, params); หยุดพัก; ค่าเริ่มต้น: weChatMSGService.eventDefaultreply (msg, params); หยุดพัก; } } หยุดพัก; ค่าเริ่มต้น: wechatmsgservice.defaultmsg (msg, params); }} ผลการส่งคืน; -เพียงแค่ให้แนวคิดโปรดย้ายหากคุณอ้างถึงรหัส
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น