mirror of
https://github.com/oshima-studios/OshimaGameModule.git
synced 2025-04-22 11:59:35 +08:00
修改 qqbot 控制器
This commit is contained in:
parent
d5327af742
commit
1bdb4507c5
@ -26,7 +26,7 @@ namespace Oshima.FunGame.WebAPI.Controllers
|
||||
return BadRequest("Payload 格式无效");
|
||||
}
|
||||
|
||||
_logger.LogDebug("收到 Webhook 请求:{payload}", payload);
|
||||
_logger.LogDebug("收到 Webhook 请求:{payload.Op}", payload.Op);
|
||||
|
||||
try
|
||||
{
|
||||
@ -70,7 +70,7 @@ namespace Oshima.FunGame.WebAPI.Controllers
|
||||
byte[] privateKeyBytes = Encoding.UTF8.GetBytes(seed);
|
||||
|
||||
Ed25519 ed25519 = new();
|
||||
|
||||
|
||||
ed25519.FromSeed(privateKeyBytes);
|
||||
|
||||
// 将你的消息转换为 byte[]
|
||||
@ -107,11 +107,15 @@ namespace Oshima.FunGame.WebAPI.Controllers
|
||||
// TODO
|
||||
_logger.LogInformation("收到来自用户 {c2cMessage.Author.UserOpenId} 的消息:{c2cMessage.Content}", c2cMessage.Author.UserOpenId, c2cMessage.Content);
|
||||
// 上传图片
|
||||
var (fileUuid, fileInfo, ttl, error) = await _service.UploadC2CMediaAsync(c2cMessage.Author.UserOpenId, 1, $"{Request.Scheme}://{Request.Host}{Request.PathBase}/images/zi/dj1.png");
|
||||
string url = $"{Request.Scheme}://{Request.Host}{Request.PathBase}/images/zi/dj1.png";
|
||||
var (fileUuid, fileInfo, ttl, error) = await _service.UploadC2CMediaAsync(c2cMessage.Author.UserOpenId, 1, url);
|
||||
_logger.LogDebug("发送的图片地址:{url}", url);
|
||||
if (string.IsNullOrEmpty(error))
|
||||
{
|
||||
// 回复消息
|
||||
await _service.SendC2CMessageAsync(c2cMessage.Author.UserOpenId, $"你发送的消息是:{c2cMessage.Content}", msgId: c2cMessage.Id);
|
||||
// 回复富媒体消息
|
||||
await _service.SendC2CMessageAsync(c2cMessage.Author.UserOpenId, "", msgType: 7, media: new { file_info = fileInfo });
|
||||
await _service.SendC2CMessageAsync(c2cMessage.Author.UserOpenId, "", msgType: 7, media: new { file_info = fileInfo }, msgId: c2cMessage.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -131,7 +135,7 @@ namespace Oshima.FunGame.WebAPI.Controllers
|
||||
// TODO
|
||||
_logger.LogInformation("收到来自群组 {groupAtMessage.GroupOpenId} 的消息:{groupAtMessage.Content}", groupAtMessage.GroupOpenId, groupAtMessage.Content);
|
||||
// 回复消息
|
||||
await _service.SendGroupMessageAsync(groupAtMessage.GroupOpenId, $"你发送的消息是:{groupAtMessage.Content}", msgType: 0);
|
||||
await _service.SendGroupMessageAsync(groupAtMessage.GroupOpenId, $"你发送的消息是:{groupAtMessage.Content}", msgId: groupAtMessage.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,15 +1,16 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Oshima.FunGame.WebAPI.Constant;
|
||||
using Oshima.FunGame.WebAPI.Models;
|
||||
|
||||
namespace Oshima.FunGame.WebAPI.Services
|
||||
{
|
||||
public class QQBotService(IOptions<BotConfig> botConfig, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
|
||||
public class QQBotService(IOptions<BotConfig> botConfig, ILogger<QQBotService> logger, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
|
||||
{
|
||||
private readonly BotConfig _botConfig = botConfig.Value;
|
||||
private readonly ILogger<QQBotService> _logger = logger;
|
||||
private readonly HttpClient _httpClient = httpClientFactory.CreateClient();
|
||||
private readonly IMemoryCache _memoryCache = memoryCache;
|
||||
private const string AccessTokenCacheKey = "QQBotAccessToken";
|
||||
@ -29,7 +30,7 @@ namespace Oshima.FunGame.WebAPI.Services
|
||||
string accessToken = await GetAccessTokenAsync();
|
||||
HttpRequestMessage request = new(HttpMethod.Post, $"https://api.sgroup.qq.com{url}");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("QQBot", accessToken);
|
||||
Statics.RunningPlugin?.Controller.WriteLine($"使用的 Access Token:{accessToken}", Milimoe.FunGame.Core.Library.Constant.LogLevel.Debug);
|
||||
_logger.LogDebug("使用的 Access Token:{accessToken}", accessToken);
|
||||
Dictionary<string, object> requestBody = new()
|
||||
{
|
||||
{ "content", content },
|
||||
@ -50,7 +51,11 @@ namespace Oshima.FunGame.WebAPI.Services
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(requestBody), System.Text.Encoding.UTF8, "application/json");
|
||||
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
string errorBody = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError("状态码:{response.StatusCode},错误信息:{errorBody}", response.StatusCode, errorBody);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(string? fileUuid, string? fileInfo, int ttl, string? error)> UploadC2CMediaAsync(string openid, int fileType, string url)
|
||||
@ -68,7 +73,7 @@ namespace Oshima.FunGame.WebAPI.Services
|
||||
string accessToken = await GetAccessTokenAsync();
|
||||
HttpRequestMessage request = new(HttpMethod.Post, $"https://api.sgroup.qq.com{url}");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("QQBot", accessToken);
|
||||
Statics.RunningPlugin?.Controller.WriteLine($"使用的 Access Token:{accessToken}", Milimoe.FunGame.Core.Library.Constant.LogLevel.Debug);
|
||||
_logger.LogDebug("使用的 Access Token:{accessToken}", accessToken);
|
||||
Dictionary<string, object> requestBody = new()
|
||||
{
|
||||
{ "file_type", fileType },
|
||||
@ -89,7 +94,7 @@ namespace Oshima.FunGame.WebAPI.Services
|
||||
{
|
||||
return (null, null, 0, "反序列化富媒体消息失败。");
|
||||
}
|
||||
Statics.RunningPlugin?.Controller.WriteLine($"接收到的富媒体消息:{mediaResponse.FileInfo}", Milimoe.FunGame.Core.Library.Constant.LogLevel.Debug);
|
||||
_logger.LogDebug("接收到的富媒体消息:{mediaResponse.FileInfo},有效时间:{mediaResponse.Ttl}", mediaResponse.FileInfo, mediaResponse.Ttl);
|
||||
return (mediaResponse.FileUuid, mediaResponse.FileInfo, mediaResponse.Ttl, null);
|
||||
}
|
||||
|
||||
@ -118,7 +123,7 @@ namespace Oshima.FunGame.WebAPI.Services
|
||||
throw new Exception("获取 Access Token 失败!");
|
||||
}
|
||||
_memoryCache.Set(AccessTokenCacheKey, tokenResponse.AccessToken, TimeSpan.FromSeconds(expiresIn - 60));
|
||||
Statics.RunningPlugin?.Controller.WriteLine($"获取到 Access Token:{tokenResponse.AccessToken}", Milimoe.FunGame.Core.Library.Constant.LogLevel.Debug);
|
||||
_logger.LogDebug("获取到 Access Token:{tokenResponse.AccessToken}", tokenResponse.AccessToken);
|
||||
return tokenResponse.AccessToken;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user