Skip to content

Commit

Permalink
Resend telegram msg every 15 min (#10)
Browse files Browse the repository at this point in the history
If the whole internet connection is offline the watchdog will now try to resend the telegram message every 15 min, until we get ANY response (200, 404, ...) from the server.
  • Loading branch information
tzerk committed Mar 19, 2017
1 parent 4d12c9f commit a2a4bd0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
43 changes: 36 additions & 7 deletions lib/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

}
2 changes: 1 addition & 1 deletion lib/watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Variables
const VERSION = "0.1.5"
const VERSION = "0.1.8"
var errmsg string

func main() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a2a4bd0

Please sign in to comment.