Skip to content

Commit

Permalink
合并
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jan 3, 2023
2 parents 58ede12 + 9bb6a0f commit 2a63ece
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 421 deletions.
25 changes: 9 additions & 16 deletions XinjingdailyBot.Command/SuperCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,17 @@ public SuperCommand(
[TextCmd("RESTART", UserRights.SuperCmd, Description = "重启机器人")]
public async Task ResponseRestart(Message message)
{
_ = Task.Run(async () =>
try
{
try
{
Process.Start(Environment.ProcessPath!);
}
catch (Exception ex)
{
_logger.LogError("遇到错误", ex);
}
await Task.Delay(2000);
Process.Start(Environment.ProcessPath!);
await _botClient.SendCommandReply("机器人即将重启", message);
Environment.Exit(0);
});

var text = "机器人即将重启";
await _botClient.SendCommandReply(text, message);
}
catch (Exception ex)
{
await _botClient.SendCommandReply("启动进程遇到错误", message);
_logger.LogError("启动进程遇到错误", ex);
}
}

/// <summary>
Expand Down
34 changes: 34 additions & 0 deletions XinjingdailyBot.Infrastructure/Attribute/TaskMarkAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using XinjingdailyBot.Infrastructure.Enums;

namespace XinjingdailyBot.Infrastructure.Attribute
{

/// <summary>
/// 用于标记Query命令
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class TaskMarkAttribute : System.Attribute
{
//TimeSpan

///// <summary>
///// 创建特性
///// </summary>
///// <param name="command"></param>
//public TaskMarkAttribute(string command)
//{
// Command = command;
// Rights = UserRights.None;
//}
///// <summary>
///// 创建特性
///// </summary>
///// <param name="command"></param>
///// <param name="rights"></param>
//public TaskMarkAttribute(string command, UserRights rights)
//{
// Command = command;
// Rights = rights;
//}
}
}
2 changes: 1 addition & 1 deletion XinjingdailyBot.Service/Bot/Common/ChannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ChannelService : IChannelService
User IChannelService.BotUser { get => _botUser; }

