A simple Telegram bot written in Go. It runs as a webhook-based bot, echoes back any text message it receives (with an Echo: prefix), exposes a health/index route, and provides a helper endpoint to set the Telegram webhook URL.
- Echo bot: Replies with
Echo: <your message>to incoming text messages. - HTTP server on port 8000:
/→ returnsHello World(plain text)./telegram/webhook→ Telegram webhook endpoint used by the bot./set-webhook→ helper route to call Telegram'ssetWebhookfor this app.
- Graceful shutdown: Listens for
Ctrl+C/SIGINTand shuts down the HTTP server cleanly.
- Go 1.20+ (or compatible with your environment)
- A Telegram bot token from @BotFather
- (Optional) A public HTTPS URL for webhooks (Koyeb, ngrok, etc.)
The application uses environment variables, typically loaded from a .env file via godotenv.
Required variables:
TELEGRAM_BOT_TOKEN– your bot token from BotFatherTELEGRAM_WEBHOOK_SECRET_TOKEN– secret token used to validate Telegram webhook callsWEBHOOK_BASE_URL– optional base URL used by/set-webhookto build the full webhook URL- If not set, it defaults to
http://localhost:8000.
- If not set, it defaults to
Example .env file:
TELEGRAM_BOT_TOKEN=123456789:example-token
TELEGRAM_WEBHOOK_SECRET_TOKEN=some-secret-string
WEBHOOK_BASE_URL=https://your-app-url.example.com- Install dependencies:
go mod tidy-
Ensure your
.envfile is present in the project root. -
Run the bot:
go run main.goYou should see output similar to:
HTTP server listening on :8000
Then:
- Open
http://localhost:8000/→ you should seeHello World. Ctrl+Cin the terminal will terminate the server gracefully.
The bot exposes a helper route to configure the webhook from your running app.
-
Ensure
WEBHOOK_BASE_URLis set appropriately:- For local testing with a tunnel (e.g. ngrok), set it to your public URL, e.g.
https://<ngrok-id>.ngrok.io. - For Koyeb, use your Koyeb app URL such as
https://<your-app-name>-<region>.koyeb.app.
- For local testing with a tunnel (e.g. ngrok), set it to your public URL, e.g.
-
With the app running, open in your browser or via
curl:
GET /set-webhook
Examples:
curl http://localhost:8000/set-webhook
# or after deployment
curl https://<your-app-url>/set-webhookThe response will show whether Telegram accepted the webhook and the URL it was set to.
main.go– contains:loadEnv– loads environment variables from.env.newBot– creates the Telegram*bot.Botwith the default handler and webhook secret.newMux– builds the HTTP mux and routes (/,/telegram/webhook,/set-webhook).newServer– constructs thehttp.Serveron:8000.startServer– starts the HTTP server and performs graceful shutdown on context cancel.handler– Telegram update handler that logs and echoes incoming text messages.
You can deploy this repository to Koyeb and run it as a web service.
- Push this repository to GitHub (or another supported Git provider).
- Click the Deploy to Koyeb button at the top of this README, which opens the Koyeb dashboard.
- In Koyeb, choose your repository and branch.
- Set the following environment variables in the Koyeb service settings:
TELEGRAM_BOT_TOKENTELEGRAM_WEBHOOK_SECRET_TOKENWEBHOOK_BASE_URL– set to your Koyeb app URL, e.g.https://<your-app-name>-<region>.koyeb.app
- Deploy the service.
After the service is running on Koyeb:
- Visit
https://<your-app-url>/set-webhookonce to register the webhook with Telegram. - Send a message to your bot in Telegram and it should respond with
Echo: <your message>.
- The
.gitignoreignores common Go build artifacts and.envfiles so your secrets are not committed. - Adjust ports, paths, or logging as needed for your environment.