Skip to content

Commit 74f99fb

Browse files
CopilotLazuliKao
andcommitted
Implement automatic Telegram bot command registration
Co-authored-by: LazuliKao <46601807+LazuliKao@users.noreply.github.com>
1 parent 2a26387 commit 74f99fb

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/HuaJiBot.NET.Adapter.Telegram/TelegramAdapter.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Telegram.Bot.Exceptions;
77
using Telegram.Bot.Types;
88
using Telegram.Bot.Types.Enums;
9+
using BotCommand = Telegram.Bot.Types.BotCommand;
910

1011
namespace HuaJiBot.NET.Adapter.Telegram;
1112

@@ -61,6 +62,9 @@ public override async Task SetupServiceAsync()
6162
return Task.CompletedTask;
6263
};
6364

65+
// Subscribe to OnInitialized event to register commands after all plugins are loaded
66+
Events.OnInitialized += RegisterBotCommandsAsync;
67+
6468
Log("Telegram bot is receiving messages...");
6569
}
6670
catch (Exception ex)
@@ -412,4 +416,67 @@ private async Task HandleUpdateAsync(Update update)
412416
LogError("Error handling Telegram update", ex);
413417
}
414418
}
419+
420+
private async void RegisterBotCommandsAsync(object? sender, BotServiceBase service)
421+
{
422+
try
423+
{
424+
// Collect all commands from all loaded plugins
425+
var commands = new List<BotCommand>();
426+
427+
foreach (var (entryPoint, plugin) in Internal.Plugins)
428+
{
429+
if (!plugin.Enabled)
430+
continue;
431+
432+
foreach (var commandInfo in plugin.GetAllCommands())
433+
{
434+
// Telegram bot commands must be lowercase and can only contain letters, digits and underscores
435+
// Maximum length is 32 characters
436+
var commandName = commandInfo.Name.ToLowerInvariant();
437+
438+
// Check if command name contains only valid characters (letters, digits, underscores)
439+
if (!System.Text.RegularExpressions.Regex.IsMatch(commandName, @"^[a-z0-9_]+$"))
440+
{
441+
LogDebug($"Skipping command '{commandInfo.Name}' - contains invalid characters for Telegram (only a-z, 0-9, _ allowed)");
442+
continue;
443+
}
444+
445+
if (commandName.Length > 32)
446+
{
447+
LogDebug($"Skipping command '{commandInfo.Name}' - name too long for Telegram (max 32 chars)");
448+
continue;
449+
}
450+
451+
// Telegram command description max length is 256 characters
452+
var description = commandInfo.Description;
453+
if (description.Length > 256)
454+
{
455+
description = description.Substring(0, 253) + "...";
456+
}
457+
458+
commands.Add(new BotCommand
459+
{
460+
Command = commandName,
461+
Description = description
462+
});
463+
}
464+
}
465+
466+
if (commands.Count > 0)
467+
{
468+
// Register commands with Telegram Bot API
469+
await _botClient.SetMyCommands(commands, cancellationToken: _cancellationTokenSource.Token);
470+
Log($"Successfully registered {commands.Count} commands with Telegram Bot API");
471+
}
472+
else
473+
{
474+
Log("No commands to register with Telegram Bot API");
475+
}
476+
}
477+
catch (Exception ex)
478+
{
479+
LogError("Failed to register bot commands with Telegram", ex);
480+
}
481+
}
415482
}

0 commit comments

Comments
 (0)