GithubHelp home page GithubHelp logo

zkhssb / qqchannelsharp Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 1.0 248 KB

基于.NET7.0的QQ频道机器人开发框架

License: Apache License 2.0

C# 100.00%
framework qq qq-bot qq-robot qqbot qqchannel

qqchannelsharp's Introduction


QQChannelSharp

使用 .NET 7.0 开发的异步频道机器人开发框架

nuget licenseworkflow

文档

语雀文档

能力

  • OpenApi拥有错误重试能力
  • 支持机器人本地/远程会话管理器 Remote正在计划中
  • 异步开发: 异步OpenApi, 异步事件处理
支持的事件
  • ATMessageEvent - 公域机器人AT消息
  • AudioEvent - 音频事件消息
  • ChannelEvent - 频道事件
  • DirectMessageDeleteEvent - 私信消息删除事件
  • DirectMessageEvent - 私信消息事件
  • ErrorNotifyEvent - WebSocket错误通知事件
  • ForumAuditEvent - 论坛审核事件
  • GuildEvent - 频道事件
  • GuildMemberEvent - 频道成员事件
  • HandlerErrorEvent - 事件处理监听器错误事件
  • InteractionEvent - 互动事件
  • MessageAuditEvent - 消息审核事件
  • MessageDeleteEvent - 消息删除事件
  • MessageEvent - 私域机器人消息事件
  • MessageReactionEvent - 消息表情表态事件
  • PlainEvent - 普通事件
  • PostEvent - 主题帖子事件
  • PublicMessageDeleteEvent - 公域消息撤回事件
  • ReadyEvent - 机器人鉴权成功事件
  • ReplyEvent - 主题帖子回复事件
  • ResumedEvent - 机器人重连成功事件
  • ThreadEvent - 论坛主题事件
待实现接口 (V1)
  • WEBHOOK

问题反馈

该项目属于测试阶段,如遇bug或者有想法/建议 请打开issue

交流

在使用中有任何疑问可以加入我们的QQ频道


依赖的开源库

RestSharp | .NET HTTPAPI客户端


快速开始

新建一个.NET7 项目,并从 Github 或 Nuget 上导入 QQChannelSharp。

在 Program 中引入必要的命名空间

using QQChannelSharp;
using QQChannelSharp.Interfaces;
using QQChannelSharp.Logger;
using QQChannelSharp.OpenApi;
using QQChannelSharp.Sessions;

设置日志

日志默认会启用输出, 且日志等级为 Info

Log.Configuration
	.EnableConsoleOutput()
    .SetLogLevel(LogLevel.Info);

启动本地会话

在启动本地会话之前,需要创建一个机器人信息和 OpenApi。这些数据将最终传递到一个会话管理器中,用于管理机器人的分片和事件的分发。创建完毕后,您可以通过调用会话管理器中的方法来注册一个类,或直接使用运算符相加将会话管理器事件总线中公开的事件注册为事件处理器。

这边演示最简单的 直接使用+=注册事件

需要更高级的订阅请参考文档: 事件订阅

ChannelBotInfo botInfo = new("AppId", "Token", true); // 设置机器人信息
OpenApiOptions options = new OpenApiOptions(botInfo)  // 设置OpenApi
    .UseRetry(); // 启用重试功能
ISessionManager sessionManager = new LocalSessionManager(botInfo, options); // 创建一个本地会话管理器
sessionManager.EventBus.ATMessageEvent += MessageEvent; // 直接注册一个事件 (@消息)
await sessionManager.StartAndWait(); // 阻塞在此处启动 (会自动计算Intents)

// 消息事件处理器
static async ValueTask MessageEvent(ATMessageEvent message)
{
    // 用于回复消息
    var msg = new MessageBody()
        .Text("你在")
        .Segment(new ChannelSegment(message.Message.ChannelID))
        .Text("发送的纯文本为:" + message.Context.GetText())
        .EndLine()
        .Emoji(EmojiType.WoofWoof, EmojiType.Moe)
        .Segment(Segment.Emoji(4));
    await message.Context.ReplyMessageAsync(msg); // 引用并回复消息
}

完整的代码

using QQChannelSharp;
using QQChannelSharp.Interfaces;
using QQChannelSharp.Logger;
using QQChannelSharp.OpenApi;
using QQChannelSharp.Sessions;

Log.Configuration
	.EnableConsoleOutput() // 启用控制台输出 (默认开启)
    .SetLogLevel(LogLevel.Info); // 设置日志输出等级 (默认Info)

ChannelBotInfo botInfo = new("AppId", "Token", true); // 设置机器人信息
OpenApiOptions options = new OpenApiOptions(botInfo)  // 设置OpenApi
    .UseRetry(); // 启用重试功能
ISessionManager sessionManager = new LocalSessionManager(botInfo, options); // 创建一个本地会话管理器
sessionManager.EventBus.ATMessageEvent += MessageEvent; // 直接注册一个事件 (@消息)
await sessionManager.StartAndWait(); // 阻塞在此处启动 (会自动计算Intents)

// 消息事件处理器
static async ValueTask MessageEvent(ATMessageEvent message)
{
    // 用于回复消息
    var msg = new MessageBody()
        .Text("你在")
        .Segment(new ChannelSegment(message.Message.ChannelID))
        .Text("发送的纯文本为:" + message.Context.GetText())
        .EndLine()
        .Emoji(EmojiType.WoofWoof, EmojiType.Moe)
        .Segment(Segment.Emoji(4));
    await message.Context.ReplyMessageAsync(msg); // 引用并回复消息
}

效果

img

qqchannelsharp's People

Contributors

emiyou avatar nukoooo avatar zkhssb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lucas8485

qqchannelsharp's Issues

WsClient AggregateException

--- End of stack trace from previous location ---
   at QQChannelSharp.Client.WsClient.ListeningAsync()
Unhandled exception. System.AggregateException: One or more errors occurred. (Unable to connect to the remote server)
 ---> System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server
 ---> System.Net.Http.HttpRequestException: Resource temporarily unavailable (api.sgroup.qq.com:443)
 ---> System.Net.Sockets.SocketException (11): Resource temporarily unavailable
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|281_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.HttpConnectionWaiter`1.WaitForConnectionAsync(Boolean async, CancellationToken requestCancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.WebSockets.WebSocketHandle.ConnectAsync(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken, ClientWebSocketOptions options)
   at System.Net.WebSockets.WebSocketHandle.ConnectAsync(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken, ClientWebSocketOptions options)
   at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore(Uri uri, HttpMessageInvoker invoker, CancellationToken cancellationToken)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.