Use env, add dockerfile/CI

This commit is contained in:
Hamcha 2020-01-29 12:46:04 +01:00
parent 7ea298ae9e
commit 738695586e
Signed by: hamcha
GPG key ID: 44AD3571EB09A39E
6 changed files with 87 additions and 28 deletions

22
.drone.yml Normal file
View file

@ -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

24
Dockerfile Normal file
View file

@ -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"]

25
README.md Normal file
View file

@ -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
```

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module git.fromouter.space/Hamcha/stappabot
go 1.13
require git.fromouter.space/hamcha/tg v0.0.4

2
go.sum Normal file
View file

@ -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=

37
main.go
View file

@ -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