diff --git a/lib/telegram.go b/lib/telegram.go index 1af9839..c5a3079 100644 --- a/lib/telegram.go +++ b/lib/telegram.go @@ -3,21 +3,50 @@ package lib import ( "net/http" "time" + "github.com/andlabs/ui" + "strconv" ) var t string +var sent bool +var pbVal int // --------------------------------------------------------------------------------------------------------------------- // Send a telegram message using a query URL -func Send_TelegramMessage(config Config) { +func Send_TelegramMessage(config Config, label_Update *ui.Label, pb *ui.ProgressBar) { + // Timestamp if config.TimeStamp { t = "[" + string(time.Now().Format("2006/01/02 15:04:05")) + "] %0A" } - // Learn how to setup a telegram bot: https://core.telegram.org/bots - resp, _ := http.Get("https://api.telegram.org/bot" + config.Botid + - ":" + config.Token + - "/sendMessage?chat_id=" + config.Chatid + - "&text=" + t + config.Message) - defer resp.Body.Close() + + for { + // Update status message + label_Update.SetText(" Sending Telegram message...") + time.Sleep(2 * time.Second) + + // Learn how to setup a telegram bot: https://core.telegram.org/bots + resp, err := http.Get("https://api.telegram.org/bot" + config.Botid + + ":" + config.Token + + "/sendMessage?chat_id=" + config.Chatid + + "&text=" + t + config.Message) + + // http.Get will return an error if there is no internet connection + if err != nil { + for i := 0; i <= 15; i++ { + pbVal = int(100/float32(15) * float32(i)) + if pbVal > 100 { + pbVal = 100 + } + pb.SetValue(pbVal) + label_Update.SetText(" No internet connection. Retry in... " + strconv.Itoa(15 - i) + " min") + time.Sleep(1 * time.Minute) + } + // if we do have a connection, break the loop and exit this function + } else { + defer resp.Body.Close() + break + } + } + } diff --git a/lib/watchdog.go b/lib/watchdog.go index fecba09..aae5d5c 100644 --- a/lib/watchdog.go +++ b/lib/watchdog.go @@ -49,7 +49,7 @@ func Watchdog( // Only procede with exit routine if we reached the fail threshold if PENALTY >= config.FailLimit { // Use the Telegram API to send a message - Send_TelegramMessage(config) + Send_TelegramMessage(config, label_Update, pb) // Optional: shutdown the computer if the monitored process is disconnected if config.ShutdownOnDC { diff --git a/main.go b/main.go index 9f2f61a..b847d8c 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( ) // Variables -const VERSION = "0.1.5" +const VERSION = "0.1.8" var errmsg string func main() { @@ -38,13 +38,14 @@ func main() { box_about := ui.NewVerticalBox() tab := ui.NewTab() + pb := ui.NewProgressBar() + tbtn := ui.NewButton("Send Telegram test message") tbtn.OnClicked(func(*ui.Button) { - Send_TelegramMessage(config) + Send_TelegramMessage(config, label_Update, pb) }) sep := ui.NewHorizontalSeparator() - pb := ui.NewProgressBar() // Append all UI elements to the box container box_main.Append(label_Process, false)