access_token 是钉钉群机器人的id
java版
package io.renren.common.utils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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;
import java.io.IOException;
public class SendTextMessage {
public static String WEBHOOK_TOKEN = "https://oapi.dingtalk.com/robot/send?access_token=0c8c8c01f2b93a7358c13743e7e79443e13a317b306883ab78e83339b19a321c";
public static HttpClient httpclient = HttpClients.createDefault();
public static void sendDingDing(String msg) throws IOException {
HttpPost httppost = new HttpPost(WEBHOOK_TOKEN);
httppost.addHeader("Content-Type", "application/json; charset=utf-8");
String format = String.format("{ \"msgtype\": \"text\", \"text\": {\"content\": \"%s\"}}", msg);
StringEntity se = new StringEntity(format, "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);
}
}
public static void main(String[] args) throws IOException {
sendDingDing("测试");
}
}
python版
先安装依赖
pip install DingtalkChatbot
from dingtalkchatbot.chatbot import DingtalkChatbot
# 初始化机器人小丁
webhook = 'https://oapi.dingtalk.com/robot/send?access_token={access_token}'.format(access_token='0c8c8c01f2b93a7358c13743e7e79443e13a317b306883ab78e83339b19a321c') # 填写你自己创建的机器人
xiaoding = DingtalkChatbot(webhook)
xiaoding.send_text(msg='你好~~~请自我介绍!')