如何制作一个企业微信机器人

最近老是看到别人在企业微信群里搞个机器人,每天定时发送消息。闲着蛋疼就了解了一下。

1、创建一个企业微信机器人你会发现它其实就是一个接口。

如何制作一个企业微信机器人_第1张图片

以及有它的配置说明,发送不同格式或类型的消息。

如何制作一个企业微信机器人_第2张图片

2、编辑脚本

导入依赖的jar包


    org.apache.httpcomponents
    httpclient
    4.5.13


    org.apache.httpcomponents
    httpmime
    4.5.13


    org.apache.httpcomponents
    httpcore
    4.4.15

 脚本

package com.ms.wxwork.robot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Robot {

    public static String WX_TOKEN = "https://qyapi.weixin.qq.com/xxxxxxxxxxxxxxxxxxxxxx";
    public static String HOLIDAY = "https://timor.tech/api/holiday/year/";
    public static String WX_WORK = "https://doc.weixin.qq.com/xxxxxxxxxxxxxxxxxxxxxxxxx";
    public HttpClient httpClient;
    public HttpPost httpPost;
    public HttpGet httpGet;
    public Map param;

    public static void main(String[] args) {
        String temp = "";
        Robot robot = new Robot();
        Date date = new Date();

        String format = robot.dateFormat(date, "yyyy-MM-dd");
        Boolean is_holiday = robot.isHoliday(date);
        String holiday = robot.getHoliday(date);
        String week = robot.getWeek(date);

        if (is_holiday){
            if (holiday != null){
                JSONObject jsonObject = JSONObject.parseObject(holiday);
                String name = jsonObject.getString("name");
                String holiday_date = jsonObject.getString("date");
                temp = "今天是:" + holiday_date + "(" + week + "),为" + name + "假日!";
            }else {
                temp = "今天是:" + format+ "(" + week + "),祝大家周末愉快!";
            }
        }else {
            if (holiday != null){
                JSONObject jsonObject = JSONObject.parseObject(holiday);
                String name = jsonObject.getString("name");
                String holiday_date = jsonObject.getString("date");
                temp = "今天是:" + holiday_date + "("+ week +"),为" + name + ",请按时填写日报!"+"\n" + WX_WORK;
            }else {
                temp = "今天是:" + format + "(" + week + "),请按时填写日报!"+"\n" + WX_WORK;
            }
        }
        robot.workReport(temp);
    }

    public Boolean isHoliday(Date date){
        Robot robot = new Robot();
        String week = robot.getWeek(date);
        String holiday = robot.getHoliday(date);
        if (holiday != null){
            JSONObject jsonObject = JSONObject.parseObject(holiday);
            Boolean is_holiday = jsonObject.getBoolean("holiday");
            return is_holiday;
        }else {
            if (week.equalsIgnoreCase("星期六") || week.equalsIgnoreCase("星期日")){
                return true;
            }
        }
        return false;
    }

    public void workReport(String temp){
        try {
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(WX_TOKEN);
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            param = new HashMap();
            Map map = new HashMap();
            map.put("content","温馨提示:"+temp);
            map.put("mentioned_list","@all");
            param.put("msgtype","text");
            param.put("text",map);
            String p = map2json(param);
            StringEntity entity = new StringEntity(p,"utf-8");
            httpPost.setEntity(entity);
            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 (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取今天是周几
     */
    public String getWeek(Date date){
        String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * @param date
     */
    public String getHoliday(Date date){
        try {
            Robot robot = new Robot();
            String format = robot.dateFormat(date, "YYYY-MM");

            httpClient = new DefaultHttpClient();
            httpGet = new HttpGet(HOLIDAY+format);
            HttpResponse response = httpClient.execute(httpGet);
            String result = EntityUtils.toString(response.getEntity(), "utf-8");

            // 根据传进来的日期获取信息
            JSONObject dateInfo = JSONObject.parseObject(result).getJSONObject("holiday").getJSONObject(robot.dateFormat(date, "MM-dd"));
            if (dateInfo == null){
                return null;
            }else {
                return dateInfo.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String map2json(Map map){
        return JSON.toJSONString(map);
    }

    /**
     * @param date
     * @param format
     * @return 格式化后的时间字符
     */
    public String dateFormat(Date date, String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }
}

测试

如何制作一个企业微信机器人_第3张图片

 3、在windows上挂个定时任务

如何制作一个企业微信机器人_第4张图片

 

 

你可能感兴趣的:(接口测试,java,java)