Reintentar varias veces

This commit is contained in:
Cat /dev/Nulo 2022-09-18 09:41:32 -03:00
parent da1acfc2f0
commit f4ea75fa68
1 changed files with 30 additions and 6 deletions

36
main.go
View File

@ -1,9 +1,11 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
@ -27,27 +29,49 @@ func readConfig() (Config, error) {
return config, nil
}
const DEADLINE = 6000
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]
}
delay := 1
original_time := time.Now()
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 {
log.Println(err)
delay = delay * 2
if delay > DEADLINE {
break
}
time.Sleep(time.Second * time.Duration(delay))
continue
} else {
break
}