From 7e68c8d665147a95db1aa679a6526f3b22ac7fe6 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 24 Mar 2022 12:47:37 +0100 Subject: [PATCH] First!!!1 --- Dockerfile | 24 ++++++++++++++++ go.mod | 11 +++++++ go.sum | 17 +++++++++++ main.go | 64 +++++++++++++++++++++++++++++++++++++++++ modules/metafora/mod.go | 63 ++++++++++++++++++++++++++++++++++++++++ modules/module.go | 8 ++++++ utils/command.go | 30 +++++++++++++++++++ 7 files changed, 217 insertions(+) create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 modules/metafora/mod.go create mode 100644 modules/module.go create mode 100644 utils/command.go 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/go.mod b/go.mod new file mode 100644 index 0000000..3d664f0 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.fromouter.space/crunchy-rocks/clessy-ng + +go 1.18 + +require git.fromouter.space/hamcha/tg v0.1.0 + +require ( + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..caf6e52 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +git.fromouter.space/hamcha/tg v0.1.0 h1:cJwL8pElkBtaDn7Bxa14zvlnBTTK8LdcCtcbBg7hEvk= +git.fromouter.space/hamcha/tg v0.1.0/go.mod h1:aIFj7n5FP+Zr/Zv6I6Kq4ZqhRxC12gXFQcC3iOakv9M= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..cd587a9 --- /dev/null +++ b/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "flag" + "log" + "os" + "strings" + + "git.fromouter.space/crunchy-rocks/clessy-ng/modules" + "git.fromouter.space/crunchy-rocks/clessy-ng/modules/metafora" + + "git.fromouter.space/hamcha/tg" +) + +var mods = map[string]modules.Module{ + "metafora": &metafora.Module{}, +} + +func checkErr(err error, message string, args ...interface{}) { + if err != nil { + args = append(args, err) + log.Fatalf("FATAL: "+message+"\n\t%s", args) + } +} + +func main() { + disable := flag.String("disable", "", "Blacklist mods (separated by comma)") + enable := flag.String("enable", "", "Whitelist mods (separated by comma)") + flag.Parse() + + bind := os.Getenv("CLESSY_BIND") + if bind == "" { + bind = ":8080" + } + api := tg.MakeAPIClient(os.Getenv("CLESSY_TOKEN")) + name, err := api.GetMe() + checkErr(err, "could not retrieve bot info") + + toActivate := make(map[string]modules.Module) + if *disable != "" { + for _, modname := range strings.Split(*disable, ",") { + modname = strings.TrimSpace(modname) + delete(mods, modname) + } + toActivate = mods + } else if *enable != "" { + for _, modname := range strings.Split(*enable, ",") { + toActivate[modname] = mods[modname] + } + } + + for modname, mod := range toActivate { + log.Printf("Initializing %s", modname) + err := mod.Initialize(api, name.Username) + checkErr(err, "Starting module %s failed with error", modname) + } + + api.SetWebhook(os.Getenv("CLESSY_WEBHOOK")) + log.Fatal(api.HandleWebhook(bind, os.Getenv("CLESSY_PATH"), webhook)) +} + +func webhook(update tg.APIUpdate) { + +} diff --git a/modules/metafora/mod.go b/modules/metafora/mod.go new file mode 100644 index 0000000..e635ce5 --- /dev/null +++ b/modules/metafora/mod.go @@ -0,0 +1,63 @@ +package metafora + +import ( + "math/rand" + + "git.fromouter.space/crunchy-rocks/clessy-ng/utils" + "git.fromouter.space/hamcha/tg" +) + +var metaactions = []string{ + "Puppami", "Degustami", "Lucidami", "Manipolami", "Disidratami", "Irritami", "Martorizzami", + "Lustrami", "Osannami", "Sorseggiami", "Assaporami", "Apostrofami", "Spremimi", "Dimenami", + "Agitami", "Stimolami", "Suonami", "Strimpellami", "Stuzzicami", "Spintonami", "Sguinzagliami", + "Modellami", "Sgrullami", "Cavalcami", "Perquotimi", "Misurami", "Sventolami", "Induriscimi", + "Accordami", "Debuggami", "Accarezzami", "Revisionami", "Imbottigliami", "Badami", "Scuotimi", + "Terremotami", "Incentivami", "Sollecitami", "Allenami", "Censiscimi", "Decollami", "Smagnetizzami", + "Nobilitami", "Elevami", "Accrescimi", "Impostami", "Ereggimi", "Fischiettami", "Scaldami", "Gonfiami", + "Lubrificami", +} + +var metaobjects = []string{ + "il birillo", "il bastone", "l'ombrello", "il malloppo", "il manico", "il manganello", + "il ferro", "la mazza", "l'archibugio", "il timone", "l'arpione", "il flauto", "la reliquia", + "il fioretto", "lo scettro", "il campanile", "la proboscide", "il pino", "il maritozzo", "il perno", + "il tubo da 100", "la verga", "l'idrante", "il pendolo", "la torre di Pisa", "la lancia", + "il cilindro", "il lampione", "il joystick", "il Wiimote", "il PSMove", "l'albero maestro", + "il trenino", "la sciabola", "il weedle", "il serpente", "il missile", "la limousine", + "il selfie-stick", "il candelotto", "la falce", "la biscia", "la banana", "la pannocchia", + "il papavero", "la carota", "la fava", "la salsiccia", "il cono", "l'hard drive", "la manopola", + "la manovella", "il pennello", "l'asta", "il cacciavite", "lo spazzolino", +} + +type Module struct { + client *tg.Telegram + name string +} + +func (m *Module) Initialize(api *tg.Telegram, name string) error { + m.client = api + m.name = name + return nil +} + +func (m *Module) OnUpdate(update tg.APIUpdate) { + // Not a message? Ignore + if update.Message == nil { + return + } + + if utils.IsCommand(*update.Message, m.name, "metafora") { + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: update.Message.Chat.ChatID, + Text: metaforaAPI(), + }) + return + } +} + +func metaforaAPI() string { + n := rand.Intn(len(metaactions)) + m := rand.Intn(len(metaobjects)) + return metaactions[n] + " " + metaobjects[m] +} diff --git a/modules/module.go b/modules/module.go new file mode 100644 index 0000000..f253f4d --- /dev/null +++ b/modules/module.go @@ -0,0 +1,8 @@ +package modules + +import "git.fromouter.space/hamcha/tg" + +type Module interface { + Initialize(*tg.Telegram, string) error + OnUpdate(tg.APIUpdate) +} diff --git a/utils/command.go b/utils/command.go new file mode 100644 index 0000000..866fb1b --- /dev/null +++ b/utils/command.go @@ -0,0 +1,30 @@ +package utils + +import ( + "strings" + + "git.fromouter.space/hamcha/tg" +) + +func IsCommand(update tg.APIMessage, botname string, cmdname string) bool { + if update.Text == nil { + return false + } + + text := strings.TrimSpace(*(update.Text)) + + shortcmd := "/" + cmdname + fullcmd := shortcmd + "@" + botname + + // Check short form + if text == shortcmd || strings.HasPrefix(text, shortcmd+" ") { + return true + } + + // Check long form + if text == shortcmd || strings.HasPrefix(text, fullcmd+" ") { + return true + } + + return false +}