当前位置:网站首页>C#钉钉开发:取得所有员工通讯录和发送工作通知
C#钉钉开发:取得所有员工通讯录和发送工作通知
2022-07-02 06:32:00 【清山博客】
需求描述:
1.通过调用钉钉api获取组织里所有员工的信息(通讯录)。
2.通过调用钉钉api向指定员工发送工作通知。
实现步骤:
一、钉钉开发者中心配置
1.登录钉钉开发者中心 开发者后台统一登录 - 钉钉统一身份认证。
2.创建 企业内部应用-H5微应用。

3.设置应用权限
因为要读取所有员工信息,所以 权限范围 选择所有员工,通讯录管理 权限全部勾上。

4.开发管理:设置服务器出口IP,即你需要调用api的IP地址。

二、代码编写
1.下载钉钉SDK:钉钉官方提供了统一的SDK,使用SDK可以便捷地调用服务端API,支持多种语言,下载地址:服务端SDK下载 - 钉钉开放平台。
2.目前暂不支持一次性获取企业下所有员工userid值,如果开发者希望通过接口实现获取企业下所有员工userid值,请参考以下步骤:
调用获取子部门ID列表接口,逐级遍历获取该企业下所有部门的ID。
通过调用获取部门用户userid列表接口,分别获取每个部门下的员工userid。
3.需要用到的接口
4.封装好的C#类
将代码中的agentId、appkey、appsecret替换为你自己的应用即可。
using System;
using System.Collections.Generic;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
namespace RC114.WinFrom.DutyAlert.Util
{
public class DingDingHelper
{
//【企业内部应用】配置参数(因为获取用户信息需要企业内部应用来进行获取,需要配置应用权限,IP白名单等)
private static readonly long agentId = ***;
private static readonly string appkey = "******";
private static readonly string appsecret = "******";
private static string _accessToken = "";
private static DateTime _accessTokenTime = DateTime.Now.AddMinutes(-1);
public static string GetAccessToken()
{
//没有AccessToken或者AccessToken过期,则重新获取
if (_accessToken.IsEmpty() || _accessTokenTime < DateTime.Now)
{
var client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
var req = new OapiGettokenRequest { Appkey = appkey, Appsecret = appsecret };
req.SetHttpMethod("Get");
var execute = client.Execute(req);
if (execute.Errcode == 0)
{
_accessToken = execute.AccessToken;
_accessTokenTime = DateTime.Now.AddHours(1);
}
}
return _accessToken;
}
/// <summary>
/// 取得所有部门编号
/// </summary>
/// <returns></returns>
public static List<long> GetDepartIds()
{
var client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
var req = new OapiV2DepartmentListsubidRequest { DeptId = 1 };
var res = client.Execute(req, GetAccessToken());
if (res.Errcode == 0) return res.Result.DeptIdList;
return new List<long>();
}
/// <summary>
/// 取得部门下用户列表(仅用户Id)
/// </summary>
/// <param name="depid"></param>
/// <returns></returns>
public static List<string> GetDepartUserIds(long depid)
{
var client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
var req = new OapiUserListidRequest { DeptId = depid };
var res = client.Execute(req, GetAccessToken());
if (res.Errcode == 0)
{
if (res.Result == null) return new List<string>();
if (res.Result.UseridList == null) return new List<string>();
return res.Result.UseridList;
}
return new List<string>();
}
/// <summary>
/// 根据用户UserId取得用户信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static string GetUserInfoByUserId(string userId)
{
var client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
var req = new OapiV2UserGetRequest();
req.Userid = userId;
req.Language = "zh_CN";
var res = client.Execute(req, GetAccessToken());
if (res.Errcode == 0) return res.Result.ToJson();
return "";
}
/// <summary>
/// 开发文档: https://developers.dingtalk.com/document/app/asynchronous-sending-of-enterprise-session-messages
/// 发送工作提醒
/// </summary>
/// <param name="userId"></param>
/// <param name="message"></param>
/// <returns></returns>
public static string SendMsg(string userId, string message)
{
var client =
new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
var request = new OapiMessageCorpconversationAsyncsendV2Request();
request.AgentId = agentId;
request.UseridList = userId;
request.ToAllUser = false;
var msg = new OapiMessageCorpconversationAsyncsendV2Request.MsgDomain();
msg.Msgtype = "text";
msg.Text = new OapiMessageCorpconversationAsyncsendV2Request.TextDomain();
msg.Text.Content = message;
request.Msg_ = msg;
long taskid = 0;
var res = client.Execute(request, GetAccessToken());
if (res.Errcode == 0)
{
taskid = res.TaskId;
}
if (taskid > 0)
{
var client2 =
new DefaultDingTalkClient(
"https://oapi.dingtalk.com/topapi/message/corpconversation/getsendprogress");
OapiMessageCorpconversationGetsendprogressRequest request2 =
new OapiMessageCorpconversationGetsendprogressRequest();
request2.AgentId = agentId;
request2.TaskId = taskid;
OapiMessageCorpconversationGetsendprogressResponse
response = client2.Execute(request2, GetAccessToken());
if (response.Errcode == 0)
{
return "发送成功!";
}
switch (response.Errcode)
{
case 143103: return "企业单应用消息发送人次QPM超限";
case 143104: return "企业每分钟发送人次QPM超限";
case 143105: return "单应用给单人每日推送量超限";
case 143106: return "单应用给单人推送重复消息超限";
case 143203: return "ISV单应用发送给单企业人次QPM超限";
case 143204: return "ISV单应用发送给所有企业人次QPM超限";
case 143205: return "单应用给单人每日推送量超限";
case 143206: return "单应用给单人推送重复消息超限";
case 1430000: return "接受者为空";
case 1430001: return "未识别的grantType";
case 1430002: return "包含违禁内容";
case 1430003: return "触发OAPI系统保护";
case 1430004: return "超级高管保护";
}
}
return "";
}
}
}边栏推荐
- [blackmail virus data recovery] suffix Hydra blackmail virus
- [flask] ORM one-to-one relationship
- C language replaces spaces in strings with%20
- commands out of sync. did you run multiple statements at once
- Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
- Learning C
- Qt QTimer类
- 程序猿学英语-Learning C
- Googlenet network explanation and model building
- Comparable,Comparator,Clonable 接口使用剖析
猜你喜欢
随机推荐
方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
HCIA - application layer
Installing Oracle database 19C for Linux
Minecraft插件服开服
Tensorflow2 keras classification model
Sqli labs level 1
CarSim learning experience - rough translation 1
commands out of sync. did you run multiple statements at once
TCP/IP—传输层
Valin cable: BI application promotes enterprise digital transformation
Matlab other
Tensorflow2 keras 分类模型
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
OpenShift 部署应用
web安全--逻辑越权
OpenFeign 简单使用
What is SQL injection
[flask] ORM one-to-one relationship
kubernetes部署loki日志系统



![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)





