Use env, add dockerfile/CI
This commit is contained in:
parent
7ea298ae9e
commit
738695586e
6 changed files with 87 additions and 28 deletions
22
.drone.yml
Normal file
22
.drone.yml
Normal 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
24
Dockerfile
Normal 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
25
README.md
Normal 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
5
go.mod
Normal 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
2
go.sum
Normal 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
37
main.go
|
@ -1,8 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -11,14 +9,6 @@ import (
|
||||||
"git.fromouter.space/hamcha/tg"
|
"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{}) {
|
func checkErr(err error, msg string, args ...interface{}) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("FATAL ERROR\n"+msg+":\n ", args...)
|
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 api *tg.Telegram
|
||||||
var cfg Config
|
var MaxRequestsPerMessage int
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfgpath := flag.String("config", "stappa.conf", "Path to config file")
|
MaxRequestsPerMessage, _ = strconv.Atoi(os.Getenv("STAPPA_MAXREQ"))
|
||||||
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()
|
|
||||||
|
|
||||||
// Set default maxreq
|
// Set default maxreq
|
||||||
if cfg.MaxRequestsPerMessage < 1 {
|
if MaxRequestsPerMessage < 1 {
|
||||||
cfg.MaxRequestsPerMessage = 5
|
MaxRequestsPerMessage = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
api = tg.MakeAPIClient(cfg.Token)
|
api = tg.MakeAPIClient(os.Getenv("STAPPA_TOKEN"))
|
||||||
api.SetWebhook(cfg.WebhookURL)
|
api.SetWebhook(os.Getenv("STAPPA_WEBHOOK"))
|
||||||
api.HandleWebhook(cfg.Bind, cfg.WebhookPath, webhook)
|
api.HandleWebhook(os.Getenv("STAPPA_BIND"), os.Getenv("STAPPA_PATH"), webhook)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CardFaceFlipPics struct {
|
type CardFaceFlipPics struct {
|
||||||
|
@ -109,10 +90,10 @@ func webhook(update tg.APIUpdate) {
|
||||||
// Check for card requests
|
// Check for card requests
|
||||||
if update.Message != nil && update.Message.Text != nil {
|
if update.Message != nil && update.Message.Text != nil {
|
||||||
requests := getCardRequests(*update.Message.Text)
|
requests := getCardRequests(*update.Message.Text)
|
||||||
if len(requests) > cfg.MaxRequestsPerMessage {
|
if len(requests) > MaxRequestsPerMessage {
|
||||||
api.SendTextMessage(tg.ClientTextMessageData{
|
api.SendTextMessage(tg.ClientTextMessageData{
|
||||||
ChatID: update.Message.Chat.ChatID,
|
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,
|
ReplyID: &update.Message.MessageID,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue