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
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
}

View file

@ -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."
```