FeishuNetSdk 2.2.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package FeishuNetSdk --version 2.2.7
                    
NuGet\Install-Package FeishuNetSdk -Version 2.2.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FeishuNetSdk" Version="2.2.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FeishuNetSdk" Version="2.2.7" />
                    
Directory.Packages.props
<PackageReference Include="FeishuNetSdk" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FeishuNetSdk --version 2.2.7
                    
#r "nuget: FeishuNetSdk, 2.2.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FeishuNetSdk@2.2.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FeishuNetSdk&version=2.2.7
                    
Install as a Cake Addin
#tool nuget:?package=FeishuNetSdk&version=2.2.7
                    
Install as a Cake Tool

FeishuNetSdk

内置自动缓存和过期重取Token机制。

.NET FeishuNetSdk

飞书开放平台网址:https://open.feishu.cn/

接口清单详见:

TenantAccessToken 适用接口清单

UserAccessToken 适用接口清单

<img src="https://github.com/vicenteyu/FeishuNetSdk/blob/main/business.png" alt="商业合作、定制开发" height="350px">

用法:

1、安装Nuget包

PM> Install-Package FeishuNetSdk

2、服务注册

输入应用凭证的方式

builder.Services.AddFeishuNetSdk(options =>
{
    options.AppId = "cli_test";
    options.AppSecret = "secret_test";
    //options.EnableLogging = true; //启用日志 (true = 启用, false = 关闭, 默认 = 启用)
    //options.IgnoreStatusException = true; //忽略状态异常错误(true = 忽略, false = 启用, 默认 = 忽略)
});

使用配置文件的方式

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 接口使用方法

  1. 跳转登录页面,获取登录预授权码:code
  2. 使用IFeishuAppApi,根据code获取user_access_token
  3. 使用IFeishuUserApi,调用接口。user_access_token默认为第一参数。

示例:

消息卡片(模板消息)

[HttpGet("t2")]
public async Task<IResult> GetT2Async()
{
    var dto = new TemplateCardDto
    {
        Data = new()
        {
            TemplateId = "ctp_xx0123456789",  // <== 模板Id
            TemplateVariable = new()          // <== 模板变量
            {
                { "aa", Aa },
                { "bb", Bb },
                { "cc", Cc }
            }
        }
    };
    var result = await _feishuApi.PostImV1MessagesAsync("open_id",
        new FeishuNetSdk.Im.PostImV1MessagesBodyDto
        {
            MsgType = "interactive",
            ReceiveId = "ou_3c5beeexxxxxx6ce936414bb0d13d386",
            Content = dto.ToString(),
        });
}

文件上传示例

参数类型 FormDataFile 支持 filePathFileInfobyte[]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);
}

云文档操作

文档操作前提需要有编辑权限,步骤如下:

  1. 自建应用添加机器人能力。
  2. 应用机器人加入或创建一个新群组
  3. 进入目标文档,将该群组设置为文档协作者
  4. 调用接口方法。

以下是仅在特殊情况下使用。

关闭接口日志

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 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.5.4 79 9/12/2025
3.5.3 157 9/5/2025
3.5.2 165 8/29/2025
3.5.1 76 8/23/2025
3.5.0 154 8/16/2025
3.4.9 120 8/9/2025
3.4.8 104 8/1/2025
3.4.7 242 7/26/2025
3.4.6 94 7/18/2025
3.4.5 100 7/11/2025
3.4.4 327 7/5/2025
3.4.3 100 6/27/2025
3.4.2 89 6/21/2025
3.4.1 232 6/13/2025
3.4.0 147 6/6/2025
3.3.9 105 5/30/2025
3.3.8 112 5/25/2025
3.3.7 173 5/18/2025
3.3.6 182 5/6/2025
3.3.5 185 4/18/2025
3.3.4 131 4/12/2025
3.3.3 120 4/5/2025
3.3.2 170 3/30/2025
3.3.1 143 3/21/2025
3.3.0 152 3/14/2025
3.2.9 263 3/7/2025
3.2.8 189 2/28/2025
3.2.7 190 2/21/2025
3.2.6 256 2/14/2025
3.2.5 267 2/9/2025
3.2.4 268 2/5/2025
3.2.3 328 1/17/2025
3.2.2 218 1/11/2025
3.2.1 426 1/6/2025
3.2.0 150 12/27/2024
3.1.9 159 12/21/2024
3.1.8 242 12/15/2024
3.1.7 475 12/8/2024
3.1.6 304 11/30/2024
3.1.5 267 11/27/2024
3.1.4 151 11/22/2024
3.1.3 162 11/15/2024
3.1.2 141 11/8/2024
3.1.1 249 11/3/2024
3.1.0 372 10/26/2024
3.0.9 283 10/23/2024
3.0.8 336 10/19/2024
3.0.7 166 10/12/2024
3.0.6 169 10/9/2024
3.0.5 141 9/27/2024
3.0.4 182 9/24/2024
3.0.3 154 9/21/2024
3.0.2 159 9/18/2024
3.0.1 176 9/13/2024
3.0.0 663 9/8/2024
2.4.6 228 9/1/2024
2.4.5 138 8/28/2024
2.4.4 191 8/24/2024
2.4.3 166 8/15/2024
2.4.2 175 8/8/2024
2.4.1 122 8/2/2024
2.4.0 403 7/30/2024
2.3.9 163 7/24/2024
2.3.8 153 7/22/2024
2.3.7 164 7/18/2024
2.3.6 169 7/12/2024
2.3.5 145 7/9/2024
2.3.4 152 7/4/2024
2.3.3 150 7/1/2024
2.3.2 158 6/27/2024
2.3.1 186 6/23/2024
2.3.0 158 6/20/2024
2.2.9 173 6/14/2024
2.2.8 152 6/7/2024
2.2.7 554 6/3/2024
2.2.6 129 5/28/2024
2.2.5 181 5/23/2024
2.2.4 129 5/13/2024
2.2.3 132 5/10/2024
2.2.2 164 4/30/2024
2.2.1 162 4/27/2024
2.2.0 186 4/23/2024
2.1.9.1 153 4/19/2024
2.1.8 222 4/11/2024
2.1.7 229 4/9/2024
2.1.6 157 4/2/2024
2.1.5 173 3/29/2024
2.1.4 151 3/26/2024
2.1.3 212 3/19/2024
2.1.2 183 3/7/2024
2.1.1 182 3/1/2024
2.1.0 180 2/20/2024
2.0.9 177 2/5/2024
2.0.8 386 1/26/2024
2.0.7 165 1/23/2024
2.0.6 187 1/12/2024
2.0.5 167 1/9/2024
2.0.4 170 1/4/2024
2.0.3 185 12/28/2023
2.0.2 182 12/21/2023
2.0.1 1,009 12/12/2023
2.0.0 197 12/6/2023
1.0.13 178 11/13/2023
1.0.12 171 11/12/2023
1.0.11 173 11/9/2023
1.0.10 178 11/7/2023
1.0.9 258 11/2/2023
1.0.8 149 10/31/2023
1.0.7 170 10/27/2023
1.0.6 175 10/26/2023
1.0.5 186 10/26/2023