Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8af03fa52b | |||
| d6f54bb0e6 | |||
| db122e8aef | |||
| fce6e8d013 |
@@ -9,35 +9,39 @@ namespace BotPages.Core.Abstractions;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IMessengerAdapter
|
public interface IMessengerAdapter
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Доступные возможности мессенджера.
|
||||||
|
/// </summary>
|
||||||
Capabilities Capabilities { get; }
|
Capabilities Capabilities { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить текстовое сообщение в чат.
|
/// Отправить текстовое сообщение в чат.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task SendTextAsync(string chatId, string text, MessageFormat format,
|
Task<string?> SendTextAsync(string chatId,
|
||||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
string text,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct);
|
MessageFormat format = MessageFormat.Plain,
|
||||||
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
string? messageId = null,
|
||||||
|
CancellationToken ct = default
|
||||||
|
);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить файл в чат.
|
/// Отправить файл в чат.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Task SendFileAsync(string chatId, FileDescriptor file, string? caption, MessageFormat? captionFormat, CancellationToken ct);
|
Task SendFileAsync(string chatId,
|
||||||
|
FileDescriptor file,
|
||||||
|
string? caption = null,
|
||||||
|
MessageFormat? captionFormat = null,
|
||||||
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
CancellationToken ct = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Создать билдер альбома для отправки медиагруппы.
|
/// Создать билдер альбома для отправки медиагруппы.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IAlbumBuilder CreateAlbumBuilder(PageContext ctx);
|
IAlbumBuilder CreateAlbumBuilder(PageContext ctx);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Начать отображение прогресса операции.
|
|
||||||
/// </summary>
|
|
||||||
Task<string?> StartProgressAsync(PageContext ctx, string title, CancellationToken ct);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Обновить прогресс операции.
|
|
||||||
/// </summary>
|
|
||||||
Task UpdateProgressAsync(PageContext ctx, string messageId, string title, int percent, CancellationToken ct);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Вызывается при выходе со страницы.
|
/// Вызывается при выходе со страницы.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -27,47 +27,4 @@ public sealed class PageContext
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this);
|
public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Начать прогресс операции.
|
|
||||||
/// </summary>
|
|
||||||
public async Task<string?> StartProgressAsync(string title, CancellationToken ct)
|
|
||||||
{
|
|
||||||
var messageId = await Adapter.StartProgressAsync(this, title, ct);
|
|
||||||
|
|
||||||
if (messageId != null)
|
|
||||||
{
|
|
||||||
_progressMessageId = messageId;
|
|
||||||
_progressTitle = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
return messageId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Обновить прогресс операции.
|
|
||||||
/// </summary>
|
|
||||||
public Task UpdateProgressAsync(int percent, CancellationToken ct)
|
|
||||||
{
|
|
||||||
if (_progressMessageId != null)
|
|
||||||
{
|
|
||||||
return Adapter.UpdateProgressAsync(this, _progressMessageId, _progressTitle ?? "", percent, ct);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Обновить прогресс операции.
|
|
||||||
/// </summary>
|
|
||||||
public Task UpdateProgressAsync(string messageId, int percent, CancellationToken ct)
|
|
||||||
{
|
|
||||||
return Adapter.UpdateProgressAsync(this, messageId, _progressTitle ?? "", percent, ct);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private string? _progressMessageId = null;
|
|
||||||
private string? _progressTitle = null;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -11,21 +11,31 @@ public static class PageContextAdapterExtensions
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить текстовое сообщение.
|
/// Отправить текстовое сообщение.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static Task SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain,
|
public static Task<string?> SendTextAsync(this PageContext ctx,
|
||||||
|
string text,
|
||||||
|
MessageFormat format = MessageFormat.Plain,
|
||||||
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
string? messageId = null,
|
||||||
CancellationToken ct = default)
|
CancellationToken ct = default)
|
||||||
=> ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, ct);
|
=> ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, messageId, ct);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Отправить файл.
|
/// Отправить файл.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default)
|
public static Task SendFileAsync(this PageContext ctx,
|
||||||
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
FileDescriptor file,
|
||||||
|
string? caption = null,
|
||||||
/// <summary>
|
MessageFormat? captionFormat = null,
|
||||||
/// Отправить файл.
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
/// </summary>
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
CancellationToken ct = default
|
||||||
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, null, ct);
|
)
|
||||||
|
=> ctx.Adapter.SendFileAsync(chatId: ctx.Update.Chat.Id,
|
||||||
|
file: file,
|
||||||
|
caption: caption,
|
||||||
|
captionFormat: captionFormat,
|
||||||
|
inline: inline,
|
||||||
|
reply: reply,
|
||||||
|
ct: ct);
|
||||||
}
|
}
|
||||||
@@ -14,9 +14,8 @@ public sealed class MessageBuilder
|
|||||||
private readonly List<List<ReplyButton>> _reply = new();
|
private readonly List<List<ReplyButton>> _reply = new();
|
||||||
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _files = new();
|
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _files = new();
|
||||||
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new();
|
private readonly List<(FileDescriptor file, string? caption, MessageFormat? captionFormat)> _album = new();
|
||||||
private string? _progressTitle = null;
|
private bool _disableReplyKeyboard;
|
||||||
private int? _progressPercent = null;
|
private string? _editMessageId = null;
|
||||||
private string? _progressMessageId = null;
|
|
||||||
|
|
||||||
/// <summary>Создать билдер сообщений.</summary>
|
/// <summary>Создать билдер сообщений.</summary>
|
||||||
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
||||||
@@ -29,6 +28,13 @@ public sealed class MessageBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Редактировать сообщение.</summary>
|
||||||
|
public MessageBuilder EditMessage(string messagId)
|
||||||
|
{
|
||||||
|
_editMessageId = messagId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Добавить inline‑кнопку.</summary>
|
/// <summary>Добавить inline‑кнопку.</summary>
|
||||||
public MessageBuilder Inline(string label, string value)
|
public MessageBuilder Inline(string label, string value)
|
||||||
{
|
{
|
||||||
@@ -64,9 +70,20 @@ public sealed class MessageBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Отключение Reply клавиатуры.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public MessageBuilder DisableReply()
|
||||||
|
{
|
||||||
|
_disableReplyKeyboard = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Добавить reply‑кнопку.</summary>
|
/// <summary>Добавить reply‑кнопку.</summary>
|
||||||
public MessageBuilder Reply(params ReplyButton[] label)
|
public MessageBuilder Reply(params ReplyButton[] label)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.Add(label.ToList());
|
_reply.Add(label.ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -74,6 +91,7 @@ public sealed class MessageBuilder
|
|||||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||||
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
|
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.Add(row.ToList());
|
_reply.Add(row.ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -81,6 +99,7 @@ public sealed class MessageBuilder
|
|||||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||||
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.AddRange(row.Select(t => t.ToList()).ToList());
|
_reply.AddRange(row.Select(t => t.ToList()).ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -99,26 +118,30 @@ public sealed class MessageBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Установить прогресс операции.</summary>
|
|
||||||
public MessageBuilder Progress(string title, int percent = 0)
|
|
||||||
{
|
|
||||||
_progressTitle = title;
|
|
||||||
_progressPercent = percent;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Отправить собранное сообщение.</summary>
|
/// <summary>Отправить собранное сообщение.</summary>
|
||||||
public async Task SendAsync(CancellationToken ct = default)
|
public async Task<string?> SendAsync(CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
|
string? messageId = null;
|
||||||
|
|
||||||
|
List<List<ReplyButton>>? reply = null;
|
||||||
|
if (_disableReplyKeyboard) reply = new();
|
||||||
|
else if (_reply.Any()) reply = _reply;
|
||||||
|
|
||||||
// Текст
|
// Текст
|
||||||
if (!string.IsNullOrWhiteSpace(_text))
|
if (!string.IsNullOrWhiteSpace(_text))
|
||||||
{
|
{
|
||||||
await _ctx.SendTextAsync(_text, _format, _inline, _reply, ct);
|
messageId = await _ctx.SendTextAsync(_text, _format, _inline, reply, _editMessageId, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Файлы
|
// Файлы
|
||||||
foreach (var (file, caption, captionFormat) in _files)
|
foreach (var (file, caption, captionFormat) in _files)
|
||||||
await _ctx.SendFileAsync(file, caption, captionFormat, ct);
|
await _ctx.SendFileAsync(file: file
|
||||||
|
, caption: caption
|
||||||
|
, captionFormat: captionFormat
|
||||||
|
, reply: reply
|
||||||
|
, inline: _inline
|
||||||
|
, ct: ct
|
||||||
|
);
|
||||||
|
|
||||||
// Альбом
|
// Альбом
|
||||||
if (_album.Count > 0)
|
if (_album.Count > 0)
|
||||||
@@ -129,25 +152,12 @@ public sealed class MessageBuilder
|
|||||||
await builder.SendAsync(ct);
|
await builder.SendAsync(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Прогресс
|
|
||||||
if (_progressTitle is not null)
|
|
||||||
{
|
|
||||||
if (_progressMessageId is null)
|
|
||||||
_progressMessageId = await _ctx.StartProgressAsync(_progressTitle, ct);
|
|
||||||
|
|
||||||
if (_progressPercent > 0 && !string.IsNullOrEmpty(_progressMessageId))
|
|
||||||
await _ctx.UpdateProgressAsync(_progressMessageId, _progressPercent.Value, ct);
|
|
||||||
}
|
|
||||||
|
|
||||||
_text = null;
|
_text = null;
|
||||||
_files.Clear();
|
_files.Clear();
|
||||||
_album.Clear();
|
_album.Clear();
|
||||||
|
|
||||||
if (_progressPercent >= 100)
|
_editMessageId = null;
|
||||||
{
|
|
||||||
_progressTitle = null;
|
return messageId;
|
||||||
_progressMessageId = null;
|
|
||||||
_progressPercent = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,33 +85,44 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task SendTextAsync(string chatId, string text, MessageFormat format,
|
public async Task<string?> SendTextAsync(string chatId, string text,
|
||||||
IEnumerable<IEnumerable<InlineButton>>? inline,
|
MessageFormat format = MessageFormat.Plain,
|
||||||
IEnumerable<IEnumerable<ReplyButton>>? reply, CancellationToken ct)
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
string? messageId = null,
|
||||||
|
CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
if (_client is null)
|
if (_client is null)
|
||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
|
_logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InlineKeyboardMarkup? inlineMarkup = null;
|
||||||
ReplyMarkup? markup = null;
|
ReplyMarkup? markup = null;
|
||||||
|
|
||||||
if (inline is not null && inline.Any())
|
if (inline is not null && inline.Any())
|
||||||
{
|
{
|
||||||
markup = new InlineKeyboardMarkup(
|
inlineMarkup = new InlineKeyboardMarkup(
|
||||||
inline.Select(row => row.Select(b => new InlineKeyboardButton(b.Label, b.Value)).ToArray())
|
inline.Select(row => row.Select(b => new InlineKeyboardButton(b.Label, b.Value)).ToArray())
|
||||||
.ToArray()
|
.ToArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (reply is not null && reply.Any())
|
else if (reply is not null)
|
||||||
{
|
{
|
||||||
markup = new ReplyKeyboardMarkup(
|
if (reply.Any())
|
||||||
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
ResizeKeyboard = true
|
markup = new ReplyKeyboardMarkup(
|
||||||
};
|
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
ResizeKeyboard = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
markup = new ReplyKeyboardRemove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var parseMode = ParseMode.None;
|
var parseMode = ParseMode.None;
|
||||||
@@ -147,17 +158,44 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
text = text.Substring(0, Capabilities.MaxMessageLength);
|
text = text.Substring(0, Capabilities.MaxMessageLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _client.SendMessage(
|
if (!string.IsNullOrWhiteSpace(messageId))
|
||||||
chatId: long.Parse(chatId),
|
{
|
||||||
text: text,
|
await _client.EditMessageText(
|
||||||
parseMode: parseMode,
|
messageId: int.Parse(messageId),
|
||||||
replyMarkup: markup,
|
chatId: long.Parse(chatId),
|
||||||
cancellationToken: ct
|
text: text,
|
||||||
);
|
parseMode: parseMode,
|
||||||
|
replyMarkup: inlineMarkup,
|
||||||
|
cancellationToken: ct
|
||||||
|
);
|
||||||
|
|
||||||
|
return messageId;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (inlineMarkup is not null) markup = inlineMarkup;
|
||||||
|
|
||||||
|
var message = await _client.SendMessage(
|
||||||
|
chatId: long.Parse(chatId),
|
||||||
|
text: text,
|
||||||
|
parseMode: parseMode,
|
||||||
|
replyMarkup: markup,
|
||||||
|
cancellationToken: ct
|
||||||
|
);
|
||||||
|
|
||||||
|
return message.Id.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task SendFileAsync(string chatId, FileDescriptor file, string? caption, MessageFormat? captionFormat, CancellationToken ct)
|
public async Task SendFileAsync(string chatId,
|
||||||
|
FileDescriptor file,
|
||||||
|
string? caption = null,
|
||||||
|
MessageFormat? captionFormat = null,
|
||||||
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
CancellationToken ct = default
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (_client is null)
|
if (_client is null)
|
||||||
{
|
{
|
||||||
@@ -219,20 +257,46 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReplyMarkup? markup = null;
|
||||||
|
|
||||||
|
if (inline is not null && inline.Any())
|
||||||
|
{
|
||||||
|
markup = new InlineKeyboardMarkup(
|
||||||
|
inline.Select(row => row.Select(b => new InlineKeyboardButton(b.Label, b.Value)).ToArray())
|
||||||
|
.ToArray()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (reply is not null)
|
||||||
|
{
|
||||||
|
if (reply.Any())
|
||||||
|
{
|
||||||
|
markup = new ReplyKeyboardMarkup(
|
||||||
|
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
ResizeKeyboard = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
markup = new ReplyKeyboardRemove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// В зависимости от FileKind выбираем подходящий метод
|
// В зависимости от FileKind выбираем подходящий метод
|
||||||
switch (file.Kind)
|
switch (file.Kind)
|
||||||
{
|
{
|
||||||
case FileKind.Photo:
|
case FileKind.Photo:
|
||||||
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
await _client.SendPhoto(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
case FileKind.Video:
|
case FileKind.Video:
|
||||||
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", parseMode, cancellationToken: ct);
|
await _client.SendVideo(long.Parse(chatId), inputFile, caption: caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
case FileKind.Audio:
|
case FileKind.Audio:
|
||||||
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
await _client.SendAudio(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", parseMode, cancellationToken: ct);
|
await _client.SendDocument(long.Parse(chatId), inputFile, caption ?? "", parseMode, replyMarkup: markup, cancellationToken: ct);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,62 +304,6 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IAlbumBuilder CreateAlbumBuilder(PageContext ctx) => new TelegramAlbumBuilder(this, ctx, _logger, _client);
|
public IAlbumBuilder CreateAlbumBuilder(PageContext ctx) => new TelegramAlbumBuilder(this, ctx, _logger, _client);
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task<string?> StartProgressAsync(PageContext ctx, string title, CancellationToken ct)
|
|
||||||
{
|
|
||||||
if (_client is null)
|
|
||||||
{
|
|
||||||
_logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
string text = "0%";
|
|
||||||
if (!string.IsNullOrEmpty(title))
|
|
||||||
{
|
|
||||||
text = title + Environment.NewLine + text;
|
|
||||||
}
|
|
||||||
|
|
||||||
var message = await _client.SendMessage(
|
|
||||||
chatId: long.Parse(ctx.Update.Chat.Id),
|
|
||||||
text: text,
|
|
||||||
cancellationToken: ct
|
|
||||||
);
|
|
||||||
|
|
||||||
return message.Id.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task UpdateProgressAsync(PageContext ctx, string messageId, string title, int percent, CancellationToken ct)
|
|
||||||
{
|
|
||||||
if (_client is null)
|
|
||||||
{
|
|
||||||
_logger.Log(LogLevel.Critical, $"{MessengerType} client is not initialized.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
percent = Math.Clamp(percent, 0, 100);
|
|
||||||
|
|
||||||
string text = $"{percent}%";
|
|
||||||
if (!string.IsNullOrEmpty(title))
|
|
||||||
{
|
|
||||||
text = title + Environment.NewLine + text;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _client.EditMessageText(
|
|
||||||
messageId: int.Parse(messageId),
|
|
||||||
chatId: long.Parse(ctx.Update.Chat.Id),
|
|
||||||
text: text,
|
|
||||||
cancellationToken: ct
|
|
||||||
);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Log(LogLevel.Critical, ex.Message, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task OnLeaveAsync(PageContext ctx, CancellationToken ct) => Task.CompletedTask;
|
public Task OnLeaveAsync(PageContext ctx, CancellationToken ct) => Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
|||||||
{
|
{
|
||||||
_logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends.");
|
_logger.Log(LogLevel.Warn, "Albums not supported. Degraded to sequential sends.");
|
||||||
foreach (var (file, caption, captionFormat) in _items)
|
foreach (var (file, caption, captionFormat) in _items)
|
||||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct: ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ public sealed class TelegramAlbumBuilder : IAlbumBuilder
|
|||||||
{
|
{
|
||||||
// Telegram не поддерживает document в альбомах — деградация
|
// Telegram не поддерживает document в альбомах — деградация
|
||||||
_logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately.");
|
_logger.Log(LogLevel.Warn, $"Document '{file.Kind}' in album not supported. Sending document separately.");
|
||||||
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
await _adapter.SendFileAsync(_ctx.Update.Chat.Id, file, caption, captionFormat, ct: ct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,21 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<Nullable>enable</Nullable>
|
||||||
<Nullable>enable</Nullable>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
<PackageId>BotPages</PackageId>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<Authors>FrigaT</Authors>
|
||||||
|
<Company>FrigaT</Company>
|
||||||
|
<Product>BotPages</Product>
|
||||||
|
<Description>Платформонезависимый framework для создания диалоговых ботов с системой страниц.</Description>
|
||||||
|
<Copyright>Copyright © 2025 FrigaT</Copyright>
|
||||||
|
<RepositoryUrl>https://git.frigat.duckdns.org/FrigaT/BotPages</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PackageProjectUrl>https://git.frigat.duckdns.org/FrigaT/BotPages</PackageProjectUrl>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public sealed class FilesPage : SingletonPage
|
|||||||
{
|
{
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
await ctx.SendFileAsync(file, $"Файл '{file.Name}' получен и отправлен обратно.", ct);
|
await ctx.SendFileAsync(file, $"Файл '{file.Name}' получен и отправлен обратно.", ct: ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Обращение через Storage
|
//Обращение через Storage
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ public sealed class SubmitPage : SingletonPage
|
|||||||
{
|
{
|
||||||
var progress = new MessageBuilder(ctx);
|
var progress = new MessageBuilder(ctx);
|
||||||
|
|
||||||
await progress
|
var messageId = await progress
|
||||||
.Progress("Отправка заявки", 7)
|
.Text("Отправка заявки\n7%")
|
||||||
.SendAsync(ct);
|
.SendAsync(ct);
|
||||||
|
|
||||||
int i = 7;
|
int i = 7;
|
||||||
@@ -22,7 +22,8 @@ public sealed class SubmitPage : SingletonPage
|
|||||||
i += 25;
|
i += 25;
|
||||||
Thread.Sleep(TimeSpan.FromMilliseconds(200));
|
Thread.Sleep(TimeSpan.FromMilliseconds(200));
|
||||||
await progress
|
await progress
|
||||||
.Progress("Отправка заявки", i)
|
.Text($"Отправка заявки\n{i}%")
|
||||||
|
.EditMessage(messageId!)
|
||||||
.SendAsync(ct);
|
.SendAsync(ct);
|
||||||
}
|
}
|
||||||
while (i < 100);
|
while (i < 100);
|
||||||
|
|||||||
Reference in New Issue
Block a user