Reintentar varias veces

This commit is contained in:
Cat /dev/Nulo 2022-09-18 09:41:32 -03:00
parent da1acfc2f0
commit f4ea75fa68

36
main.go
View file

@ -1,9 +1,11 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"strconv" "strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
@ -27,27 +29,49 @@ func readConfig() (Config, error) {
return config, nil return config, nil
} }
const DEADLINE = 6000
func main() { func main() {
config, err := readConfig() config, err := readConfig()
if err != nil { if err != nil {
panic(err) panic(err)
} }
bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
panic(err)
}
text := "No se especificó un mensaje." text := "No se especificó un mensaje."
if len(os.Args) > 1 { if len(os.Args) > 1 {
text = os.Args[1] text = os.Args[1]
} }
delay := 1
original_time := time.Now()
for true { for true {
msg := tgbotapi.NewMessage(config.ChatId, text) bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
log.Println(err)
delay = delay * 2
if delay > DEADLINE {
break
}
time.Sleep(time.Second * time.Duration(delay))
continue
}
timestamp := ""
if delay > 1 {
timestamp = fmt.Sprintf("[%s] ", original_time)
}
msg := tgbotapi.NewMessage(config.ChatId, timestamp+text)
if _, err := bot.Send(msg); err != nil { if _, err := bot.Send(msg); err != nil {
log.Println(err) log.Println(err)
delay = delay * 2
if delay > DEADLINE {
break
}
time.Sleep(time.Second * time.Duration(delay))
continue
} else { } else {
break break
} }