好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java微信公众号发送消息模板

本文实例为大家分享了 java 微信公众号 发送消息模板的具体代码,供大家参考,具体内容如下

这段时间接触公众号开发,写下向用户发送消息模板的接口调用

先上接口代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

public static jsonobject sendmodelmessage(servletcontext context,jsonobject jsonmsg) {

     system.out.println( "消息内容:" +jsonmsg);

     boolean result = false ;

     try {

       getwx_accesstoken(context);

     } catch (exception e) {

       // todo auto-generated catch block

       e.printstacktrace();

     }

     // 拼接请求地址

     string requesturl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=access_token" ;

     requesturl = requesturl.replace( "access_token" , context.getattribute(contexttokenname).tostring());

     // 发送客服消息

     jsonobject jsonobject = getjsonbywx(requesturl, context, "post" ,jsonmsg, false );

 

     if ( null != jsonobject) {

       int errorcode = jsonobject.getint( "errcode" );

       string errormsg = jsonobject.getstring( "errmsg" );

       if ( 0 == errorcode) {

         result = true ;

         system.out.println( "模板消息发送成功 errcode:{} " +errorcode+ "----" +errormsg);

       } else {

         system.out.println( "模板消息发送失败 errcode:{} " +errorcode+ "----" +errormsg);

       }

     }

 

     return null ;

   }

15行那段getjsonbywx是统一调用微信接口的方法,每个项目都有自己的调用方法,我这里就不贴了。接口调用链接: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=access_token

接下来就是建个bean类,里面写入一下颜色及值

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

private string value;

   private string color;

  

   public string getvalue() {

     return value;

   }

 

   public void setvalue(string value) {

     this .value = value;

   }

 

   public string getcolor() {

     return color;

   }

 

   public void setcolor(string color) {

     this .color = color;

  

 

}

在公众号里填写模板消息的对应格式

之后就是有个触发点,我选择发货后把发货信息发送给用户

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

pagedata wechattemplate = new pagedata();

     wechattemplate.put( "template_id" , "填写你的模板id" );

     wechattemplate.put( "touser" , userinfo.get( "openid" )); //获取用户的openid

  

     map<string,templatemessageutil> mapdata = new hashmap<>();

    

     templatemessageutil first = new templatemessageutil();   

     first.setcolor( "#173177" );

     first.setvalue( "发货通知" );

     mapdata.put( "first" , first);

    

     templatemessageutil text1 = new templatemessageutil(); 

     text1.setcolor( "#173177" );

     text1.setvalue( "您好,您所购买的商品已发货。" );

     mapdata.put( "text1" , text1);

    

     templatemessageutil text2 = new templatemessageutil(); 

     text2.setcolor( "#173177" );

     text2.setvalue(expresser_name);

     mapdata.put( "text2" , text2);

    

     templatemessageutil text3 = new templatemessageutil(); 

     text3.setcolor( "#173177" );

     text3.setvalue(expresser_phone);

     mapdata.put( "text3" , text3);

    

     templatemessageutil remark = new templatemessageutil();   

     remark.setcolor( "#173177" );

     remark.setvalue( "请保持电话畅通>>" );

     mapdata.put( "remark" , remark);

 

     jsonobject json = new jsonobject();

     json.put( "data" ,mapdata);

     json.putall(wechattemplate); //转为json

        

     wxinterface.sendmodelmessage(context,json);

之后手机就会收到信息了

整体思路是这样,也是参照百度而来,因为每个人的项目里方法都不一样,我就不详细贴上,既然做到发送模板消息了,统一调用微信接口的方法应每个人该也早写在工具类里了,每个人都不同,当应该都有,调用这个方法,把微信模板消息连接的条件access_token写进去就请求了,剩下的就是传入你要发送的消息,消息存入集合,集合转json才行,jsonobject类相信也都有,我也不贴了,每个人项目都不一样,没必要照搬过去,就照着自己原先已有的类改进。

原文链接:https://www.cnblogs.com/lisiping/archive/2018/08/18/9496216.html

查看更多关于java微信公众号发送消息模板的详细内容...

  阅读:53次