From 629370c1c57c73024675b25129d9331aa2c3eac7 Mon Sep 17 00:00:00 2001 From: Daniele Tricoli Date: Sun, 27 Feb 2022 02:48:51 +0100 Subject: [PATCH] Create a bot that read messages --- .drone.yml | 12 ++++++++++++ go.mod | 14 ++++++++++++++ go.sum | 13 +++++++++++++ main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 18 ++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 .drone.yml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 main_test.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..20a9163 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,12 @@ +--- +kind: pipeline +type: docker +name: default + +steps: + - name: test + image: golang:1.17 + commands: + - go build + - go vet + - go test -v diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8b90de7 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module noa.mornie.org/eriol/tg2rss + +go 1.17 + +require ( + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/stretchr/testify v1.7.0 +) + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..02c9430 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b32aed5 --- /dev/null +++ b/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "log" + "os" + "strconv" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +const ( + DEBUG = "DEBUG" + TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" +) + +func main() { + + bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN)) + if err != nil { + log.Panic(err) + } + + bot.Debug = parseBoolOrFalse(os.Getenv(DEBUG)) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 30 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message != nil { + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + } + } +} + +func parseBoolOrFalse(s string) bool { + r, err := strconv.ParseBool(s) + if err != nil { + return false + } + + return r +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2b1abf9 --- /dev/null +++ b/main_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseBoolOrFalse(t *testing.T) { + + assert.Equal(t, parseBoolOrFalse("True"), true) + assert.Equal(t, parseBoolOrFalse("TRUE"), true) + assert.Equal(t, parseBoolOrFalse("true"), true) + assert.Equal(t, parseBoolOrFalse(""), false) + assert.Equal(t, parseBoolOrFalse("False"), false) + assert.Equal(t, parseBoolOrFalse("FALSE"), false) + assert.Equal(t, parseBoolOrFalse("false"), false) +}