前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot对接企业微信机器人

springboot对接企业微信机器人

作者头像
用户9347382
发布2022-01-10 16:52:47
1.6K0
发布2022-01-10 16:52:47
举报
文章被收录于专栏:程序员阿杰

ROBOTSpringBoot

代码语言:javascript
复制
@Component
public class NewScheduling {
    @Autowired
    private SimpleDateFormat time;
    private static final String WXID = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f0474735-939d-4c44-9a78-8234b7abebe6";
    private static final Logger logger = LoggerFactory.getLogger(NewScheduling.class);
    @Scheduled(fixedDelay = 60000*60*2) //每2小时执行一次
    public void scheduledTaskByFixedDelay() {
        logger.info("定时任务开始 ByFixedDelay:" + time.format(new Date()));
        newsTask();
        logger.info("定时任务结束 ByFixedDelay:" + time.format(new Date()));
    }

设置定时任务 从接口中拿到需要的数据返回实体对象

代码语言:javascript
复制
/*
    定时拿取接口数据
     */
    private void scheduledTask() {
        try {
            String baiduUrl = "https://api.map.baidu.com/weather/v1/?district_id=360111&data_type=all&ak=QLVCURfTor6cr3IekRiK7ebaqLjnqvYN";
            StringBuffer stringBuffer = new StringBuffer();
            URL url = new URL(baiduUrl);
            URLConnection urlConnection = url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
            String line = null;
            while((line = bufferedReader.readLine() )!= null){
                stringBuffer.append(line);
            }
            bufferedReader.close();
            String seccussData = stringBuffer.toString();
            String msg = map(seccussData);
            HttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(WXID);
            httppost.addHeader("Content-Type", "application/json; charset=utf-8");
            JSONObject jsonObject = new JSONObject();
            JSONObject contentObject = new JSONObject();
            contentObject.put("content", msg);
            contentObject.put("mentioned_mobile_list", "@all");
            jsonObject.put("msgtype", "text");
            jsonObject.put("text", contentObject);
            StringEntity stringEntity = new StringEntity(jsonObject.toString(), "utf-8");
            httppost.setEntity(stringEntity);
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                System.out.println(result);
            }
        } catch (MalformedURLException e) {
            logger.info(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String  map (String text){
        JSONObject jsonObject = JSONObject.parseObject(text);
        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();
    }
}

本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名,转载请标明出处 最后编辑时间为: 2021/08/06 10:29

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档