This commit is contained in:
Cat /dev/Nulo 2022-09-09 21:45:41 -03:00
commit cda89d6e97
3 changed files with 74 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module nulo.in/telegram-bot-send-message
go 1.19
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
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=

67
main.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"encoding/json"
"io"
"log"
"os"
"path"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Config struct {
Token string `json:"token"`
ChatId int64 `json:"chat_id"`
}
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")
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)
if err != nil {
return config, err
}
return config, nil
}
func main() {
config, err := readConfig()
if err != nil {
panic(err)
}
bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
panic(err)
}
text := "No se especificó un mensaje."
if len(os.Args) > 1 {
text = os.Args[1]
}
for true {
msg := tgbotapi.NewMessage(config.ChatId, text)
if _, err := bot.Send(msg); err != nil {
log.Println(err)
} else {
break
}
}
}