首先要先进入企业内部成为管理员,因为在钉钉pc端自己定义的机器人只有Webhook,没有企业内部机器人的appkey和appsecert,然后就拿着这两个去获取其他id最后用代码实现
进入钉钉管理后台,更新机器人发送互动卡片(普通版) - 钉钉开放平台,然后登录,去创建一个自己可以测试的企业,现在去创建一个企业,然后创建一个企业内部群,在钉钉后台开发管理创建企业内部群
然后点击我的后台
如果自己是管理员,就直接点击创建应用,如果不是就会象我这样,要去找群主给自己一个管理员权限
进去后,然后点击机器人,创建应用
创建应用
创建好够就去点机器人
进入里面后就是这样,我的机器人是已经上线了,所以不用管,然后在添加应用能力翻到最下面,添加机器人
添加机器人
然后去点击权限配置,在搜索栏输入qyapi_robot_sendmsg
然后申请权限,最后点击版本与发布,然后点击发布或者是上线,我的是完成的,不用管
然后再钉钉上去找到自己的创建的钉钉群,然后就去创建机器人,找到企业机器人
找到自己的创建的机器人
应用他就行了
然后回到这个页面
他就会变成这样
然后翻到最下面 选择
获取到appkey和aooSecret了
已存在的企业内部群,点这个链接根据corpid选择会话JSAPI获取。需要手机钉钉扫描调试获取此参数,然后点这个
点这个
用手机扫码,然后点击运行测试,选择自己的企业内部的群,在调式结果获取到chatId
最后根据这个代码
package org.lanqiao;import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMediaUploadRequest;
import com.dingtalk.api.response.OapiMediaUploadResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taobao.api.FileItem;import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;public class FileUploadAndShare {private static final String APP_KEY = "";private static final String APP_SECRET = "";private static final String CHAT_ID = "";public static String getAccessToken() {String url = "https://oapi.dingtalk.com/gettoken?appkey=" + APP_KEY + "&appsecret=" + APP_SECRET;try {URL obj = new URL(url);HttpURLConnection con = (HttpURLConnection) obj.openConnection();con.setRequestMethod("GET");BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));String inputLine;StringBuffer response = new StringBuffer();while ((inputLine = in.readLine()) != null) {response.append(inputLine);}in.close();Map<String, Object> responseMap = new Gson().fromJson(response.toString(), new TypeToken<Map<String, Object>>(){}.getType());System.out.println((String) responseMap.get("access_token"));return (String) responseMap.get("access_token");} catch (Exception e) {e.printStackTrace();return null;}}public static String uploadFile(String accessToken, File file) {try {DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/media/upload");OapiMediaUploadRequest req = new OapiMediaUploadRequest();req.setType("file");FileItem item = new FileItem(file);req.setMedia(item);req.setTopContentType("multipart/form-data");OapiMediaUploadResponse rsp = client.execute(req, accessToken);System.out.println(rsp.getBody());Map<String, Object> responseMap = new Gson().fromJson(rsp.getBody(), new TypeToken<Map<String, Object>>(){}.getType());String s = (String) responseMap.get("media_id");return s;} catch (Exception e) {e.printStackTrace();return null;}}public static void shareFile(String accessToken, String chatId, String mediaId) {String url = "https://oapi.dingtalk.com/chat/send?access_token=" + accessToken;try {URL obj = new URL(url);HttpURLConnection con = (HttpURLConnection) obj.openConnection();con.setRequestMethod("POST");con.setDoOutput(true);con.setRequestProperty("Content-Type", "application/json");Map<String, Object> requestBody = new HashMap<>();requestBody.put("chatid", chatId);Map<String, Object> msg = new HashMap<>();msg.put("msgtype", "file");Map<String, String> file = new HashMap<>();file.put("media_id", mediaId);msg.put("file", file);requestBody.put("msg", msg);String jsonInputString = new Gson().toJson(requestBody);try(OutputStream os = con.getOutputStream()) {byte[] input = jsonInputString.getBytes("utf-8");os.write(input, 0, input.length);}BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));String inputLine;StringBuffer response = new StringBuffer();while ((inputLine = in.readLine()) != null) {response.append(inputLine);}in.close();System.out.println("Response: " + response.toString());} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {String accessToken = getAccessToken();if (accessToken != null) {File file = new File("C:\\Users\\chenxufei\\Desktop\\tag_import_template(1).xlsx");String mediaId = uploadFile(accessToken, file);if (mediaId != null) {shareFile(accessToken, CHAT_ID, mediaId);}}}
}
获取到id依次给,mian方法里面file给自己excel表地址,然后运行就可以了,如果报错就有可能没有导入包,把import的包复制到ai去问。