《用微信测试公众号慰问你的好兄弟/姐妹》:用java简单实现微信公众号消息推送(入门且详细且有效)


一、前言

不知各位屁股们有没有这种感受:
就比如这天晚上,我关注了一个营销公众号:喜欢XXX的屁股
》。
然后这天晚上它就一直给我推这一系列的文章!
真的,对于这种行为真的很反感,很讨厌,对于不喜欢的人。
但是,我不一样,我很喜欢。不!不!不!,不是你们想的那样(狗头)。
我的意思是我不是不喜欢。
这里我引用一句古语:己所不欲,勿施于人。
意思就是 "自己不喜欢的,就要强加给对方,然后你就快乐了!"
在这里插入图片描述
对啊!我何不自己弄个测试公众号,我用来轰炸我的 好兄弟/好姐妹!!!
想想就激动!(冷笑话:激动就激动,xx不要动!你干嘛!哎呦~~~)
话不多刷,直接开干!
这里说明一下,公众号用的是测试公众号,此项目用于学习,无任何商业用途,而且我也是刚入门的小白,欢迎各位大佬指点!
此文章作为博主笔记,方便后续查看,如有侵权删。
在这里插入图片描述

二、注册微信测试号并配置信息

点击进入微信公众平台:
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
然后注册登陆进入这个界面:
在这里插入图片描述
记住这几个参数,后面代码要用到:分别是appID,appsecret,关注微信号ID,模板ID。

1.获取appID和appsecret:

在这里插入图片描述

2.获取关注微信号ID:

这里让你的兄弟微信扫码关注公众号,然后在右面用户列表就会出现关注用户的微信号了,
然后记住这个关注微信号参数,后面要用到。

在这里插入图片描述

3.生成并获取模板ID:

点击新增测试模板,编辑模板信息,编辑完成后,记住生成的模板ID
点击新增测试模板
在这里插入图片描述
在这里插入图片描述
模板内容:

{{date.DATA}} 
作者:{{author.DATA}} 
收信人:{{user.DATA}} 
地点: {{place.DATA}} 
备注:今天是我们交往的第{{day.DATA}}天!无论多久没联系,多远的距离,请你记住,你还是我的好儿子!

ok!到此前期所有的配置就搞定了!接下来进入代码阶段!!!

三、代码解读

1.新建一个springboot项目

在这里插入图片描述

2.引入pom依赖

<dependencies>
        <!--fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>
        <!--apche httpclient的maven依赖-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!-- web应用依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

3.编写代码

目录结构:HttpUtils,SendMessage,Template
在这里插入图片描述
Template:发送模板实体类

package com.jljlm.wx;

/**
 * @Auther: jljlm
 * @Description: 发送模板实体类
 */
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class Template {
    private String touser;
    private String template_id;
    private String url;
    private Map<String, Map<String,String>> data;
}

发送http请求工具类:HttpUtils

package com.jljlm.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Auther: jljlm
 * @Description: 发送http请求工具类
 */
public class HttpUtils {
    /**
     * 模拟get请求到微信服务器获取access_token参数
     */
    public static String sendGet(String url) {
        String response = null;
        // 创建一个HttpClient对象
        CloseableHttpClient aDefault = HttpClients.createDefault();
        // 创建http GET请求
        HttpGet httpGet = new HttpGet(url);
        try {
            // 使用HttpClient来执行Request请求,得到对方的response
            CloseableHttpResponse execute = aDefault.execute(httpGet);
            response = EntityUtils.toString(execute.getEntity(), StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (aDefault != null) {
                try {
                    // 关闭
                    aDefault.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        JSONObject jsonObject = JSON.parseObject(response);
        String access_token = jsonObject.getString("access_token");
        return access_token;
    }

    /**
     * 模拟post请求到微信服务器发送模板内容信息
     */
    public static String sendPost(String url, String data,String access_token) {
        String response = null;
        try {
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse httpResponse = null;
            try {
                httpclient = HttpClients.createDefault();
                HttpPost httppost = new HttpPost(url+access_token);
                HttpEntity stringentity = new StringEntity(data, ContentType.create("application/json", "UTF-8"));
                httppost.setEntity(stringentity);
                httpResponse = httpclient.execute(httppost);
                response = EntityUtils
                        .toString(httpResponse.getEntity());
            } finally {
                if (httpclient != null) {
                    // 关闭
                    httpclient.close();
                }
                if (httpResponse != null) {
                    httpResponse.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

}

SendMessage:发送消息

package com.jljlm.wx;

import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: jljlm
 * @Description: 发送消息
 */
public class SendMessage {
    // 测试号信息参数appID(填自己的!!!)
    public static final String APP_ID = "xxxxxxxxx";
    // 测试号信息参数appsecret(填自己的!!!)
    public static final String APP_SECRET = "xxxxxxxxx";
    // 模板ID(填自己的!!!!)
    public static final String TEMPLATE_ID="xxxxxxxxx";
    // 关注用户的微信号(填自己的!!!!)
    public static final String OPEN_ID = "xxxxxxxxx";
    // 模板消息详情跳转URL
    public static final String TO_URL="http://www.baidu.com";
    // get请求链接
    public static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
            + APP_ID + "&secret=" + APP_SECRET;
    // pose请求链接
    public static final String SEND_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

    public static void main(String[] args) {
        // 1.模拟get请求到微信服务器获取access_token参数
        String access_token = HttpUtils.sendGet(ACCESS_TOKEN_URL);
        // 2.编辑模板内容信息
        HashMap<String, String> temp1 = new HashMap<>();
        temp1.put("value","天冷了,让我暖你一整天!");
        temp1.put("color","#000000");
        HashMap<String, String> temp2 = new HashMap<>();
        temp2.put("value","加辣椒了吗?");
        temp2.put("color","#000000");
        HashMap<String, String> temp3 = new HashMap<>();
        temp3.put("value","海绵");
        temp3.put("color","#000000");
        HashMap<String, String> temp4 = new HashMap<>();
        temp4.put("value","地球");
        temp4.put("color","#000000");
        HashMap<String, String> temp5 = new HashMap<>();
        HashMap<String, String> temp6 = new HashMap<>();
        temp5.put("value","5201314");
        temp5.put("color","#000000");
        HashMap<String, Map<String ,String>> templateData  = new HashMap<>();
        templateData.put("date",temp1);
        templateData.put("author",temp2);
        templateData.put("user",temp3);
        templateData.put("place",temp4);
        templateData.put("day",temp5);
        Template wxTemplate = Template.builder()
            .template_id(TEMPLATE_ID)
            .data(templateData)
            .url(TO_URL)
            .touser(OPEN_ID)
            .build();
        String jsonString = JSON.toJSONString(wxTemplate);
        // 3.模拟post请求到微信服务器发送模板内容信息
        HttpUtils.sendPost(SEND_TEMPLATE_URL,jsonString,access_token);
    }
}

最后点击运行,这样就搞定了!!!

在这里插入图片描述

总结:

看到项目跑起来,已经开始激动的,快让你的好兄弟/好姐妹扫码关注,暖他一整天!!!

@作者:加辣椒了吗?
简介:憨批大学生一枚,喜欢在博客上记录自己的学习心得,也希望能够帮助到你们!
在这里插入图片描述

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加辣椒了吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值