public ChannelService(
ILogger<ChannelService> logger,
ILogger<ChannelService> logger,
ITelegramBotClient botClient,
IOptions<OptionsSetting> optionsSetting)
{
Expand Down
61 changes: 31 additions & 30 deletions XinjingdailyBot.Service/Bot/Common/ReceiverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,42 @@
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Interface.Bot.Common;

namespace XinjingdailyBot.Service.Bot.Common;

[AppService(ServiceType = typeof(IReceiverService), ServiceLifetime = LifeTime.Scoped)]
public class ReceiverService : IReceiverService
namespace XinjingdailyBot.Service.Bot.Common
{
private readonly ITelegramBotClient _botClient;
private readonly IUpdateService _updateService;
private readonly ILogger<ReceiverService> _logger;
private readonly OptionsSetting _optionsSetting;

public ReceiverService(
ITelegramBotClient botClient,
IUpdateService updateService,
ILogger<ReceiverService> logger,
IOptions<OptionsSetting> options)
[AppService(ServiceType = typeof(IReceiverService), ServiceLifetime = LifeTime.Scoped)]
public class ReceiverService : IReceiverService
{
_botClient = botClient;
_updateService = updateService;
_logger = logger;
_optionsSetting = options.Value;
}
private readonly ITelegramBotClient _botClient;
private readonly IUpdateService _updateService;
private readonly ILogger<ReceiverService> _logger;
private readonly OptionsSetting _optionsSetting;

public async Task ReceiveAsync(CancellationToken stoppingToken)
{
ReceiverOptions receiverOptions = new()
public ReceiverService(
ITelegramBotClient botClient,
IUpdateService updateService,
ILogger<ReceiverService> logger,
IOptions<OptionsSetting> options)
{
_botClient = botClient;
_updateService = updateService;
_logger = logger;
_optionsSetting = options.Value;
}

public async Task ReceiveAsync(CancellationToken stoppingToken)
{
AllowedUpdates = Array.Empty<UpdateType>(),
ThrowPendingUpdates = _optionsSetting.Bot.ThrowPendingUpdates,
};
ReceiverOptions receiverOptions = new()
{
AllowedUpdates = Array.Empty<UpdateType>(),
ThrowPendingUpdates = _optionsSetting.Bot.ThrowPendingUpdates,
};

_logger.LogInformation("接收服务运行中...");
_logger.LogInformation("接收服务运行中...");

await _botClient.ReceiveAsync(
updateHandler: _updateService,
receiverOptions: receiverOptions,
cancellationToken: stoppingToken);
await _botClient.ReceiveAsync(
updateHandler: _updateService,
receiverOptions: receiverOptions,
cancellationToken: stoppingToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace XinjingdailyBot.Service.Tasks
namespace XinjingdailyBot.Service.Bot.Common
{
public class TaskService : IHostedService, IDisposable
{
Expand Down
93 changes: 47 additions & 46 deletions XinjingdailyBot.Service/Bot/Common/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,66 @@
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Data;

namespace XinjingdailyBot.Service.Bot.Common;

[AppService(ServiceType = typeof(IUpdateService), ServiceLifetime = LifeTime.Scoped)]
public class UpdateService : IUpdateService
namespace XinjingdailyBot.Service.Bot.Common
{
private readonly ILogger<UpdateService> _logger;
private readonly IUserService _userService;
private readonly IDispatcherService _dispatcherService;

public UpdateService(
ILogger<UpdateService> logger,
IUserService userService,
IDispatcherService dispatcherService)
[AppService(ServiceType = typeof(IUpdateService), ServiceLifetime = LifeTime.Scoped)]
public class UpdateService : IUpdateService
{
_logger = logger;
_userService = userService;
_dispatcherService = dispatcherService;
}
private readonly ILogger<UpdateService> _logger;
private readonly IUserService _userService;
private readonly IDispatcherService _dispatcherService;

public async Task HandleUpdateAsync(ITelegramBotClient _, Update update, CancellationToken cancellationToken)
{
_logger.LogUpdate(update);

var dbUser = await _userService.FetchUserFromUpdate(update);

if (dbUser == null)
public UpdateService(
ILogger<UpdateService> logger,
IUserService userService,
IDispatcherService dispatcherService)
{
return;
_logger = logger;
_userService = userService;
_dispatcherService = dispatcherService;
}

var handler = update.Type switch
public async Task HandleUpdateAsync(ITelegramBotClient _, Update update, CancellationToken cancellationToken)
{
UpdateType.ChannelPost => _dispatcherService.OnChannalPostReceived(dbUser, update.ChannelPost!),
UpdateType.Message => _dispatcherService.OnMessageReceived(dbUser, update.Message!),
UpdateType.CallbackQuery => _dispatcherService.OnCallbackQueryReceived(dbUser, update.CallbackQuery!),
//UpdateType.InlineQuery
//UpdateType.ChosenInlineResult,
_ => null
};
_logger.LogUpdate(update);

if (handler != null)
{
await handler;
var dbUser = await _userService.FetchUserFromUpdate(update);

if (dbUser == null)
{
return;
}

var handler = update.Type switch
{
UpdateType.ChannelPost => _dispatcherService.OnChannalPostReceived(dbUser, update.ChannelPost!),
UpdateType.Message => _dispatcherService.OnMessageReceived(dbUser, update.Message!),
UpdateType.CallbackQuery => _dispatcherService.OnCallbackQueryReceived(dbUser, update.CallbackQuery!),
//UpdateType.InlineQuery
//UpdateType.ChosenInlineResult,
_ => null
};

if (handler != null)
{
await handler;
}
}
}

public async Task HandlePollingErrorAsync(ITelegramBotClient _, Exception exception, CancellationToken cancellationToken)
{
var ErrorMessage = exception switch
public async Task HandlePollingErrorAsync(ITelegramBotClient _, Exception exception, CancellationToken cancellationToken)
{
ApiRequestException apiRequestException => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};

_logger.LogInformation("处理轮询出错: {ErrorMessage}", ErrorMessage);
_logger.LogInformation("处理轮询出错: {ErrorMessage}", ErrorMessage);

if (exception is RequestException)
{
await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
if (exception is RequestException)
{
await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
}
}
}
}
Loading

0 comments on commit 2a63ece

Please sign in to comment.