• 企业微信机器人


    今天升级了企业微信,发现新增了一个机器人功能,于是好奇的玩了一下。

    目前发现该机器人主要用途是用来发布定时消息,或者发布监控程序报告。

    代码如下:

    1.控制层

    package com.qt.controller;
    
    import com.qt.service.RobotService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/index")
    public class RobotController {
        @Autowired
        private RobotService robotService;
        /**
         * 测试企业微信机器人
         * @return  String
         */
        @RequestMapping(value = "/testRobot", method = RequestMethod.GET)
        public String testRobot()  {
            return robotService.testRobot();
        }
    }
    View Code

    2.业务逻辑

    package com.qt.service;
    
    import com.qt.utils.InterfaceUtils;
    import org.springframework.stereotype.Service;
    import com.alibaba.fastjson.JSONObject;
    
    @Service
    public class RobotService {
    
        /**
         * 测试企业微信机器人
         * @return
         */
        public String testRobot(){
            String result;
            JSONObject text = new JSONObject();
            text.put("content", "hello world!");
            JSONObject msg = new JSONObject();
            msg.put("msgtype", "text");
            msg.put("text", text);
            String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4e2f766b-419a-4767-ad3d-d3dae461f11a";
            result = InterfaceUtils.sendPost(url,msg.toJSONString(),"");
            return result;
        }
    
    }
    

     3.工具类

    package com.qt.utils;
    
    
    import org.apache.http.HttpHost;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.params.ConnRoutePNames;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class InterfaceUtils {
    
        public static String sendPost(String url, String data,String contentType){
            String result = "";
    
            contentType = "application/json";
            try {
                org.apache.http.client.HttpClient client = new DefaultHttpClient();
    
                //设置代理
                //HttpHost proxy = new HttpHost("192.168.0.0", 8080, "http");
                //client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    
                HttpPost post = new HttpPost(url);
                post.setHeader("Content-Type", contentType);
                post.addHeader("Authorization", "Basic YWRtaW46");
    
                StringEntity s = new StringEntity(data, "utf-8");
                s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,contentType));
                post.setEntity(s);
    
                // 发送请求
                HttpResponse httpResponse = client.execute(post);
    
                // 获取响应输入流
                InputStream inStream = httpResponse.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
                StringBuilder strber = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null){
                    strber.append(line + "
    ");
                }
                inStream.close();
    
                result = strber.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return result;
        }
    }
    View Code

    然而很不幸,才玩了没一会,管理员就把它关闭了。。。

  • 相关阅读:
    Accept Xcode/iOS License to run git
    Public Key Retrieval is not allowed
    No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer
    HttpURLConnection调用Restful接口
    关于线程同步(7种同步方式)
    面试(初级)
    面试题(高级)
    RedisTemplate和StringRedisTemplate的区别 RedisTemplate几种序列化方式比较
    Redis 3.2.1集群 —— Redis-trib.rb工具详解(含原理)
    Redis 3.2.1集群 —— CLUSTER MEET ip port
  • 原文地址:https://www.cnblogs.com/stuhjf/p/11127795.html
Copyright © 2020-2023  润新知