v0.5.1

시작하기

마지막 업데이트


go-bot을 Go module에 추가하고 하나의 플랫폼에서 최소 구성의 에코 bot을 실행합니다.

사전 요구 사항


항목요구 사항
Go1.25.0 이상 (go.mod)
TelegramBotFather에서 발급받은 Bot token
DiscordBot token. Developer Portal에서 Message Content Intent를 활성화해야 합니다
LINEChannel secret, 장기 channel access token, webhook 포트로 전달되는 공개 HTTPS URL

Telegram과 Discord는 아웃바운드로 연결하므로 NAT 뒤에서도 동작합니다. LINE은 이벤트를 서버로 푸시하므로 인바운드 HTTPS 엔드포인트가 필요합니다(로컬 테스트에는 ngrok 같은 리버스 프록시를 사용할 수 있습니다).

설치


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

사용하는 플랫폼 패키지만 import합니다.
  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. )

최소 구성의 에코 bot


모든 플랫폼은 같은 순서를 따릅니다. New, Reply, Start, 그리고 종료 시 Close입니다. handler가 비어 있지 않은 값을 반환하면 그 값이 답장으로 전송됩니다.
  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. }

Discord와 LINE 버전은 생성자와 Input 타입만 다릅니다.
플랫폼생성자Handler 입력
Telegramtelegram.New(token)telegram.Input
Discorddiscord.New(token)discord.Input
LINEline.New(secret, token, "8080")line.Input

라이브러리는 환경 변수를 읽지 않습니다. 위의 변수 이름은 여러분의 프로그램에 속한 것입니다.

다음 단계