FeishuNetSdk 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package FeishuNetSdk --version 1.0.5
NuGet\Install-Package FeishuNetSdk -Version 1.0.5
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="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FeishuNetSdk --version 1.0.5
#r "nuget: FeishuNetSdk, 1.0.5"
#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.
// Install FeishuNetSdk as a Cake Addin
#addin nuget:?package=FeishuNetSdk&version=1.0.5

// Install FeishuNetSdk as a Cake Tool
#tool nuget:?package=FeishuNetSdk&version=1.0.5

FeishuNetSdk

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

接口清单详见:TenantAccessToken适用接口清单-969个

自建应用用法

安装Nuget包

FeishuNetSdk

依赖注册

builder.Services.AddHttpApi<IFeishuApi>();
builder.Services.AddHttpApi<IFeishuTenantApi>();
builder.Services.AddTokenProvider<IFeishuTenantApi>(async service =>
{
    var token = await service.GetService<IFeishuApi>()!
        .PostAuthV3TenantAccessTokenInternalAsync(
            new FeishuNetSdk.Auth.Spec.PostAuthV3TenantAccessTokenInternalBodyDto
            {
                AppId = "cli_a5bf8739dab8d00c",
                AppSecret = "vn7MjifCNm04dUlWBg6yWbijHvEpel6G"
            });
    return new TokenResult { Access_token = token.TenantAccessToken, Expires_in = token.Expire };
});

方法调用

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    private readonly IFeishuTenantApi _feishuApi;
    public TestController(IFeishuTenantApi feishuApi)
    {
        _feishuApi = feishuApi;
    }

    [HttpGet("t2")]
    public async Task<IResult> GetT2Async()
    {
        var result2 = await _feishuApi.GetImV1ChatsAsync();
        return Results.Json(result2);
    }
}

文件上传示例

参数 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));

    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(
    	"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(
        "OQBpbF8AEoZ0gqxpCMwcRPWFn8c",
        "bytes=0-100"))
        .EnsureSuccessStatusCode();
    await result.SaveAsAsync(filePath);
    return Results.Json(result);
}

云文档操作

文档操作前需要用户授权,操作步骤如下:

  1. 将应用机器人加入或创建一个新群组。
  2. 将该群组设置为文档协作者。
  3. 继续调用接口方法。

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

接口重载/覆盖

  1. 新建API,继承于 IFeishuTenantApi
  2. 使用重载/覆盖方法

如果要覆盖方法,比如是在保持参数完全一致的情况下,修改http地址,需要在方法前加 new (参数不一致是重载,重载不用加new ),然后将新地址更换到属性上。更换http方法、返回参数及其他属性也是同理。

新建API
public interface INewApi : IFeishuTenantApi
{
    [HttpGet("/open-apis/event/v1/outbound_ip1")]
    new System.Threading.Tasks.Task<HttpResponseMessage> GetEventV1OutboundIpAsync();
}
新增依赖注册
builder.Services.AddHttpApi<INewApi>();
修改方法调用
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    private readonly INewApi _feishuApi; //此处更改为新的API:INewApi
    public TestController(INewApi feishuApi)
    {
        _feishuApi = feishuApi;
    }

    [HttpGet("t2")]
    public async Task<IResult> GetT2Async()
    {
        var result2 = await _feishuApi.GetEventV1OutboundIpAsync();
        return Results.Json(result2);
    }
}

启用状态异常错误

默认:关闭

飞书接口在返回结果异常时,同时会返回状态异常,状态异常通常无法实质判断异常原因,具体原因会在返回结果中提示。所以接口默认忽略状态异常,开启方法如下:

  1. 新建API,继承于 IFeishuTenantApi
  2. 添加接口属性 IgnoreStatusExceptionFilter 并将属性 Enable 设置为 false ,意为停用忽略,即启用异常提示。
  3. 参照上述使用新接口进行方法调用。
[IgnoreStatusExceptionFilter(Enable = false)]
public interface INewApi : IFeishuTenantApi
{ }

开启状态异常之后的捕获方法:

try
{
    var result2 = await _feishuApi.GetEventV1OutboundIpAsync();
    return Results.Json(result2);
}
catch (HttpRequestException ex) when (ex.InnerException is ApiResponseStatusException statusException)
{
    // 响应状态码异常
    return Results.Problem(statusException.Message);
}

关闭接口日志

默认:开启

  1. 新建API,继承于 IFeishuTenantApi
  2. 添加接口属性 LoggingFilter 并设置属性 Enablefalse ,即可停用日志。
  3. 参照上述使用新接口进行方法调用。
  4. LoggingFilterIgnoreStatusExceptionFilter 可以同时存在。
[LoggingFilter(Enable = false), IgnoreStatusExceptionFilter(Enable = false)]
public interface INewApi : IFeishuTenantApi
{ }

需要注意: IFeishuApiIFeishuTenantApi 各有独立的 LoggingFilterIgnoreStatusExceptionFilter 属性,若想全部关闭,需要分别继承接口并将属性 Enable 设置为 false

启用缓存

默认:关闭

缓存属性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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.4 55 5/13/2024
2.2.3 53 5/10/2024
2.2.2 92 4/30/2024
2.2.1 90 4/27/2024
2.2.0 91 4/23/2024
2.1.9.1 81 4/19/2024
2.1.8 105 4/11/2024
2.1.7 110 4/9/2024
2.1.6 84 4/2/2024
2.1.5 90 3/29/2024
2.1.4 77 3/26/2024
2.1.3 132 3/19/2024
2.1.2 104 3/7/2024
2.1.1 104 3/1/2024
2.1.0 96 2/20/2024
2.0.9 102 2/5/2024
2.0.8 150 1/26/2024
2.0.7 89 1/23/2024
2.0.6 110 1/12/2024
2.0.5 100 1/9/2024
2.0.4 103 1/4/2024
2.0.3 111 12/28/2023
2.0.2 110 12/21/2023
2.0.1 418 12/12/2023
2.0.0 138 12/6/2023
1.0.13 123 11/13/2023
1.0.12 111 11/12/2023
1.0.11 108 11/9/2023
1.0.10 111 11/7/2023
1.0.9 198 11/2/2023
1.0.8 91 10/31/2023
1.0.7 106 10/27/2023
1.0.6 105 10/26/2023
1.0.5 114 10/26/2023