diff options
| author | Christine Dodrill <me@christine.website> | 2017-05-06 10:29:23 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2017-05-06 10:29:23 -0700 |
| commit | 49b5bfd6e071cccbdbbca5d331198134ba3b5d59 (patch) | |
| tree | 6936c73cfe1ed51fbef216e954fe858e2aabe1d8 | |
| parent | 4d65901933bb7bed40783d9b9f6ae2ea2b829889 (diff) | |
| download | x-49b5bfd6e071cccbdbbca5d331198134ba3b5d59.tar.xz x-49b5bfd6e071cccbdbbca5d331198134ba3b5d59.zip | |
meirl bot version 1
| -rw-r--r-- | tg/meirl/.gitignore | 1 | ||||
| -rw-r--r-- | tg/meirl/main.go | 53 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tg/meirl/.gitignore b/tg/meirl/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/tg/meirl/.gitignore @@ -0,0 +1 @@ +.env diff --git a/tg/meirl/main.go b/tg/meirl/main.go new file mode 100644 index 0000000..d070b66 --- /dev/null +++ b/tg/meirl/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "log" + + "github.com/Xe/ln" + "github.com/caarlos0/env" + _ "github.com/joho/godotenv/autoload" + "gopkg.in/telegram-bot-api.v4" +) + +type config struct { + RedditUsername string `env:"REDDIT_USERNAME,required"` + RedditPassword string `env:"REDDIT_PASSWORD.required"` + + TelegramToken string `env:"TELEGRAM_TOKEN,required"` + TelegramAdmin string `env:"TELEGRAM_ADMIN,required"` +} + +func main() { + var cfg config + err := env.Parse(&cfg) + if err != nil { + ln.Fatal(ln.F{"err": err, "action": "env.Parse"}) + } + + bot, err := tgbotapi.NewBotAPI(cfg.TelegramToken) + if err != nil { + ln.Fatal(ln.F{"err": err, "action": "tgbotapi.NewBotAPI"}) + } + + bot.Debug = true + + ln.Log(ln.F{"action": "telegram_active", "username": bot.Self.UserName}) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates, err := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message == nil { + continue + } + + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) + msg.ReplyToMessageID = update.Message.MessageID + + bot.Send(msg) + } +} |
