Skip to content

Telegram Bots

Telegram can run through a public webhook or through long polling. Use webhook mode for hosted production apps. Use polling for local development, private hosts, or environments that cannot receive public Telegram webhooks.

Webhook Quick Start

csharp
using HPD.Agent.Bots.Telegram;

builder.Services.AddTelegramBot(options =>
{
    options.BotToken = builder.Configuration["Telegram:BotToken"]!;
    options.SecretToken = builder.Configuration["Telegram:SecretToken"];
    options.UserName = builder.Configuration["Telegram:UserName"];
    options.AgentId = builder.Configuration["Agent:Id"] ?? "support-agent";
    options.Mode = TelegramBotMode.Webhook;
}, registerInfrastructure: true);

var app = builder.Build();

app.MapTelegramWebhook("/telegram/webhook");

Then set Telegram's webhook URL:

bash
curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setWebhook" \
  -d "url=https://your-domain/telegram/webhook" \
  -d "secret_token=$TELEGRAM_WEBHOOK_SECRET_TOKEN"

When SecretToken is configured, HPD verifies Telegram's x-telegram-bot-api-secret-token header.

Polling Quick Start

csharp
builder.Services.AddTelegramBotWithPolling(options =>
{
    options.BotToken = builder.Configuration["Telegram:BotToken"]!;
    options.UserName = builder.Configuration["Telegram:UserName"];
    options.AgentId = builder.Configuration["Agent:Id"] ?? "support-agent";
    options.Mode = TelegramBotMode.Polling;
});

Do not map MapTelegramWebhook(...) in polling mode. Telegram webhooks and polling are mutually exclusive.

Configuration

OptionRequiredNotes
BotTokenYesTelegram bot token. Can also come from TELEGRAM_BOT_TOKEN.
SecretTokenWebhook optionalVerifies webhook requests. Can also come from TELEGRAM_WEBHOOK_SECRET_TOKEN.
UserNameRecommendedBot username for reliable group mention detection. Can also come from TELEGRAM_BOT_USERNAME.
ApiBaseUrlOptionalDefaults to Telegram API. Can also come from TELEGRAM_API_BASE_URL.
AgentIdYesHPD agent definition to run.
ModeYesWebhook, Polling, or Auto.
LongPollingPolling onlyTimeout, limit, allowed updates, delete-webhook, and drop-pending-updates options.
StreamingDebounceMsOptionalControls edit cadence for streamed output.

Auto only produces polling behavior when AddTelegramBotWithPolling(...) registered the polling service. The polling service polls only when Telegram has no webhook URL and the runtime does not look serverless.

Conversation Behavior

Thread keys are telegram:{ChatId} or telegram:{ChatId}:{MessageThreadId}. Direct messages always process. Groups and channels should mention the bot or use a bot command mention; configure UserName for reliable detection.

Capabilities And Limits

AreaHPD Telegram behavior
StreamingEdits messages as output arrives.
Text sizeTelegram messages split around the 4096 character limit.
FilesInbound files can become HPD input files; outbound post supports one document per message.
CardsRender as inline keyboards where possible.
Callback dataTelegram callback data is limited to 64 bytes.
HistoryTelegram does not expose full historical thread listing; HPD can only use cached/runtime context.
PermissionsHPD permission requests are currently denied automatically in the Telegram streaming path.

Troubleshooting

SymptomCheck
Webhook receives 401/403SecretToken does not match Telegram's secret_token.
Polling receives nothingA webhook may still be set; delete the webhook or enable polling cleanup options.
Group messages are ignoredBot username is missing, the bot was not mentioned, or privacy settings block messages.

Built for production .NET agent applications.