diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..d1c799b --- /dev/null +++ b/.drone.yml @@ -0,0 +1,22 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: publish + image: plugins/docker + settings: + auto_tag: true + registry: + from_secret: docker_registry + repo: + from_secret: docker_repo + username: + from_secret: docker_username + password: + from_secret: docker_password + when: + event: + - push + - tag + branch: master diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1cd1b90 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM golang:alpine as golang +WORKDIR /go/src/app +COPY . . + +# Static build required so that we can safely copy the binary over. +RUN CGO_ENABLED=0 go build -o /go/bin/app -ldflags '-extldflags "-static"' + +FROM alpine:latest as alpine +RUN apk --no-cache add tzdata zip ca-certificates +WORKDIR /usr/share/zoneinfo +# -0 means no compression. Needed because go's +# tz loader doesn't handle compressed data. +RUN zip -r -0 /zoneinfo.zip . + +FROM scratch +# the test program: +COPY --from=golang /go/bin/app /app +# the timezone data: +ENV ZONEINFO /zoneinfo.zip +COPY --from=alpine /zoneinfo.zip / +# the tls certificates: +COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +ENTRYPOINT ["/app"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..84f6cf9 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# stappabot + +## Getting started + +Install stappabot + +```sh +go get git.fromouter.space/Hamcha/stappabot +``` + +Set these env vars: + +```sh +STAPPA_TOKEN=telegram-bot-token +STAPPA_BIND=:5749 +STAPPA_WEBHOOK=https://telegram.webhook/endpoint +STAPPA_PATH=endpoint +STAPPA_MAXREQUEST=5 +``` + +Run: + +```sh +stappabot +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4a019ae --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.fromouter.space/Hamcha/stappabot + +go 1.13 + +require git.fromouter.space/hamcha/tg v0.0.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8ec06cc --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +git.fromouter.space/hamcha/tg v0.0.4 h1:rZyE7om501CTIaoHJ5Z9DutSVgMcWdQUqIc+LpFqrkc= +git.fromouter.space/hamcha/tg v0.0.4/go.mod h1:63tLaopVQkIt7kotqIoYTGmV2Df+Mo4AOXSpk62qRLM= diff --git a/main.go b/main.go index 3a99bf9..d954f17 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,6 @@ package main import ( - "encoding/json" - "flag" "fmt" "os" "strconv" @@ -11,14 +9,6 @@ import ( "git.fromouter.space/hamcha/tg" ) -type Config struct { - Token string - Bind string - WebhookURL string - WebhookPath string - MaxRequestsPerMessage int -} - func checkErr(err error, msg string, args ...interface{}) { if err != nil { fmt.Printf("FATAL ERROR\n"+msg+":\n ", args...) @@ -28,28 +18,19 @@ func checkErr(err error, msg string, args ...interface{}) { } var api *tg.Telegram -var cfg Config +var MaxRequestsPerMessage int func main() { - cfgpath := flag.String("config", "stappa.conf", "Path to config file") - flag.Parse() - - cfgfile, err := os.Open(*cfgpath) - checkErr(err, "Could not open config file") - - err = json.NewDecoder(cfgfile).Decode(&cfg) - checkErr(err, "Could not decode JSON from config file contents") - - cfgfile.Close() + MaxRequestsPerMessage, _ = strconv.Atoi(os.Getenv("STAPPA_MAXREQ")) // Set default maxreq - if cfg.MaxRequestsPerMessage < 1 { - cfg.MaxRequestsPerMessage = 5 + if MaxRequestsPerMessage < 1 { + MaxRequestsPerMessage = 5 } - api = tg.MakeAPIClient(cfg.Token) - api.SetWebhook(cfg.WebhookURL) - api.HandleWebhook(cfg.Bind, cfg.WebhookPath, webhook) + api = tg.MakeAPIClient(os.Getenv("STAPPA_TOKEN")) + api.SetWebhook(os.Getenv("STAPPA_WEBHOOK")) + api.HandleWebhook(os.Getenv("STAPPA_BIND"), os.Getenv("STAPPA_PATH"), webhook) } type CardFaceFlipPics struct { @@ -109,10 +90,10 @@ func webhook(update tg.APIUpdate) { // Check for card requests if update.Message != nil && update.Message.Text != nil { requests := getCardRequests(*update.Message.Text) - if len(requests) > cfg.MaxRequestsPerMessage { + if len(requests) > MaxRequestsPerMessage { api.SendTextMessage(tg.ClientTextMessageData{ ChatID: update.Message.Chat.ChatID, - Text: fmt.Sprintf("You asked for way too many cards (%d!), please only ask me for at most %d cards in a single message.", len(requests), cfg.MaxRequestsPerMessage), + Text: fmt.Sprintf("You asked for way too many cards (%d!), please only ask me for at most %d cards in a single message.", len(requests), MaxRequestsPerMessage), ReplyID: &update.Message.MessageID, }) return