78 lines
2.5 KiB
C#
Raw Normal View History

2025-08-21 16:59:07 +08:00
using System.Text.Json;
using DeepSeek.Core;
using DeepSeek.Core.Models;
using Mliybs.QQBot.Bots.WebSocket;
namespace AGSS.QQBot
{
public class QQBot:BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
string id = "102802431";
string secret = "VTRPNLJHFEDCBA98888888889ABCDEFH";
using var bot = new WebSocketBot(id, secret); // 此处的id与secret为外部填入的参数
// 该方法会返回ReadyEvent对象只要没有抛出异常就视为连接成功
// 方法参数填入需要订阅的事件,默认不填表示订阅私信与群聊事件(不包括频道私信)
// 使用位或运算订阅多个事件如WebSocketIntent.GroupAndC2cEvent | WebSocketIntent.PublicGuildMessages
await bot.ConnectAsync();
bot.MessageReceived.Subscribe(async x =>
{
if (x.Content == " test")
{
}
else
{
// Create an instance using the apiKey
var client = new DeepSeekClient("sk-e18c1c1fb9c94888a4e59328436640b6");
// Construct the request body
var request = new ChatRequest
{
Messages = [
Message.NewUserMessage(x.Content)
],
// Specify the model
Model = Constant.Model.ChatModel
};
var chatResponse = await client.ChatAsync(request, new CancellationToken());
if (chatResponse is null)
{
Console.WriteLine(client.ErrorMsg);
}
await x.ReplyAsync(chatResponse?.Choices.First().Message?.Content);
}
Console.WriteLine(x.Content);
// 回复消息
});
bot.KeepRunning(); // 如果当前线程为主线程请调用该方法或Console.ReadLine()卡住线程
}
catch (Exception e)
{
Console.WriteLine($"启动失败了{e}");
}
}
}
}