当前位置:网站首页>C nail development: obtain all employee address books and send work notices
C nail development: obtain all employee address books and send work notices
2022-07-02 08:46:00 【Qingshan blog】
Requirements describe :
1. By calling nails api Get information about all employees in the organization ( Mail list ).
2. By calling nails api Send work notices to designated employees .
Implementation steps :
One 、 Nailing Developer Center Configuration
1. Log in to nailing Developer Center Developers log in uniformly in the background - Nail unified identity authentication .
2. establish Enterprise internal application -H5 Microapplication .
3. Set application permissions
Because you need to read all employee information , therefore Scope of authority Select all employees , Address book management Check all permissions .
4. Development management : Set the server exit IP, That is, you need to call api Of IP Address .
Two 、 Code writing
1. Download nail SDK: Nail officials provide a unified SDK, Use SDK You can easily call the server API, Support for multiple languages , Download address : Server side SDK download - Nail open platform .
2. At present, it is not supported to obtain all employees of the enterprise at one time userid value , If developers want to obtain all employees of the enterprise through the interface userid value , Please refer to the following steps :
call Get sub department ID list Interface , Traverse step by step to get the ID.
By calling Get department users userid list Interface , Get the employees of each department separately userid.
Reference resources : Obtain the information of all employees of the enterprise - Nail open platform
3. Interfaces needed
(1) Get sub department ID list - Nail open platform
(2) Get department users userid list - Nail open platform
(3) according to userid Get user details - Nail open platform
(4) Send work notice - Nail open platform
4. Packaged C# class
Put... In the code agentId、appkey、appsecret Replace it with your own application .
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
{
//【 Enterprise internal application 】 Configuration parameters ( Because obtaining user information requires enterprise internal applications , Application permissions need to be configured ,IP White list, etc )
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()
{
// No, AccessToken perhaps AccessToken Be overdue , Then we can get back
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>
/// Get all department numbers
/// </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>
/// Get the user list under the Department ( Only users 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>
/// According to the user UserId Get user information
/// </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>
/// Developing documents : https://developers.dingtalk.com/document/app/asynchronous-sending-of-enterprise-session-messages
/// Send work reminders
/// </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 " Send successfully !";
}
switch (response.Errcode)
{
case 143103: return " Enterprise single application message sending person times QPM Transfinite ";
case 143104: return " Enterprises send people per minute QPM Transfinite ";
case 143105: return " The daily push volume of a single application to a single person exceeds the limit ";
case 143106: return " A single application pushes repeated messages to a single person, exceeding the limit ";
case 143203: return "ISV The number of people sent by a single application to a single enterprise QPM Transfinite ";
case 143204: return "ISV A single application is sent to all enterprises QPM Transfinite ";
case 143205: return " The daily push volume of a single application to a single person exceeds the limit ";
case 143206: return " A single application pushes repeated messages to a single person, exceeding the limit ";
case 1430000: return " Receiver is empty ";
case 1430001: return " Unrecognized grantType";
case 1430002: return " Contains prohibited content ";
case 1430003: return " Trigger OAPI System protection ";
case 1430004: return " Super Executive Protection ";
}
}
return "";
}
}
}
边栏推荐
猜你喜欢
随机推荐
实现双向链表(带傀儡节点)
D interface and domain problems
HCIA—應用層
zipkin 简单使用
Hcia - Application Layer
Openshift deployment application
gocv拆分颜色通道
Web security -- core defense mechanism
Zipkin is easy to use
Tcp/ip - transport layer
Data asset management function
Minecraft群组服开服
汉诺塔问题的求解与分析
HCIA - application layer
TCP/IP—传输层
[blackmail virus data recovery] suffix Hydra blackmail virus
旋转链表(图解说明)
IP protocol and IP address
Qt的拖动事件
Linux二进制安装Oracle Database 19c