添加项目文件。
This commit is contained in:
74
OneBotController.cs
Normal file
74
OneBotController.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OneBotApp.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("onebot")] // 确保路由与LLOneBot配置一致
|
||||
public class OneBotController : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody] dynamic data)
|
||||
{
|
||||
// 检查消息类型是否为私聊消息
|
||||
if (data.message_type == "private")
|
||||
{
|
||||
// 获取消息内容和用户ID
|
||||
var message = data.message;
|
||||
var userId = data.user_id;
|
||||
|
||||
// 检查消息内容是否为"/cx"
|
||||
if (message.ToString().Contains("/cx"))
|
||||
{
|
||||
// 构建回复消息
|
||||
var replyMessage = new
|
||||
{
|
||||
action = "send_private_msg",
|
||||
params_ = new
|
||||
{
|
||||
user_id = userId,
|
||||
message = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "text",
|
||||
data = new
|
||||
{
|
||||
text = "Hello"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 发送回复消息到OneBot服务器
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
// 设置超时时间(可选)
|
||||
client.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
// 序列化回复消息为JSON字符串
|
||||
var json = Newtonsoft.Json.JsonConvert.SerializeObject(replyMessage);
|
||||
// 创建StringContent对象,设置内容类型为application/json
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
// 发送POST请求到OneBot服务器的action端点
|
||||
var response = await client.PostAsync("http://localhost:3000/", content);
|
||||
|
||||
// 检查响应状态码
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
// 记录错误信息
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
Console.WriteLine($"Error sending message to OneBot server: {errorContent}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 返回200 OK响应
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Program.cs
Normal file
40
Program.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
// 创建WebApplicationBuilder实例,用于配置应用
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 添加控制器服务,并配置JSON序列化选项
|
||||
builder.Services.AddControllers()
|
||||
.AddNewtonsoftJson(options =>
|
||||
{
|
||||
// 使用DefaultContractResolver以保留属性名的大小写
|
||||
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
||||
});
|
||||
|
||||
// 注册HttpClient
|
||||
builder.Services.AddHttpClient();
|
||||
|
||||
// 构建WebApplication实例
|
||||
var app = builder.Build();
|
||||
|
||||
// 配置HTTP请求管道
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
// 如果是开发环境,启用开发者异常页面,方便调试
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
// 启用路由中间件,用于处理路由
|
||||
app.UseRouting();
|
||||
|
||||
// 启用授权中间件,用于处理授权
|
||||
app.UseAuthorization();
|
||||
|
||||
// 将控制器映射到路由
|
||||
app.MapControllers();
|
||||
|
||||
// 启动应用
|
||||
app.Run();
|
||||
14
QQBot.csproj
Normal file
14
QQBot.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
QQBot.sln
Normal file
25
QQBot.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36202.13 d17.14
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QQBot", "QQBot.csproj", "{7BC0E27B-5563-4BEF-8DE2-35179FFC531E}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7BC0E27B-5563-4BEF-8DE2-35179FFC531E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7BC0E27B-5563-4BEF-8DE2-35179FFC531E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7BC0E27B-5563-4BEF-8DE2-35179FFC531E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7BC0E27B-5563-4BEF-8DE2-35179FFC531E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {C03E1308-61BA-46DD-8C5B-65B7A711BD6D}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user