Usar variables de env en vez de archivo json

This commit is contained in:
Cat /dev/Nulo 2022-09-10 08:57:42 -03:00
parent 6f7247952a
commit da1acfc2f0
2 changed files with 7 additions and 28 deletions

22
main.go
View file

@ -1,11 +1,9 @@
package main package main
import ( import (
"encoding/json"
"io"
"log" "log"
"os" "os"
"path" "strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
@ -18,24 +16,14 @@ type Config struct {
func readConfig() (Config, error) { func readConfig() (Config, error) {
config := Config{} config := Config{}
homeDir, err := os.UserHomeDir() config.Token = os.Getenv("TELEGRAM_BOT_TOKEN")
if err != nil {
return config, err
}
pathName := path.Join(homeDir, ".config/telegram-bot-send-message.json")
file, err := os.Open(pathName) var err error
if err != nil { config.ChatId, err = strconv.ParseInt(os.Getenv("TELEGRAM_CHAT_ID"), 10, 64)
return config, err
}
configBytes, err := io.ReadAll(file)
if err != nil {
return config, err
}
err = json.Unmarshal(configBytes, &config)
if err != nil { if err != nil {
return config, err return config, err
} }
return config, nil return config, nil
} }

View file

@ -1,18 +1,9 @@
# telegram-bot-send-message # telegram-bot-send-message
Get token from [@BotFather](https://t.me/BotFather). Get token from [@BotFather](https://t.me/BotFather). Get chat id with a bot like [@getmy_idbot](https://t.me/getmy_idbot).
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
}
```
Run Run
```go ```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."
``` ```