fluid-telegram/cmd/adapter/main.go

59 lines
1.7 KiB
Go
Raw Normal View History

// Command adapter runs the R-1 Telegram publishing adapter for one channel.
//
// It owns rendering and delivery only: it has no MTProto session and no
// provisioning code path, so a compromised adapter cannot create, rename,
// delete or re-permission anything (docs/adapter-contract.md). Configuration
// comes from `provision adapter-config`, per that same doc's Configuration
// section.
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/tegwick/fluid-telegram/internal/adapter"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
token := os.Getenv("TELEGRAM_BOT_TOKEN")
if token == "" {
return fmt.Errorf("TELEGRAM_BOT_TOKEN is not set; injected from OpenBao at start, see docs/adapter-contract.md")
}
chatIDStr := os.Getenv("TELEGRAM_CHANNEL_ID")
if chatIDStr == "" {
return fmt.Errorf("TELEGRAM_CHANNEL_ID is not set; run `provision adapter-config`")
}
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
if err != nil {
return fmt.Errorf("TELEGRAM_CHANNEL_ID %q is not an integer: %w", chatIDStr, err)
}
attachVisual := os.Getenv("ATTACH_VISUAL") == "true"
controlURL := os.Getenv("FLUID_CONTROL_URL")
hallBaseURL := os.Getenv("HALL_BASE_URL")
addr := os.Getenv("ADAPTER_LISTEN_ADDR")
if addr == "" {
addr = ":8080"
}
cfg := adapter.Config{
ChatID: chatID,
AttachVisual: attachVisual,
HallBaseURL: hallBaseURL,
}
a := adapter.New(cfg, adapter.NewBotAPISender(token), adapter.NewMemStore(), adapter.NewHTTPTelemetry(controlURL))
log.Printf("adapter listening on %s (chat_id=%d, attach_visual=%v)", addr, chatID, attachVisual)
return http.ListenAndServe(addr, a.Handler())
}