企業微信機器人

今天升級了企業微信,發現新增了一個機器人功能,因而好奇的玩了一下。java

目前發現該機器人主要用途是用來發布定時消息,或者發佈監控程序報告。web

代碼以下:spring

1.控制層apache

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.業務邏輯json

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.工具類api

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

 

 

然而很不幸,才玩了沒一會,管理員就把它關閉了。。。微信

相關文章
相關標籤/搜索