BotSharp is a .NET library for developing QQ bots. It provides a simple and flexible way to interact with the QQ Bot platform, handling webhook events and providing services for sending messages.
- Handles QQ Bot webhook events
- Provides a client for interacting with the QQ Bot API
- Includes services for sending and receiving messages
- Extensible and configurable
You can install the BotSharp library via NuGet Package Manager:
dotnet add package BotSharpTo use BotSharp, you need to configure the BotOptions in your appsettings.json file:
{
"QQBot": {
"AppId": "YOUR_APP_ID",
"AppSecret": "YOUR_APP_SECRET",
"Token": "YOUR_TOKEN"
}
}Then, in your Program.cs file, add the QQBot services:
using BotSharp.Extensions;
using BotSharp.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddQQBot();
builder.Services.Configure<BotOptions>(builder.Configuration.GetSection("QQBot"));
// ...Finally, create a webhook endpoint to receive events from the QQ Bot platform:
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using BotSharp.Interfaces;
using BotSharp.Models;
namespace BotSharp.Examples.Controllers
{
[ApiController]
[Route("/[controller]")]
public class WebhookController : ControllerBase
{
private readonly ILogger<WebhookController> _logger;
private readonly IQBotClient _botClient;
private readonly IWebhookValidator _webhookValidator;
private readonly IMessageService _messageService;
public WebhookController(ILogger<WebhookController> logger, IQBotClient botClient, IWebhookValidator webhookValidator, IMessageService messageService)
{
_logger = logger;
_botClient = botClient;
_webhookValidator = webhookValidator;
_messageService = messageService;
_botClient.OnAtMessageCreated += OnAtMessageCreated;
}
[HttpPost]
public async Task<IActionResult> Post()
{
using var reader = new StreamReader(Request.Body);
var payload = await reader.ReadToEndAsync();
Request.Headers.TryGetValue("X-Tencent-Signature", out var signature);
Request.Headers.TryGetValue("X-Tencent-Timestamp", out var timestamp);
Request.Headers.TryGetValue("X-Tencent-Nonce", out var nonce);
if (!await _webhookValidator.ValidateSignatureAsync(payload, signature!, timestamp!, nonce!))
{
_logger.LogWarning("Signature validation failed.");
return Unauthorized();
}
await _botClient.ProcessWebhookPayloadAsync(payload);
return Ok();
}
private void OnAtMessageCreated(Message message)
{
_logger.LogInformation($"Received message from {message.Author.Username}: {message.Content}");
_messageService.SendMessageAsync(message.ChannelId, "Hello, world!");
}
}
}Contributions are welcome! Please feel free to submit a pull request.
This section documents the public API of the BotSharp library.
The main client for interacting with the QQ Bot.
Properties
User? CurrentUser { get; }- Description: Gets the current user information after a successful connection.
Events
event Action<ReadyPayload> OnReady- Description: Triggered when the bot is ready and has successfully connected.
event Action<Message> OnAtMessageCreated- Description: Triggered when an @ message is received.
Methods
Task ProcessWebhookPayloadAsync(string payload)- Description: Processes the raw payload received from the QQ Bot webhook.
- Parameters:
payload(string): The JSON payload from the webhook.
Service for sending messages.
Methods
Task<Message> SendMessageAsync(string channelId, string message)- Description: Sends a message to a specified channel.
- Parameters:
channelId(string): The ID of the channel to send the message to.message(string): The content of the message.
- Returns: A
Taskcontaining the sentMessageobject.
Service for handling authentication.
Methods
Task<string> GetAccessTokenAsync()- Description: Gets the access token required for API calls.
- Returns: A
Taskcontaining the access token string.
Service for validating incoming webhooks.
Methods
Task<bool> ValidateSignatureAsync(string payload, string signature, string timestamp, string nonce)- Description: Validates the signature of a webhook request to ensure it's from Tencent.
- Parameters:
payload(string): The request body.signature(string): The value of theX-Tencent-Signatureheader.timestamp(string): The value of theX-Tencent-Timestampheader.nonce(string): The value of theX-Tencent-Nonceheader.
- Returns: A
Taskcontainingtrueif the signature is valid, otherwisefalse.