跳转到主要内容
调用本接口,使用消息模板发送工作通知。

接口调用说明

在调用本接口前,确保已经在开发者后台配置并启用了消息模板。详情请参考创建和配置消息模板。
  • 单次发送人数最大1000。
  • 每分钟接收人数最大5000。
  • 给同一员工,每天只能发送一条内容相同的消息。
  • 每天给每个员工最多可发送100条。

请求

基本信息
HTTP URLhttps://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate
HTTP MethodPOST
支持的应用类型appType-第三方企业应用
权限要求permission-qyapi_base-调用企业API时需要具备的基本权限

查询参数

名称类型是否必填示例值描述
access_tokenStringBe3xxxx调用该接口的应用凭证,通过获取第三方企业的access_token接口获取。

请求体

名称类型是否必填示例值描述
agent_idNumber948060598应用的agentid,可调用获取企业授权信息接口获取。
template_idStringe27a9eed42b34a14a2xxxx消息模板ID。 在开发者后台应用的开发管理页面查看。
userid_listString”123,456”接收者的用户userId列表。最大列表长度为5000。
dept_id_listString421897262接收者的部门id列表。最大列表长度为500。 重要 dept_id_listuserid_list不能同时为空。
dataString{"name":"淘宝 6","name2":"http://www.taobao.com"}消息模板动态参数赋值数据。说明 key 和 value 均为字符串格式。

请求示例

curl -X POST "https://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate" \
-H 'Content-Type:application/x-www-form-urlencoded;charset=utf-8' \
-d 'access_token=bdefeb04-96ad-40d9-bb92-a62a99d2e8d3' \
-d 'agent_id=123' \
-d 'data=%7B%5C%22%E5%90%8D%E5%AD%97%5C%22%3A%5C%22%E5%BC%A0%E4%B8%89%5C%22%7D' \
-d 'dept_id_list=%5C%22123%2C456%5C%22' \
-d 'template_id=xxxx' \
-d 'userid_list=%5C%22123%2C456%5C%22'
Java
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate");
OapiMessageCorpconversationSendbytemplateRequest req = new OapiMessageCorpconversationSendbytemplateRequest();
req.setAgentId(948060598L);
req.setDeptIdList("421897262");
req.setUseridList("123,456");
req.setTemplateId("e27a9eed42b34a14a2xxxx");
req.setData("{\"name\":\"淘宝6\",\"name2\":\"http://www.taobao.com\"}");
OapiMessageCorpconversationSendbytemplateResponse rsp = client.execute(req, access_token);
System.out.println(rsp.getBody());
Python
import dingtalk.api

req=dingtalk.api.OapiMessageCorpconversationSendbytemplateRequest("https://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate")

req.agent_id=123
req.userid_list=""123,456""
req.dept_id_list=""123,456""
req.template_id="xxxx"
req.data="{\"名字\":\"张三\"}"
try:
  resp= req.getResponse(access_token)
  print(resp)
except Exception,e:
  print(e)
PHP
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiMessageCorpconversationSendbytemplateRequest;
$req->setAgentId("123");
$req->setUseridList("\"123,456\"");
$req->setDeptIdList("\"123,456\"");
$req->setTemplateId("xxxx");
$req->setData("{\"名字\":\"张三\"}");
$resp = $c->execute($req, $access_token, "https://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate");
C#
IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.io/topapi/message/corpconversation/sendbytemplate");
OapiMessageCorpconversationSendbytemplateRequest req = new OapiMessageCorpconversationSendbytemplateRequest();
req.AgentId = 123L;
req.UseridList = "\"123,456\"";
req.DeptIdList = "\"123,456\"";
req.TemplateId = "xxxx";
req.Data = "{\"名字\":\"张三\"}";
OapiMessageCorpconversationSendbytemplateResponse rsp = client.Execute(req, access_token);
Console.WriteLine(rsp.Body);

响应

响应体

名称类型示例值描述
errmsgStringok返回码描述。
errcodeNumber0返回码。
task_idNumber282632271040创建的异步发送任务ID。
request_idString52izpwz1p0iw请求ID。

响应体示例

{
  "errcode": 0,
  "errmsg": "ok",
  "task_id": 282632271040,
  "request_id": "52izpwz1p0iw"
}

错误码

若调用该接口报错,可根据错误信息在全局错误码文档中查找解决方案。
错误码(errorcode)错误信息描述(errmsg)解决方案
885001消息模板不存在确认消息模板是否正确
885006消息模板不可用消息模板需要先审批通过
400002agentId不合法确认agentId是否正确
500系统异常出现未知的系统异常