v0.5.0

Getting Started

Last updated


Add go-bot to a Go module and run a minimal echo bot on one platform.

Prerequisites


ItemRequirement
Go1.25.0 or later (go.mod)
TelegramBot token from BotFather
DiscordBot token, with Message Content Intent enabled in the Developer Portal
LINEChannel 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


  1. go get github.com/pardnchiu/go-bot@latest

Import only the platform packages you use:
  1. import (
  2. "github.com/pardnchiu/go-bot/core/telegram"
  3. "github.com/pardnchiu/go-bot/core/discord"
  4. "github.com/pardnchiu/go-bot/core/line"
  5. )

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.
  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "github.com/pardnchiu/go-bot/core/telegram"
  9. )
  10. func main() {
  11. bot, err := telegram.New(os.Getenv("TELEGRAM_TOKEN"))
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. bot.Reply(func(ctx context.Context, in telegram.Input) string {
  16. return "you said: " + in.Text
  17. })
  18. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  19. defer stop()
  20. if err := bot.Start(ctx); err != nil {
  21. log.Fatal(err)
  22. }
  23. <-ctx.Done()
  24. bot.Close()
  25. }

The Discord and LINE versions change only the constructor and the Input type:
PlatformConstructorHandler input
Telegramtelegram.New(token)telegram.Input
Discorddiscord.New(token)discord.Input
LINEline.New(secret, token, "8080")line.Input

The library does not read environment variables; the variable names above belong to your program.

Next steps


中文