kettle实现企业微信发送文件
用途
通过kettle中嵌入java代码,实现企业微信发送文件。
实现效果
脚本总览
转换属性:
生成记录
用于模拟待上传的文件
Java代码
java代码参考:https://blog.csdn.net/huryer/article/details/122762053
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
// 企业ID
private static String corpid = "*****";
// 应用的凭证密钥
private static String corpsecret = "***-*****";
// 消息接收人
private static String touser = "Zhang3";
// 企业应用的id
private static int agentid = 1000003;
String firstnameField;
String lastnameField;
String nameField;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// Let's look up parameters only once for performance reason.
//
if (first) {
touser = getVariable("touser");
corpid = getVariable("corpid");
corpsecret = getVariable("corpsecret");
agentid = Integer.valueOf(getVariable("agentid"));
first=false;
}
// First, get a row from the default input hop
//
Object[] r = getRow();
// If the row object is null, we are done processing.
//
if (r == null) {
setOutputDone();
return false;
}
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
// call wechat
//sendText(name);
// 发送文件
String filePath = get(Fields.In, "filePath").getString(r);
sendFile(filePath);
putRow(data.outputRowMeta, outputRow);
return true;
}
/**
* 发送文本
*
* @param content 文本内容
*/
public void sendText(String content) {
WechatUtil wx = new WechatUtil();
try {
String token = wx.getToken(corpid, corpsecret);
String data = wx.createTextData(touser, agentid, content);
String res = wx.sendMsg(data, token);
logBasic("token >>>" + token);
logBasic("data >>>" + data);
logBasic("res >>>" + res);
} catch (Exception e) {
e.getStackTrace();
}
}
/**
* 发送文件
*
* @param filePath 文件路径
*/
public void sendFile(String filePath) {
WechatUtil wx = new WechatUtil();
try {
String token = wx.getToken(corpid, corpsecret);
String media_id = wx.upload("file", filePath, token);
String data = wx.createFileData(touser, agentid, media_id);
String res = wx.sendMsg(data, token);
logBasic("token >>>" + token);
logBasic("data >>>" + data);
logBasic("res >>>" + res);
} catch (Exception e) {
e.getStackTrace();
}
}
/**
* 发送图片
*
* @param filePath 图片路径
*/
public void sendImage(String filePath) {
WechatUtil wx = new WechatUtil();
try {
String token = wx.getToken(corpid, corpsecret);
String media_id = wx.upload("image", filePath, token);
String data = wx.createImageData(touser, agentid, media_id);
String res = wx.sendMsg(data, token);
logBasic("token >>>" + token);
logBasic("data >>>" + data);
logBasic("res >>>" + res);
} catch (Exception e) {
e.getStackTrace();
}
}
/**
* 企业微信工具类
*
* @author hury
*
*/
class WechatUtil {
public static final String CHAR_SET = "utf-8";
public static final String TOKEN_API = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
public static final String MSG_SEND_API = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
public static final String MEDIA_UPLOAD_API = "https://qyapi.weixin.qq.com/cgi-bin/media/upload";
private CloseableHttpClient httpClient;
private HttpPost httpPost;
private HttpGet httpGet;
private static Logger log = LoggerFactory.getLogger(WechatUtil.class);
/**
* 获取access_token
*
* @param corpid 企业ID
* @param corpsecret 应用的凭证密钥
* @throws IOException
*/
public String getToken(String corpid, String corpsecret) throws IOException {
httpClient = HttpClients.createDefault();
httpGet = new HttpGet(TOKEN_API + "?corpid=" + corpid + "&corpsecret=" + corpsecret);
CloseableHttpResponse res = httpClient.execute(httpGet);
String resString = "";
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
JSONObject jo = JSONObject.parseObject(resString);
return jo.getString("access_token");
} catch (Exception e) {
log.error(e.getMessage());
} finally {
res.close();
}
log.debug("resp:{}", resString);
return resString;
}
/**
* 发送消息
*
* @param data post数据
* @param token 鉴权token
* @return
* @throws IOException
*/
public String sendMsg(String data, String token) throws IOException {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(MSG_SEND_API + "?access_token=" + token);
httpPost.setEntity(new StringEntity(data, CHAR_SET));
CloseableHttpResponse res = httpClient.execute(httpPost);
String resString;
try {
HttpEntity entity = res.getEntity();
resString = EntityUtils.toString(entity, CHAR_SET);
EntityUtils.consume(entity);
} finally {
res.close();
}
log.debug("call [{}], param:{}, res:{}", MSG_SEND_API, data, resString);
return resString;
}
/**
* 创建文本消息
*
* 示例:
*
* <pre>
* {
"touser" : "UserID1|UserID2|UserID3",
"toparty" : "PartyID1|PartyID2",
"totag" : "TagID1 | TagID2",
"msgtype" : "text",
"agentid" : 1,
"text" : {
"content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。"
},
"safe":0,
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
* </pre>
*
* @param touser 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。
* 特殊情况:指定为"@all",则向该企业应用的全部成员发送
* @param agentid 企业应用的id
* @param content 消息内容,最长不超过2048个字节,超过将截断(支持id转译)
*
* @return
*/
public String createTextData(String touser, int agentid, String content) {
Map<String, Object> data = new HashMap<String, Object>();
Map<String, Object> text = new HashMap<String, Object>();
data.put("touser", touser);
data.put("msgtype", "text");
data.put("agentid", agentid);
text.put("content", content);
data.put("text", text);
return JSONObject.toJSONString(data);
}
/**
* 创建文件消息
*
* 示例:
*
* <pre>
* {
"touser" : "UserID1|UserID2|UserID3",
"toparty" : "PartyID1|PartyID2",
"totag" : "TagID1 | TagID2",
"msgtype" : "file",
"agentid" : 1,
"file" : {
"media_id" : "1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o"
},
"safe":0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
* </pre>
*
* @param touser 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。
* 特殊情况:指定为"@all",则向该企业应用的全部成员发送
* @param agentid 企业应用的id
* @param media_id 文件id,可以调用上传临时素材接口获取
*
* @return
*/
public String createFileData(String touser, int agentid, String media_id) {
Map<String, Object> data = new HashMap<String, Object>();
Map<String, Object> file = new HashMap<String, Object>();
data.put("touser", touser);
data.put("msgtype", "file");
data.put("agentid", agentid);
file.put("media_id", media_id);
data.put("file", file);
return JSONObject.toJSONString(data);
}
/**
* 创建图片消息
*
* 示例:
*
* <pre>
* {
"touser" : "UserID1|UserID2|UserID3",
"toparty" : "PartyID1|PartyID2",
"totag" : "TagID1 | TagID2",
"msgtype" : "image",
"agentid" : 1,
"image" : {
"media_id" : "MEDIA_ID"
},
"safe":0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
* </pre>
*
* @param touser 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。
* 特殊情况:指定为"@all",则向该企业应用的全部成员发送
* @param agentid 企业应用的id
* @param media_id 图片媒体文件id,可以调用上传临时素材接口获取
*
* @return
*/
public String createImageData(String touser, int agentid, String media_id) {
Map<String, Object> data = new HashMap<String, Object>();
Map<String, Object> image = new HashMap<String, Object>();
data.put("touser", touser);
data.put("msgtype", "image");
data.put("agentid", agentid);
image.put("media_id", media_id);
data.put("image", image);
return JSONObject.toJSONString(data);
}
/**
* 上传临时素材
*
* <pre>
*上传的媒体文件限制
*所有文件size必须大于5个字节
*图片(image):10MB,支持JPG,PNG格式
*语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式
*视频(video) :10MB,支持MP4格式
*普通文件(file):20MB
* </pre>
*
* @param fileType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
* @param filePath 文件路径
* @param token 调用接口凭证
* @return media_id 媒体文件上传后获取的唯一标识,3天内有效
* @throws Exception
*/
public String upload(String fileType, String filePath, String token) throws Exception {
// 返回结果
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
String uploadUrl = MEDIA_UPLOAD_API + "?access_token=" + token + "&type=" + fileType;
URL url = new URL(uploadUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");// 以POST方式提交表单
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);// POST方式不能使用缓存
// 设置请求头信息
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// 请求正文信息
// 第一部分
StringBuilder sb = new StringBuilder();
sb.append("--");// 必须多两条道
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\"; filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
// 获得输出流
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 输出表头
out.write(sb.toString().getBytes("UTF-8"));
// 文件正文部分
// 把文件以流的方式 推送道URL中
DataInputStream din = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = din.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
}
din.close();
// 结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");// 定义数据最后分割线
out.write(foot);
out.flush();
out.close();
if (HttpsURLConnection.HTTP_OK == conn.getResponseCode()) {
StringBuffer strbuffer = null;
BufferedReader reader = null;
try {
strbuffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String lineString = null;
while ((lineString = reader.readLine()) != null) {
strbuffer.append(lineString);
}
if (result == null) {
result = strbuffer.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
}
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject.getString("media_id");
}
}
参考
https://help.hitachivantara.com/Documentation/Pentaho/9.2/Products/User_Defined_Java_Class