Java开发微信公众号接口调用发送消息模板
联系公众号开发并编写向用户发送消息模板的接口调用。
我们先看接口代码
publicstatic JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) {
System.out.println("消息内容:"Msbo 结果:"+jsonMsbo); try {
getWX_AccessToken(context); } catch (Exception e) { // TODO 自动生成catch块 e.printStackTrace(); }
//拼接请求地址
String requestUrl = "";
requestUrl = ("ACCESS_TOKEN", (ContextTokenName).toString());
//发送客服消息J jsonObject = getJsonByWX(requestUrl, context, "POST",jsonMsg, false);
if (null != jsonObject) {
int errorCode = ("errcode");
String errorMsg" = ("errmsg" = );
if (0 == errorCode) {
result = true;
System.out.println("模板消息发送成功 errcode:{} "+errorCode+"----"+errorMsg); }否则{
System.out.println("发送模板消息errcode:{} "+errorCode+"----"+errorMsg);第2728章}
第15行的getJsonByWX部分是调用微信接口的统一方法。每个项目都有自己的调用方法,这里就不贴出来了。接口调用链接:
下一步就是创建一个bean类,并在里面写入颜色和值
private String value; private String color; public String getValue() { return value; } publicvoid setValue(String value) { this.value = value; } public String getColor() { return color; } publicvoid setColor(String color) { this.color = color; }
填写模板消息的适当格式和公众号
之后就有一个触发点,然后我选择船舶。然后将发货信息发送给用户
PageData wechatTemplate = new PageData(); ("template_id", "填写你的模板id"); ("touser", ("openid"));//获取用户的openid Map<String,TemplateMessageUtil> mapdata = new HashMap<>(); TemplateMessageUtil first = new TemplateMessageUtil(); ("#173177"); ("发货通知"); ("first", first); TemplateMessageUtil text1 = new TemplateMessageUtil(); ("#173177"); ("您好,您所购买的商品已发货。"); ("text1", text1); TemplateMessageUtil text2 = new TemplateMessageUtil(); ("#173177"); (expresser_name); ("text2", text2); TemplateMessageUtil text3 = new TemplateMessageUtil(); ("#173177"); (expresser_phone); ("text3", text3); TemplateMessageUtil remark = new TemplateMessageUtil(); ("#173177"); ("请保持电话畅通>>"); ("remark", remark); JSONObject json = new JSONObject(); ("data",mapdata); All(wechatTemplate);//转为json WXInterface.sendModelMessage(context,json);
然后手机接收信息
大致思路是这样的,也是根据百度来的。由于每个人的项目方法不同,我就不详细贴出来了。 ,既然模板消息已经发送了,那么调用微信接口的统一方法就应该写在工具类中给大家了。每个人都是不同的,所以应该有一个。调用该方法将条件access_token与微信模板消息绑定。只需写下来并询问即可。剩下的就是传递你想要发送的消息了。消息存储在集合中,并将集合转换为 json。我相信还有一个 JSONObject 类。我不会发布它。每个人的项目都不同。如果一定要照搬过去的话,就根据自己现有的职业进行改进即可。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。