企业微信机器人

2019-08-16 09:43:22来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

企业微信机器人

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

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

代码如下:

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 + "\n");
            }
            inStream.close();

            result = strber.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
View Code

 

 

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

 


原文链接:https://www.cnblogs.com/stuhjf/p/11127795.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:单列集合框架体系Collection

下一篇:Hadoop介绍与安装