钉钉机器人

  最近写了个小工具,目的是能够通过运行程序,能够让钉钉群里面的机器人发送一条内容,内容的是一条来自查询数据库生成的数据.

  很简单的功能,三个步骤,

  第一步,通过jdbc建立数据库连接,拿到结果集.

  第二步,处理结果集,将数据类型转换成String.

  第三部,将String,通过机器人发送出去.

  代码大致如下:

  

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

//以上是要用到的jar包

//查询工具类
public class FarmHouseReport {
	public static String getFarmHouseReport() throws Exception {
		com.mysql.jdbc.Driver driverOne=new com.mysql.jdbc.Driver();
		 DriverManager.registerDriver(driverOne);
		 //连接的三个参数,建立连接
		 String url="JDBC:Mysql://数据库地址/数据库名称";
		 String name="用户名";
		 String password="密码";
		 Connection conn=DriverManager.getConnection(url,name,password);
		//建立sql信使:
		 Statement st=(Statement) conn.createStatement();
		 String sql="SELECT b.name,c.dep_name,DATE_FORMAT(a.create_time, '%Y-%m-%d'),count(a.id) "
		 		+ "FROM farmhouse_house a,user b,departmant c "
		 		+ "WHERE DATE_FORMAT(a.create_time, '%Y-%m-%d') = DATE_SUB(curdate(), INTERVAL 1 DAY) "
		 		+ "AND a.user_id = b.id AND b.department_id = c.id "
		 		+ "GROUP BY b.name,c.dep_name,DATE_FORMAT(a.create_time, '%Y-%m-%d') "
		 		+ "ORDER BY c.dep_name;";

		//resultset用来接收查询
		 ResultSet rs = st.executeQuery(sql);
		 
		 //声明为全局
		ArrayList<InforEntity> arr = new ArrayList<InforEntity>();
		 
		 while(rs.next())
			{	
			 InforEntity info = new InforEntity();
			 info.setName(rs.getString(1));
			 info.setDepName(rs.getString(2));
			 info.setDate(rs.getString(3));
			 info.setCount(rs.getString(4));
			 arr.add(info);
			}
		
		//构建要发送的内容
		 StringBuffer stb = new StringBuffer();
		 //遍历集合
		 for (InforEntity info : arr) {
			stb.append(info.name);
			stb.append(" ");
			stb.append(info.depName);
			stb.append(" ");
			stb.append(info.date);
			stb.append(" ");
			stb.append(info.count);
			stb.append("\n");
		}
		 String str = stb.toString();
		System.out.println(str);
		return str ;
	}
}

  然后是发送类

public class sendUtils {
	// TOKEN为需要发送机器人的地址
	public static void send(String TOKEN,String stb) throws IOException, ClientProtocolException {
		HttpClient httpclient = HttpClients.createDefault();

        HttpPost httppost = new HttpPost(TOKEN);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");

        String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": "+"\""+stb+"\""+"}}";
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);

        HttpResponse response = httpclient.execute(httppost);
        if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
            String result= EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(result);
        }
	}
}

  

转载于:https://www.cnblogs.com/Mr-Brown/p/9856311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值