Qishao.DingTalkStream.Robot 2026.8.7

dotnet add package Qishao.DingTalkStream.Robot --version 2026.8.7
                    
NuGet\Install-Package Qishao.DingTalkStream.Robot -Version 2026.8.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="Qishao.DingTalkStream.Robot" Version="2026.8.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Qishao.DingTalkStream.Robot" Version="2026.8.7" />
                    
Directory.Packages.props
<PackageReference Include="Qishao.DingTalkStream.Robot" />
                    
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 Qishao.DingTalkStream.Robot --version 2026.8.7
                    
#r "nuget: Qishao.DingTalkStream.Robot, 2026.8.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 Qishao.DingTalkStream.Robot@2026.8.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=Qishao.DingTalkStream.Robot&version=2026.8.7
                    
Install as a Cake Addin
#tool nuget:?package=Qishao.DingTalkStream.Robot&version=2026.8.7
                    
Install as a Cake Tool

Qishao.DingTalkStream.Robot

Qishao.DingTalkStream.Robot 是 Qishao.DingTalkStream.Core 的机器人消息扩展库,提供机器人回调订阅、机器人消息解析、会话 Webhook 主动发送,以及机器人回调快速回复数据构造能力。

安装

dotnet add package Qishao.DingTalkStream.Robot

目标框架

  • net8.0
  • netstandard2.1

当前项目引用 Qishao.DingTalkStream.Core。netstandard2.1 额外引用 System.Text.Json。

引用方式

当前仓库内使用项目引用:

<ProjectReference Include="..\Qishao.DingTalkStream.Robot\Qishao.DingTalkStream.Robot.csproj" />

Qishao.DingTalkStream.Robot 会同时带入 Qishao.DingTalkStream.Core。

快速开始

注册机器人消息回调订阅和消息处理器:

using Qishao.DingTalkStream.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddDingtalkStream(options =>
        {
            options.ClientId = context.Configuration["ClientId"];
            options.ClientSecret = context.Configuration["ClientSecret"];
            options.AutoReplySystemMessage = true;
        })
        .RegisterIMRobotMessageCallback()
        .AddMessageHandler<RobotMessageHandler>()
        .AddHostServices();
    })
    .Build();

await host.RunAsync();

处理机器人文本消息,并通过会话 Webhook 主动回复:

using Qishao.DingTalkStream.Core;
using Qishao.DingTalkStream.Robot;

public sealed class RobotMessageHandler : IDingTalkStreamMessageHandler
{
    public async Task HandleMessage(MessageEventHanderArgs e)
    {
        if (!e.Headers.IsRobotTopic())
        {
            return;
        }

        var robotMessage = e.GetRobotMessageData();
        if (robotMessage.MsgType != "text")
        {
            return;
        }

        var text = robotMessage.GetTextContent();
        await DingtalkRobotWebhookUtilites.SendTextMessage(
            robotMessage.SessionWebhook,
            $"收到:{text.Content}");
    }
}

机器人订阅

RegisterIMRobotMessageCallback() 注册固定订阅:

字段 值
Type CALLBACK
Topic /v1.0/im/bot/messages/get

判断推送是否为机器人消息:

if (e.Headers.IsRobotTopic())
{
    var robotMessage = e.GetRobotMessageData();
}

消息模型

GetRobotMessageData() 将 DingTalkStreamDataPackage.Data 解析为 ReceivedRobotMessage。

常用属性:

属性 说明
ConversationId 会话 ID。
ChatbotCorpId 加密的机器人所在企业 CorpId。
ChatbotUserId 机器人 UserId。
MsgId 加密消息 ID。
SenderNick 发送者昵称。
SenderStaffId 发送者在企业内的 UserId。
SessionWebhook 当前会话 Webhook 地址。
SessionWebhookExpiredTime 当前会话 Webhook 过期时间。
ConversationType 1 单聊,2 群聊。
ConversationTitle 群聊标题。
IsInAtList 机器人是否在 @ 列表中。
MsgType 消息类型。
AtUsers 被 @ 人信息集合。

接收内容解析

按 MsgType 调用对应扩展方法。类型不匹配时会抛出 ArgumentException。

MsgType 方法 返回内容
text GetTextContent() Content
audio GetAudioContent() DownloadCode、Recognition、可选 Duration
picture GetPictureContent() DownloadCode、PictureDownloadCode
video GetVideoContent() DownloadCode、VideoType、可选 Duration
file GetFileContent() DownloadCode、FileName
richText GetRichTextContent() RichText 列表,元素含 Text、DownloadCode、PictureDownloadCode、Type

两种回复方式

1. 使用会话 Webhook 主动发送

DingtalkRobotWebhookUtilites 通过 SessionWebhook 发送消息,方法返回 bool,只表示 HTTP 响应是否成功。

支持构造和发送:

类型 构造方法 发送方法
Text CreateTextMessage SendTextMessage
Link CreateLinkMessage SendLinkMessage
Markdown CreateMarkdownMessage SendMarkdownMessage
ActionCard CreateActionCardMessage SendActionCardMessage
FeedCard CreateFeedCardMessage SendFeedCardMessage

示例:

await DingtalkRobotWebhookUtilites.SendMarkdownMessage(
    robotMessage.SessionWebhook,
    "处理结果",
    "### 已收到机器人消息");

2. 使用 Stream 回调立即回复

DingtalkStreamRobotUtilities 构造机器人回调响应数据,再交给 DingTalkStreamUtilities.CreateReplyMessage 包装完整响应。

var data = await DingtalkStreamRobotUtilities.CreateReply_Text_MessageDataOfRobot("收到消息");
var reply = await DingTalkStreamUtilities.CreateReplyMessage(e.Headers.MessageId, data);
await e.Reply(reply);

支持构造:

  • CreateReply_Text_MessageDataOfRobot
  • CreateReply_Markdown_MessageDataOfRobot
  • CreateReply_Image_MessageDataOfRobot
  • CreateReply_Link_MessageDataOfRobot
  • CreateReply_ActionCard_MessageDataOfRobot
  • CreateReply_ActionCard2_MessageDataOfRobot
  • CreateReply_Audio_MessageDataOfRobot
  • CreateReply_File_MessageDataOfRobot
  • CreateReply_Video_MessageDataOfRobot

注意点

  • DingtalkRobotWebhookUtilites、DingtalkStreamDataHeadersExtentions 是当前公开 API,拼写保持兼容。
  • SessionWebhook 来自钉钉推送数据,并带有 SessionWebhookExpiredTime,发送前要考虑过期时间。
  • IsRobotTopic() 只判断 headers.topic 是否等于 /v1.0/im/bot/messages/get。
  • GetRobotMessageData() 会解析 Data JSON。若推送数据结构异常,会直接抛出 JSON 或字段访问异常。
  • BtnOrientation 当前枚举值注释和名称存在历史不一致,使用前以实际序列化结果为准。

构建验证

dotnet build .\Qishao.DingTalkStream.Robot\Qishao.DingTalkStream.Robot.csproj --no-restore
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2026.8.7 100 8/7/2026
2026.7.22 123 7/22/2026