From da1acfc2f036517dcb1ff13e491bb7c81b6d6ebd Mon Sep 17 00:00:00 2001 From: Nulo Date: Sat, 10 Sep 2022 08:57:42 -0300 Subject: [PATCH] Usar variables de env en vez de archivo json --- main.go | 22 +++++----------------- readme.md | 13 ++----------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index 287e70b..b9b299d 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,9 @@ package main import ( - "encoding/json" - "io" "log" "os" - "path" + "strconv" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) @@ -18,24 +16,14 @@ type Config struct { func readConfig() (Config, error) { config := Config{} - homeDir, err := os.UserHomeDir() - if err != nil { - return config, err - } - pathName := path.Join(homeDir, ".config/telegram-bot-send-message.json") + config.Token = os.Getenv("TELEGRAM_BOT_TOKEN") - file, err := os.Open(pathName) - if err != nil { - return config, err - } - configBytes, err := io.ReadAll(file) - if err != nil { - return config, err - } - err = json.Unmarshal(configBytes, &config) + var err error + config.ChatId, err = strconv.ParseInt(os.Getenv("TELEGRAM_CHAT_ID"), 10, 64) if err != nil { return config, err } + return config, nil } diff --git a/readme.md b/readme.md index 96a258a..22d14fd 100644 --- a/readme.md +++ b/readme.md @@ -1,18 +1,9 @@ # telegram-bot-send-message -Get token from [@BotFather](https://t.me/BotFather). - -Create config at `$HOME/telegram-bot-send-message.json` - -```json -{ - "token": "replace with token", - "chat_id": replace with chat id from a bot like https://t.me/getmy_idbot -} -``` +Get token from [@BotFather](https://t.me/BotFather). Get chat id with a bot like [@getmy_idbot](https://t.me/getmy_idbot). Run ```go -telegram-bot-send-message "I hate computers." +TELEGRAM_BOT_TOKEN=replace_with_token TELEGRAM_CHAT_ID=replace_with_chat_id telegram-bot-send-message "I hate computers." ```