springboot实现企业微信机器人自动按时播报天气

springboot实现企业微信机器人自动按时播报天气

  • 第一步搭建项目。。。这个没有什么好说的
    配置:
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
  • 第二步项目文件夹的设计

在这里插入图片描述
在这里插入图片描述
这个搞完后我们来看到实体类
Forecast

package com.example.wx.bean;

public class Forecast {
    private String wdnight;//晚上风向
    private String date;
    private String high;//温度
    private String textnight;//晚上的天气
    private String wdday;//白天风向
    private String low;//最低温度
    private String wcnight;//晚上风力
    private String textday;//白天的天气
    private String wcday;//白天的风力
    private String week;//时间
    @Override
    public String toString() {
        return "明天的预计天气 "+textday+", 最高温度"+high+"度"+", 最低温度"+low+"度"+" 白天的风力 "+wcday+
                ", 风向"+wdday+ ", 晚上的风力"+wcnight+ ", 晚上的风向"+wdnight+ ", 晚上天气"+textnight +
                week+", 数据接口由百度地图提供 " + "\n" + "\n" +"感谢老哥(邹宇杰)分享接口和相关代码,前人探路后人乘凉!感谢!!!";
    }
    public String getWeek() {
        return week;
    }

    public void setWeek(String week) {
        this.week = week;
    }

    public String getWdnight() {
        return wdnight;
    }

    public void setWdnight(String wdnight) {
        this.wdnight = wdnight;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getHigh() {
        return high;
    }

    public void setHigh(String high) {
        this.high = high;
    }

    public String getTextnight() {
        return textnight;
    }

    public void setTextnight(String textnight) {
        this.textnight = textnight;
    }

    public String getWdday() {
        return wdday;
    }

    public void setWdday(String wdday) {
        this.wdday = wdday;
    }

    public String getLow() {
        return low;
    }

    public void setLow(String low) {
        this.low = low;
    }

    public String getWcnight() {
        return wcnight;
    }

    public void setWcnight(String wcnight) {
        this.wcnight = wcnight;
    }

    public String getTextday() {
        return textday;
    }

    public void setTextday(String textday) {
        this.textday = textday;
    }

    public String getWcday() {
        return wcday;
    }

    public void setWcday(String wcday) {
        this.wcday = wcday;
    }

}

Weather

package com.example.wx.bean;

import java.util.List;

public class Weather {
    private String country;
    private String province;
    private String city;
    private String name;
    private String rh;//湿度
    private String text ;//天气
    private String windclass;//风级
    private String winddir;//风向
    private String feellike;//体表温度
    private String uptime;//数据更新时间
    private Forecast forecasts;//近日天气
    @Override
    public String toString() {
        return "大家好我是南巷,天气预报来了请你接收: " + country+province+city+name+"的今天的天气:"+" 湿度为"+rh+", 天气"+text+", 风力"+windclass
                +", 风向"+winddir+", 体表温度"+feellike+"度"+", 数据更新时间"+uptime+"\n"+ "\n" + forecasts;
    }
    public Forecast getForecasts() {
        return forecasts;
    }

    public void setForecasts(Forecast forecasts) {
        this.forecasts = forecasts;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRh() {
        return rh;
    }

    public void setRh(String rh) {
        this.rh = rh;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getWindclass() {
        return windclass;
    }

    public void setWindclass(String windclass) {
        this.windclass = windclass;
    }

    public String getWinddir() {
        return winddir;
    }

    public void setWinddir(String winddir) {
        this.winddir = winddir;
    }

    public String getFeellike() {
        return feellike;
    }

    public void setFeellike(String feellike) {
        this.feellike = feellike;
    }

    public String getUptime() {
        return uptime;
    }

    public void setUptime(String uptime) {
        this.uptime = uptime;
    }

}

然后开启我们的定时任务

package com.example.wx.config;

import com.example.wx.service.ExecuteTimer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Timer {
    @Autowired
    private ExecuteTimer executeTimer;


    // "0 0 8,14,21 * * ?" 每天8点。14点,21点定时任务
    @Scheduled(cron = "0 0 8,14,21 * * ?")
    public void executeTimer() {
        executeTimer.executeTimer();
        System.out.println("cg");
    }

}

这个还要在主方法加上一个开启注解
在这里插入图片描述
**

  • 最后就是重点了

**

package com.example.wx.service;

import com.example.wx.bean.Forecast;
import com.example.wx.bean.Weather;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class ExecuteTimer {

    public void executeTimer() {
        String message = cx();
        fs(message);
    }

    public void fs(String text) {

        String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=*********"; //微信机器人地址(自己的机器人地址)

        CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建HTTP对象

        HttpPost httpPost = new HttpPost(url);  // 创建post请求

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> mapson = new HashMap<>();
        mapson.put("content", text);
        map.put("text", mapson);
        map.put("msgtype", "text");
        JSONObject jsonMap = JSONObject.fromObject(map);
        System.out.println(jsonMap);

        StringEntity entity = new StringEntity(String.valueOf(jsonMap), "UTF-8");

        httpPost.setEntity(entity);  // 响应体
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");  // 响应头

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);  // 客户端执行post请求
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public String cx() {
        String tq = null;
        String url = "https://api.map.baidu.com/weather/v1/?district_id=360111&data_type=all&ak=QLVCURfTor6cr3IekRiK7ebaqLjnqvYN";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建HTTP对象
        HttpGet httpGet = new HttpGet(url);  // 创建post请求
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            if (responseEntity != null) {
                String msg = EntityUtils.toString(responseEntity);
                tq = getmsg(msg);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tq;
    }

    public String getmsg(String msg){
        JSONObject jsonObject = JSONObject.fromObject(msg);
        JSONObject jsonObject1 = jsonObject.getJSONObject("result");
        JSONObject jsonObject2 = jsonObject1.getJSONObject("location");//地区
        JSONObject jsonObject3 = jsonObject1.getJSONObject("now");//今天天气
        JSONArray jsonArray = jsonObject1.getJSONArray("forecasts");
        JSONObject jsonObject4 = jsonArray.getJSONObject(0);//明天的天气

        Forecast forecast = new Forecast();

        forecast.setDate(jsonObject4.get("date").toString());
        forecast.setHigh(jsonObject4.get("high").toString());
        forecast.setLow(jsonObject4.get("low").toString());
        forecast.setTextday(jsonObject4.get("text_day").toString());
        forecast.setTextnight(jsonObject4.get("text_night").toString());
        forecast.setWcday(jsonObject4.get("wc_day").toString());
        forecast.setWeek(jsonObject4.get("week").toString());
        forecast.setWdday(jsonObject4.get("wd_day").toString());
        forecast.setWcnight(jsonObject4.get("wc_night").toString());
        forecast.setWdnight(jsonObject4.get("wd_night").toString());

        Weather weather = new Weather();

        weather.setCountry(jsonObject2.get("country").toString());
        weather.setProvince(jsonObject2.get("province").toString());
        weather.setCity(jsonObject2.get("city").toString());
        weather.setName(jsonObject2.get("name").toString());
        weather.setText(jsonObject3.get("text").toString());
        weather.setFeellike(jsonObject3.get("feels_like").toString());
        weather.setRh(jsonObject3.get("rh").toString());
        weather.setUptime(jsonObject3.get("uptime").toString());
        weather.setWindclass(jsonObject3.get("wind_class").toString());
        weather.setWinddir(jsonObject3.get("wind_dir").toString());
        weather.setForecasts(forecast);

        return weather.toString();
    }
}

**请添加图片描述

运行项目就可以啦!!

**

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值