微信平台更新之後,發現客服介面不錯。研究了下 和大家分享下。
依照官方文檔,是向客服介面發送規定的JSon 就可以了。
首先先封裝下JSon 的類別:
package com.lwz.wx.bean.kf; // 这个是最外层的 也可以说是基类吧、 public class Basebean { private String touser; private String msgtype; public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } }
//这个类是继承基类、 package com.lwz.wx.bean.kf; public class BaseNews extends Basebean{ private Kfnews news; public Kfnews getNews() { return news; } public void setNews(Kfnews news) { this.news = news; } } //
package com.lwz.wx.bean.kf; import java.util.List; public class Kfnews { private List<articles> articles; public List<articles> getArticles() { return articles; } public void setArticles(List<articles> articles) { this.articles = articles; } } //
package com.lwz.wx.bean.kf; public class articles { private String title; private String description; private String url; private String picurl; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getPicurl() { return picurl; } public void setPicurl(String picurl) { this.picurl = picurl; } }
以上的結構就對應
接下來就是對JSON 的資料的創建了
package com.lwz.wx.main; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import net.sf.json.JSONObject; import com.lwz.wx.bean.AccessToken; import com.lwz.wx.bean.Articles; import com.lwz.wx.bean.kf.BaseNews; import com.lwz.wx.bean.kf.BaseText; import com.lwz.wx.bean.kf.Basebean; import com.lwz.wx.bean.kf.Kfnews; import com.lwz.wx.bean.kf.articles; import com.lwz.wx.bean.kf.text; import com.lwz.wx.util.WeixinUtil; public class KfManager { private final static Logger log = Logger.getLogger(Basebean.class); public static void Gotokf(String openid){ String appId =""; //填上自己的APPID 下同 需要认证过的哦 String appSecret=""; // 调用接口获取access_token AccessToken at = WeixinUtil.getAccessToken(appId, appSecret); if (null != at) { // 调用接口发送消息 int result = WeixinUtil.Runkf( getkfnews(openid), at.getToken()); // 这个方法会在下面 展示 //int result = WeixinUtil.createMenu(getMenu(),"1832148947"); // 判断菜单创建结果 if (0 == result) log.info("调用客服信息发送成功!"); else log.info("客服调用失败,错误码:" + result); } }
private static BaseNews getkfnews(String openid) { articles art1=new articles(); art1.setDescription("1"); art1.setPicurl("http://www.baidu.com"); art1.setTitle("测试1"); art1.setUrl("http://www.baidu.com"); articles art2=new articles(); art2.setDescription("1"); art2.setPicurl("http://www.baidu.com"); art2.setTitle("测试1"); art2.setUrl("http://www.baidu.com"); List<articles> list = new ArrayList<articles>(); Kfnews news=new Kfnews(); list.add(art1); list.add(art2); news.setArticles(list); BaseNews kfbean=new BaseNews(); kfbean.setMsgtype("news"); kfbean.setTouser(openid); kfbean.setNews(news); String jsonkfbean = JSONObject.fromObject(kfbean).toString(); System.out.println(jsonkfbean); return kfbean; } private static BaseText getkftext(String openid) { text text=new text(); text.setContent("文本内容"); BaseText textbean=new BaseText(); textbean.setMsgtype("text"); textbean.setTouser(openid); textbean.setText(text); String jsonkfbean = JSONObject.fromObject(textbean).toString(); System.out.println(jsonkfbean); return textbean; } } // 上面的有用到一个调用接口的方法如下: public static String kf_news_url= "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; public static int Runkf(Basebean getkfnews, String token) { int result = 0; // 拼装创建的url String url = kf_news_url.replace("ACCESS_TOKEN", token); // 将对象转换成json字符 String jsonnews = JSONObject.fromObject(getkfnews).toString(); //System.out.println(jsonMenu); // 调用接口创建 JSONObject jsonObject = httpRequest(url, "POST", jsonnews); if (null != jsonObject) { if (0 != jsonObject.getInt("errcode")) { result = jsonObject.getInt("errcode"); log.error("调用客服接口失败 errcode:{} errmsg:{}"); } } return result; }
到這裡就完成了。可能會比較多。其他的文本的 音樂的 也都是這樣的
以上是微信公眾號開發客服介面實例代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器