feat: add OnReply & SendVideo & SendImages
This commit is contained in:
parent
bd5044be42
commit
d398585129
@ -16,6 +16,8 @@ public class BotHandler : IUpdateHandler
|
|||||||
private readonly TelegramOptions _telegramOptions;
|
private readonly TelegramOptions _telegramOptions;
|
||||||
private readonly List<Chat> _subscribers = [];
|
private readonly List<Chat> _subscribers = [];
|
||||||
private readonly Dictionary<string, BotCommand> _commands;
|
private readonly Dictionary<string, BotCommand> _commands;
|
||||||
|
|
||||||
|
public Func<TextMessage, Task>? OnReply { get; set; } = null;
|
||||||
|
|
||||||
private readonly IBotClient _bot;
|
private readonly IBotClient _bot;
|
||||||
|
|
||||||
@ -113,6 +115,20 @@ public class BotHandler : IUpdateHandler
|
|||||||
await SndPhoto(subscriber, stream);
|
await SndPhoto(subscriber, stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task SendVideo(long chatId, string path)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Sending video to {ChatId}", chatId);
|
||||||
|
await using var stream = File.OpenRead(path);
|
||||||
|
var send = new SendVideoFile(chatId.ToString(), stream);
|
||||||
|
var response = await _bot.HandleAsync(send);
|
||||||
|
if (!response.Ok)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error sending video.");
|
||||||
|
}
|
||||||
|
_logger.LogInformation("Video sent to {ChatId}", chatId);
|
||||||
|
stream.Close();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task UpdatePhotos(string[] paths)
|
public async Task UpdatePhotos(string[] paths)
|
||||||
{
|
{
|
||||||
@ -133,7 +149,10 @@ public class BotHandler : IUpdateHandler
|
|||||||
private async Task UpdatePhotosInt(string[] paths)
|
private async Task UpdatePhotosInt(string[] paths)
|
||||||
{
|
{
|
||||||
var streams = paths.Select(File.OpenRead).ToList();
|
var streams = paths.Select(File.OpenRead).ToList();
|
||||||
var photos = streams.Select(stream => new PhotoFile(stream)).Cast<IGroupableMedia>().ToList();
|
var photos = streams.Select(stream => new PhotoFile(stream)
|
||||||
|
{
|
||||||
|
Caption = Path.GetFileNameWithoutExtension(stream.Name)
|
||||||
|
}).Cast<IGroupableMedia>().ToList();
|
||||||
|
|
||||||
var s0 = _subscribers.FirstOrDefault()?.Id.ToString();
|
var s0 = _subscribers.FirstOrDefault()?.Id.ToString();
|
||||||
if(s0 is null)
|
if(s0 is null)
|
||||||
@ -180,12 +199,44 @@ public class BotHandler : IUpdateHandler
|
|||||||
await _bot.HandleAsync(send);
|
await _bot.HandleAsync(send);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Send photos to a specific chat
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="chatId"></param>
|
||||||
|
/// <param name="paths"></param>
|
||||||
|
public async Task SendPhotos(long chatId, string[] paths)
|
||||||
|
{
|
||||||
|
var streams = paths.Select(File.OpenRead).ToArray();
|
||||||
|
var photos = streams.Select(stream => new PhotoFile(stream)
|
||||||
|
{
|
||||||
|
Caption = Path.GetFileNameWithoutExtension(stream.Name)
|
||||||
|
}).Cast<IGroupableMedia>().ToArray();
|
||||||
|
|
||||||
|
var request = new SendMediaGroup(chatId.ToString(), photos);
|
||||||
|
var response = await _bot.HandleAsync(request);
|
||||||
|
if (!response.Ok)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error sending photos.");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var stream in streams)
|
||||||
|
{
|
||||||
|
stream.Close();
|
||||||
|
await stream.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async Task OnMessage(TextMessage msg)
|
private async Task OnMessage(TextMessage msg)
|
||||||
{
|
{
|
||||||
if(!_commands.TryGetValue(msg.Text, out var command))
|
if(!_commands.TryGetValue(msg.Text, out var command))
|
||||||
{
|
{
|
||||||
|
if (msg.ReplyToMessage != null && OnReply is not null)
|
||||||
|
{
|
||||||
|
await OnReply(msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
await SendHelp(msg);
|
await SendHelp(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user