Enhance Telegram bot functionality with message management and new API methods
Updated the ITelegramApi interface to include a method for deleting messages and modified SendMessageAsync to return the message ID. Enhanced the TelegramBotService to manage message visibility by replacing previous messages with new ones, ensuring a cleaner user experience. Introduced properties in TelegramSubscriber to track the latest menu and notice message IDs. Updated related tests to verify the new message management behavior and adjusted documentation to reflect these changes.
This commit is contained in:
@@ -235,6 +235,67 @@ public class TelegramBotTests
|
||||
Assert.Equal(1, sent!.Split("Симпсоны").Length - 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Menu_ReplacesThePreviousOne()
|
||||
{
|
||||
// Два меню в чате — это две панели с разными галочками, и одна из них врёт. Старое снимаем.
|
||||
var fixture = new TestDb();
|
||||
var channel = Channel.Create("Мультреалити", "mult", Now);
|
||||
channel.UpdateSettings(channel.Name, isEnabled: true, null);
|
||||
var subscriber = TelegramSubscriber.Create(100, Guid.NewGuid(), "viewer", Now);
|
||||
|
||||
await using (var seed = fixture.New())
|
||||
{
|
||||
seed.Channels.Add(channel);
|
||||
seed.TelegramSubscribers.Add(subscriber);
|
||||
await seed.SaveChangesAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
var api = Substitute.For<ITelegramApi>();
|
||||
api.SendMessageAsync(
|
||||
Arg.Any<TelegramSettings>(),
|
||||
Arg.Any<long>(),
|
||||
Arg.Any<string>(),
|
||||
Arg.Any<IReadOnlyList<IReadOnlyList<TelegramButton>>?>(),
|
||||
Arg.Any<CancellationToken>()
|
||||
)
|
||||
.Returns(777L);
|
||||
|
||||
await using (var db = fixture.New())
|
||||
{
|
||||
await new TelegramBotService(db, api).HandleAsync(
|
||||
Settings(),
|
||||
Message(100, "/start"),
|
||||
Now,
|
||||
CancellationToken.None
|
||||
);
|
||||
}
|
||||
|
||||
await using (var check = fixture.New())
|
||||
{
|
||||
Assert.Equal(777L, check.TelegramSubscribers.Single().MenuMessageId);
|
||||
}
|
||||
|
||||
await using (var db = fixture.New())
|
||||
{
|
||||
await new TelegramBotService(db, api).HandleAsync(
|
||||
Settings(),
|
||||
Message(100, "/start"),
|
||||
Now,
|
||||
CancellationToken.None
|
||||
);
|
||||
}
|
||||
|
||||
// Второй заход снял предыдущее меню, а не оставил его висеть.
|
||||
await api.Received(1)
|
||||
.DeleteMessageAsync(
|
||||
Arg.Any<TelegramSettings>(),
|
||||
100,
|
||||
777L,
|
||||
Arg.Any<CancellationToken>()
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Callback_FromUnknownChat_IsRefused()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user