FeishuNetSdk 2.3.8
See the version list below for details.
dotnet add package FeishuNetSdk --version 2.3.8
NuGet\Install-Package FeishuNetSdk -Version 2.3.8
<PackageReference Include="FeishuNetSdk" Version="2.3.8" />
paket add FeishuNetSdk --version 2.3.8
#r "nuget: FeishuNetSdk, 2.3.8"
// Install FeishuNetSdk as a Cake Addin #addin nuget:?package=FeishuNetSdk&version=2.3.8 // Install FeishuNetSdk as a Cake Tool #tool nuget:?package=FeishuNetSdk&version=2.3.8
FeishuNetSdk
内置自动缓存和过期重取Token机制。
飞书开放平台网址:https://open.feishu.cn/
接口清单详见:
<img src="https://github.com/vicenteyu/FeishuNetSdk/blob/main/business.png" alt="商业合作、定制开发" height="350px">
用法:
1、安装Nuget包
PM> Install-Package FeishuNetSdk
2、服务注册
(1)输入应用凭证
的方式
builder.Services.AddFeishuNetSdk(options =>
{
options.AppId = "cli_test";
options.AppSecret = "secret_test";
//options.EnableLogging = true; //启用日志 (true = 启用, false = 关闭, 默认 = 启用)
//options.IgnoreStatusException = true; //忽略状态异常错误(true = 忽略, false = 启用, 默认 = 忽略)
});
(2)使用配置文件
的方式
builder.Services.AddFeishuNetSdk(builder.Configuration.GetSection("FeishuNetSdk"));
在appsettings.json
根节点上增加配置:
"FeishuNetSdk": {
"AppId": "cli_test",
"AppSecret": "secret_test",
"EnableLogging": true, //启用日志 (true = 启用, false = 关闭, 默认 = 启用)
"IgnoreStatusException": true //忽略状态异常错误(true = 忽略, false = 启用, 默认 = 忽略)
}
3、注入和调用
public class TestController : ControllerBase
{
private readonly IFeishuTenantApi _tenantApi; // <== TenantAccessToken 适用API
private readonly IFeishuUserApi _userApi; // <== UserAccessToken 适用API
public TestController(IFeishuTenantApi tenantApi, IFeishuUserApi userApi)
{
_tenantApi = tenantApi;
_userApi = userApi;
}
[HttpGet("t2")]
public async Task<IResult> GetT2Async()
{
var result = await _tenantApi.GetImV1ChatsAsync();
return Results.Json(result);
}
}
4、UserAccessToken 接口使用方法
- 跳转授权页面,获取
登录预授权码
:code
。 - 使用
IFeishuAppApi
,根据code
获取user_access_token
。 - 使用
IFeishuUserApi
,调用接口。user_access_token
默认为第一参数。 - 详见完整示例:samples/WebApplication1
部分示例:
扩展方法(v2.2.9 新增)
主要针对复杂参数的扩展,例如元素组合卡片等,可以提高易用性。
- 实例化请求体
- 调用扩展方法
- 调用接口
(1)创建审批实例 请求体 设置控件: SetFormControls
var dto1 = new FeishuNetSdk.Approval.PostApprovalV4InstancesBodyDto();
dto1.SetFormControls(new object[] { //更多对象位于 Approval.Dtos 空间下,ID默认使用Guid,也可以自定义。
new InputFormControl("value1"){ Id = "fixed id" }, // <== 自定义Id
new DateFormControl(new DateTime(2019,1,1)),
new AmountFormControl(10.03m, "USD"),
new DateIntervalFormControl(new(new DateTime(2024,1,1), new DateTime(2024,1,2), 3)),
new ContactFormControl( ["value2","value3"], ["id1","id2"]),
});
await tenantApi.PostApprovalV4InstancesAsync(dto1);
(2)发送消息 请求体 设置消息类型及内容: SetContent
var dto2 = new FeishuNetSdk.Im.PostImV1MessagesBodyDto() { ReceiveId = "ou_3c5beeexxxxxx6ce936414bb0d13d386" }; // <== 接收人Id
dto2.SetContent(new PostContent //富文本消息对象,另外还有文本、消息卡片、群名片、个人名片、图片、视频、音频、文件、表情包,位于 Im.Dtos 空间下。
{
Post = new()
{
ZhCn = new PostContent.PostLanguage() { Title = "title" }
.AddContent([new TextElement(Text: "测试消息"), new AtElement(UserId: "ou_111222333")])
.AddContent([new HrElement()])
.AddContent([new LinkElement("链接地址", "https://123")])
}
});
await tenantApi.PostImV1MessagesAsync("open_id", dto2);
类似方法:
- 回复消息 请求体 PostImV1MessagesByMessageIdReplyBodyDto
- 编辑消息 请求体 PutImV1MessagesByMessageIdBodyDto
(3)批量发送消息 请求体 设置消息类型及内容: SetCardOrContent
var dto3 = new FeishuNetSdk.Im.Spec.PostMessageV4BatchSendBodyDto() { OpenIds = ["ou_18eac85dyyyyyyy9317ad4f02e8bbbb"] }; // <== 接收人Id
dto3.SetCardOrContent(new TemplateCardDto //支持的类型详见 参数说明。
{
Data = new()
{
TemplateId = "ctp_xx0123456789", // <== 模板Id
TemplateVariable = new() // <== 模板变量
{
{ "aa", "Aa" },
{ "bb", "Bb" },
{ "cc", "Cc" }
}
}
});
await tenantApi.PostMessageV4BatchSendAsync(dto3);
类似方法:
- 延时更新消息卡片 请求体 PostInteractiveV1CardUpdateBodyDto
- 更新应用发送的消息卡片 请求体 PatchImV1MessagesByMessageIdBodyDto
(4)查看指定审批定义 响应体 获取序列化的控件信息: GetFormControls
var approval = await tenantApi.GetApprovalV4ApprovalsByApprovalCodeAsync("07CE295D-8FB9-****-886C-E8086E0F9F92");
if (approval.IsSuccess)
Console.WriteLine(approval.Data.GetFormControls());
(5)元素组合卡片(支持图表、表格、表单容器等复杂卡片,详见 Im.Dtos 命名空间)
//初始化图表数据
var d1 = new ChartElement.Data(Values: [new () { { "time", "2:00" }, { "value", 8 } },new () { { "time", "4:00" }, { "value", 9 } }, new () { { "time", "6:00" }, { "value", 11 } }, new () { { "time", "8:00" }, { "value", 14 } }, new () { { "time", "10:00" }, { "value", 16 } }, new () { { "time", "12:00" }, { "value", 17 } }, new () { { "time", "14:00" }, { "value", 17 } }, new () { { "time", "16:00" }, { "value", 16 } }, new () { { "time", "18:00" }, { "value", 15 } }, ]);
//初始化图表
var element = new ChartElement()// <== 定义图表
{
AspectRatio = "16:9",
ChartSpec = new ChartElement.LineSpec() // <== 定义拆线图,另外还有 面积图、柱状图、条形图、环图、饼图、组合图、漏斗图、散点图、雷达图、条形进度图、环形进度图、词云。
{
Title = new("折线图"),
XField = ["time"],
YField = "value",
Data = [d1]
}
};
//初始化 请求体
var dto5 = new FeishuNetSdk.Im.PostImV1MessagesBodyDto() { ReceiveId = "ou_e588d*************9264e4c03fd928f" }
.SetContent(new ElementsCardDto() // <== ElementsCardDto 是元素组合卡片,TemplateCardDto 是模板卡片。
{
Header = new() { Title = new() { Content = "测试图表" } }, // <== 卡片标题
Elements = [element] // <== 卡片元素组合:[ element1, element2 …… ],如果是表单容器组件,还支持链式调用组件:elements.AddElement(element1).AddElement(element2)……。
});
//发送请求
await tenantApi.PostImV1MessagesAsync("open_id", dto5);
文件上传示例
参数类型 FormDataFile
支持 filePath
、FileInfo
、byte[]
、Stream
。
需要注意部分接口注释上有关于文件格式限制的说明。
[HttpGet("t3")]
public async Task<IResult> GetT3Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f1d7a4c4f992724f5de44bb14.jpg";
//调用接口
var result = await _feishuApi.PostImV1ImagesAsync(
new FeishuNetSdk.Im.PostImV1ImagesBodyDto
{
ImageType = "message"
},
new FormDataFile(filePath));
//当返回码异常时,返回错误消息
if (!result.IsSuccess)
return Results.Problem(result.Msg);
return Results.Json(result);
}
文件下载示例
下载操作默认返回HttpResponseMessage
,由于没有返回码(code)
可以判断操作是否成功,所以建议配合 EnsureSuccessStatusCode()
方法使用,当响应状态码异常时,会抛出异常,需要自行捕获处理。
[HttpGet("t4")]
public async Task<IResult> GetT4Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f----1.jpg";
//调用接口
var result = (await _feishuApi.GetImV1ImagesByImageKeyAsync(
image_key: "img_xxxx-fbdc-4c36-b17c-ac8aa1aee7dg"))
//当响应状态码异常时,会抛出异常,需要自行捕获处理
.EnsureSuccessStatusCode();
//保存文件到指定路径
await result.SaveAsAsync(filePath);
return Results.Json(result);
}
个别接口支持部分下载,可以按需设置参数range
,字符串格式为bytes=0-100
表示下载第0
字节到第100
字节的数据,默认不填
或者null
表示下载整个文件。示例如下:
[HttpGet("t5")]
public async Task<IResult> GetT5Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f----2.jpg";
//调用接口
var result = (await _feishuApi.GetDriveV1MediasByFileTokenDownloadAsync(
file_token: "OQBpbF8AEoZ0gqxpCMwcRPWFn8c",
range: "bytes=0-100"))
//当响应状态码异常时,会抛出异常,需要自行捕获处理
.EnsureSuccessStatusCode();
//保存到指定路径,可能只是文件的一部分,并非完整。
await result.SaveAsAsync(filePath);
return Results.Json(result);
}
HTTP状态码为 200 时,表示成功,返回文件二进制流。
HTTP状态码为 206 时,表示下载部分内容成功,返回指定 Range 的部分文件的二进制流。
注意事项:
当获取凭证
异常时,内部异常类型为TokenException
。
try
{
var result = await _feishuApi.GetEventV1OutboundIpAsync();
return Results.Json(result);
}
catch (HttpRequestException ex) when (ex.InnerException is TokenException tokenException)
{
return Results.Problem(tokenException.Message);
}
云文档操作
文档操作前提需要有编辑权限,步骤如下:
- 为
自建应用
添加机器人
能力。 - 将
应用机器人
加入或创建一个新群组
。 - 进入目标文档,将该
群组
设置为文档协作者
。 - 调用接口方法。
以下是仅在特殊情况下使用。
关闭接口日志
true = 启用, false = 关闭, 默认 = 启用
使用配置文件方式可以实时切换。
启用状态异常错误
true = 忽略, false = 启用, 默认 = 忽略
飞书接口在返回结果异常时,同时会返回状态异常,状态异常通常无法实质判断异常原因,具体原因会在返回结果中提示。所以接口默认忽略状态异常。开启状态异常之后需要进行捕获,内部异常为ApiResponseStatusException
。
使用配置文件方式可以实时切换。
try
{
var result = await _feishuApi.GetEventV1OutboundIpAsync();
return Results.Json(result);
}
catch (HttpRequestException ex) when (ex.InnerException is ApiResponseStatusException statusException)
{
// 响应状态码异常
return Results.Problem(statusException.Message);
}
接口重载/覆盖
如果要覆盖方法,比如是在保持参数完全一致的情况下,修改http地址,需要在方法前加 new
(参数不一致是重载,重载不用加new
),然后将新地址更换到属性上。更换http方法、返回参数及其他属性也是同理。
1、新建API,继承于 IFeishuTenantApi
public interface INewApi : IFeishuTenantApi
{
[HttpGet("/open-apis/event/v1/outbound_ip1")]
new System.Threading.Tasks.Task<HttpResponseMessage> GetEventV1OutboundIpAsync();
}
2、注册API
builder.Services.AddHttpApi<INewApi>();
3、修改注入
public class TestController : ControllerBase
{
//此处更改为新的API:INewApi
private readonly INewApi _feishuApi;
public TestController(INewApi feishuApi)
{
_feishuApi = feishuApi;
}
}
启用缓存
默认:不缓存
缓存属性Cache
,在接口上使用表示对接口内所有方法启用,建议仅针对具体方法使用,在单个方法上增加属性即可。数值单位是毫秒
。
public interface INewApi : IFeishuTenantApi
{
[Cache(10 * 1000)]
[HttpGet("/open-apis/event/v1/outbound_ip1")]
new System.Threading.Tasks.Task<HttpResponseMessage> GetEventV1OutboundIpAsync();
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- WebApiClientCore.Extensions.OAuths (>= 2.1.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on FeishuNetSdk:
Package | Downloads |
---|---|
FeishuNetSdk.WebSocket
适用于飞书开放平台的.Net开发包 |
|
FeishuNetSdk.Endpoint
适用于飞书开放平台的.Net开发包 |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.1.5 | 37 | 11/27/2024 |
3.1.4 | 89 | 11/22/2024 |
3.1.3 | 94 | 11/15/2024 |
3.1.2 | 80 | 11/8/2024 |
3.1.1 | 100 | 11/3/2024 |
3.1.0 | 196 | 10/26/2024 |
3.0.9 | 162 | 10/23/2024 |
3.0.8 | 265 | 10/19/2024 |
3.0.7 | 108 | 10/12/2024 |
3.0.6 | 111 | 10/9/2024 |
3.0.5 | 86 | 9/27/2024 |
3.0.4 | 108 | 9/24/2024 |
3.0.3 | 96 | 9/21/2024 |
3.0.2 | 115 | 9/18/2024 |
3.0.1 | 127 | 9/13/2024 |
3.0.0 | 188 | 9/8/2024 |
2.4.6 | 113 | 9/1/2024 |
2.4.5 | 90 | 8/28/2024 |
2.4.4 | 146 | 8/24/2024 |
2.4.3 | 120 | 8/15/2024 |
2.4.2 | 126 | 8/8/2024 |
2.4.1 | 70 | 8/2/2024 |
2.4.0 | 311 | 7/30/2024 |
2.3.9 | 92 | 7/24/2024 |
2.3.8 | 94 | 7/22/2024 |
2.3.7 | 90 | 7/18/2024 |
2.3.6 | 110 | 7/12/2024 |
2.3.5 | 91 | 7/9/2024 |
2.3.4 | 103 | 7/4/2024 |
2.3.3 | 99 | 7/1/2024 |
2.3.2 | 106 | 6/27/2024 |
2.3.1 | 135 | 6/23/2024 |
2.3.0 | 98 | 6/20/2024 |
2.2.9 | 122 | 6/14/2024 |
2.2.8 | 95 | 6/7/2024 |
2.2.7 | 506 | 6/3/2024 |
2.2.6 | 88 | 5/28/2024 |
2.2.5 | 133 | 5/23/2024 |
2.2.4 | 87 | 5/13/2024 |
2.2.3 | 81 | 5/10/2024 |
2.2.2 | 119 | 4/30/2024 |
2.2.1 | 115 | 4/27/2024 |
2.2.0 | 140 | 4/23/2024 |
2.1.9.1 | 109 | 4/19/2024 |
2.1.8 | 173 | 4/11/2024 |
2.1.7 | 184 | 4/9/2024 |
2.1.6 | 110 | 4/2/2024 |
2.1.5 | 121 | 3/29/2024 |
2.1.4 | 104 | 3/26/2024 |
2.1.3 | 166 | 3/19/2024 |
2.1.2 | 136 | 3/7/2024 |
2.1.1 | 133 | 3/1/2024 |
2.1.0 | 123 | 2/20/2024 |
2.0.9 | 141 | 2/5/2024 |
2.0.8 | 219 | 1/26/2024 |
2.0.7 | 122 | 1/23/2024 |
2.0.6 | 140 | 1/12/2024 |
2.0.5 | 126 | 1/9/2024 |
2.0.4 | 125 | 1/4/2024 |
2.0.3 | 141 | 12/28/2023 |
2.0.2 | 141 | 12/21/2023 |
2.0.1 | 965 | 12/12/2023 |
2.0.0 | 167 | 12/6/2023 |
1.0.13 | 148 | 11/13/2023 |
1.0.12 | 141 | 11/12/2023 |
1.0.11 | 145 | 11/9/2023 |
1.0.10 | 144 | 11/7/2023 |
1.0.9 | 228 | 11/2/2023 |
1.0.8 | 121 | 10/31/2023 |
1.0.7 | 140 | 10/27/2023 |
1.0.6 | 141 | 10/26/2023 |
1.0.5 | 146 | 10/26/2023 |