Getting Started
Last updated
Add go-bot to a Go module and run a minimal echo bot on one platform.
Prerequisites
| Item | Requirement |
|---|---|
| Go | 1.25.0 or later (go.mod) |
| Telegram | Bot token from BotFather |
| Discord | Bot token, with Message Content Intent enabled in the Developer Portal |
| LINE | Channel secret, long-lived channel access token, and a public HTTPS URL that forwards to your webhook port |
Telegram and Discord connect outbound, so they run behind NAT. LINE pushes events to your server, so it needs an inbound HTTPS endpoint (a reverse proxy such as ngrok works for local testing).
Install
- go get github.com/pardnchiu/go-bot@latest
Import only the platform packages you use:
- import (
- "github.com/pardnchiu/go-bot/core/telegram"
- "github.com/pardnchiu/go-bot/core/discord"
- "github.com/pardnchiu/go-bot/core/line"
- )
Minimal echo bot
Every platform follows the same order:
New, Reply, Start, then Close on shutdown. The handler's non-empty return is sent back as a reply.- package main
- import (
- "context"
- "log"
- "os"
- "os/signal"
- "syscall"
- "github.com/pardnchiu/go-bot/core/telegram"
- )
- func main() {
- bot, err := telegram.New(os.Getenv("TELEGRAM_TOKEN"))
- if err != nil {
- log.Fatal(err)
- }
- bot.Reply(func(ctx context.Context, in telegram.Input) string {
- return "you said: " + in.Text
- })
- ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
- defer stop()
- if err := bot.Start(ctx); err != nil {
- log.Fatal(err)
- }
- <-ctx.Done()
- bot.Close()
- }
The Discord and LINE versions change only the constructor and the
Input type:| Platform | Constructor | Handler input |
|---|---|---|
| Telegram | telegram.New(token) | telegram.Input |
| Discord | discord.New(token) | discord.Input |
| LINE | line.New(secret, token, "8080") | line.Input |
The library does not read environment variables; the variable names above belong to your program.
Next steps
- Bot Lifecycle — what
Startvalidates and howCloseshuts down - Reply Handler — where replies go and what happens on panic
- Platform setup: Telegram, Discord, LINE