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;//时间@Overridepublic 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;//近日天气@Overridepublic 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 {@Autowiredprivate 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();}
}
**
运行项目就可以啦!!
**
springboot实现企业微信机器人自动按时播报天气相关推荐
- 获取MAC OS苹果电脑配置信息,发送到企业微信机器人自动提醒
import subprocess import requests import time import jsonclass MacInfo:def __init__(self):self.strSe ...
- python 企业微信机器人自动推送文字和文件
1.首先需要在企业微信对应群里创建机器人. 2.获取后查看机器人的webhook. 3.发送文件需要先上传文件,所以通过编辑机器人查看api文档,获取上传url: 4.代码 #传入文件 def pos ...
- 企业微信机器人自动消息发送webhook接入代码
主要代码类如下 package com.test.common; import java.io.IOException; import java.text.SimpleDateFormat; impo ...
- python实现企业微信机器人的自动推送
关于企业微信机器人的自动推送 前些天,做了一个关于企业微信机器人自动推送消息的一个小功能,在这里来聊一下其中学习到的一些内容. 由于是需要进行自动推送新闻,因此先对需要获得信息的网页进行爬取. 在爬取 ...
- 企业微信机器人推送mysql_Zabbix.5.0设置企业微信群机器人推送告警信息
一.企业微信端配置 1.创建微信群机器人 在需要接收告警信息的企业微信群上右键(注意群里成员至少要3人以上),选择"添加群机器人",设置机器人名称,系统自动生成此机器人的webho ...
- 微信客服机器人(踩坑记录、SpringBoot、企业微信)
微信客服机器人(踩坑记录.SpringBoot.企业微信) 转载请注明出处:https://www.jjput.com/archives/wei-xin-ke-fu-ji-qi-ren 总体流程 当有 ...
- 企业微信机器人还能这么玩?
英剧<黑镜:圣诞特别篇>中有一集讲到女主日程非常忙碌,同时又对自己要求很高,于是选择在代码世界复制了一个自己,作为自己的"智能管家",这个智能管家堪称完美,完全熟悉女主 ...
- PowerShell 实现企业微信机器人推送消息
前言企业微信机器人 在ARMS告警管理中创建企业微信机器人后,您可以在通知策略中指定对应的企业微信群用于接收告警.当通知策略的匹配规则被触发时,系统会自动向您指定的企业微信群发送告警通知.企业微信群收 ...
- 封装Python脚本:使用企业微信机器人发送消息至企业微信
官方文档地址:https://developer.work.weixin.qq.com/document/path/91770#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8 ...
最新文章
- 为什么 MySQL 使用 B+ 树,而不是 B 树或者 Hash?
- 肝!Python 网络编程
- HttpURLConnection, Android访问网络,实用demo
- java unreported exception_Java异常处理
- 阿里云刘强:无影云电脑构建云上安全办公室
- c++父类和子类转化致命的代码错误
- 实现拷贝函数(strcpy)
- jquery 毫秒转换成日期_jquery js 秒 毫秒转时分秒
- 华为如何造车?动机、底气、战略布局、客户
- 根据微信的公众号获取公众号的二维码 根据公众号获得二维码的图片
- Sipdroid项目的编译运行
- wordpress知更鸟begin主题添加菜单字体图标
- 分享10款效果惊艳的HTML5图片特效
- 数据处理之特征缩放与编码
- php钉钉机器人,PHP调用钉钉机器人
- Android WIFI列表搜索及无线热点的开启和关闭
- 概要设计 重要性_艺术留学——服装设计
- R 语言消除pdf图片的空白
- iOS开发之自定义键盘(数字,字母类型等随意切换)
- k8s集群配置域名证书支持https与http
热门文章
- A. Ela Sorting Books codeforces 1737A
- vue问题: 解决ERROR in xxx.js from UglifyJs
- Python机器学习基础篇二《监督学习》
- C语言实现五子棋游戏(具体说明+具体步骤)
- ReDim Preserve只能改变数组最末维大小​​​​​​​
- 小白也能重装系统?写给小白的一封信--重装纯净版Win10系统
- 微信小程序-JAVA实现微信支付功能(微信支付2.0)
- 【私人订制Python模块库】Pyemail:更方便快捷地发送电子邮件
- Python 绘制狄拉克 delta 函数(完美实现)
- windows程序员进阶系列:《软件调试》之O--- WinDbg使用介绍