调用钉钉接口发送消息
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//获取到AccessToken获取AccessToken需要Appkey和Appsecret,请求是GTE
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
request.Appkey = "dingntkbvkvtjsbpe4di";//Appkey
request.Appsecret = "r1sW4aPW5BDY4OQQ4W7LFD5CBr-uflByXHhtXna536Xit3CobhoIU7qsEG4_hWh9";//Appsecret
request.SetHttpMethod("GET");
OapiGettokenResponse response = client.Execute(request);
//获取到AccessToken
string AccessToken = response.AccessToken;
//根据部门获取到Urid
DefaultDingTalkClient clie = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
req.DeptId = "1";
req.SetHttpMethod("GET");
OapiUserGetDeptMemberResponse rsp = clie.Execute(req, AccessToken);
List<string> userid= rsp.UserIds;
//获取到Urid就是在公司里要发送到那个人的id
string Urid = userid[0];
//发送消息
IDingTalkClient cl = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");
CorpMessageCorpconversationAsyncsendRequest req1 = new CorpMessageCorpconversationAsyncsendRequest();
req1.Msgtype = "oa";//发送消息是以oa的形式发送的,其他的还有text,image等形式
req1.AgentId = 283587777;//微应用ID
req1.UseridList = Urid;//收信息的userId,这个是by公司来区分,在该公司内这是一个唯一标识符
req1.ToAllUser = false;//是否发给所有人
//消息文本
req1.Msgcontent = "{\"message_url\": \"http://dingtalk.com\",\"head\": {\"bgcolor\": \"FFBBBBBB\",\"text\": \"头部标题\"},\"body\": {\"title\": \"正文标题\",\"form\": [{\"key\": \"姓名:\", \"value\": \"张三\" },{\"key\": \"年龄:\", \"value\": \"20\" },{\"key\": \"身高:\", \"value\": \"1.8米\"},{\"key\": \"体重:\",\"value\": \"130斤\"},{\"key\": \"学历:\",\"value\": \"本科\"},{\"key\": \"爱好:\",\"value\": \"打球、听音乐\"}],\"rich\": {\"num\": \"15.6\",\"unit\": \"元\"},\"content\": \"大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本大段文本\",\"image\": \"@lADOADmaWMzazQKA\",\"file_count\": \"3\",\"author\": \"李四 \"}}";
CorpMessageCorpconversationAsyncsendResponse rsp1 = cl.Execute(req1, AccessToken);//发送消息
}
}
}
我创建的是控台应用程序
执行效果如下:
sdk:下载https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02
钉钉开发文档:https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy
重要的参数有:AgentId ,UseridList ,Appkey,Appsecret
注意:获取AccessToken需要Appkey和Appsecret
代码在上面
描述的不太清楚,希望对大家有帮助