Script to monitor internet connection and send telegram message when internet reconnects?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MDean1201
Posts: 23
Joined: 11 Dec 2016, 10:28

Script to monitor internet connection and send telegram message when internet reconnects?

13 Jun 2021, 15:04

Hi,

Is it possible to create an autohotkey script to monitor ones internet connection, and if it's lost & then reconnected, send yourself a message via telegram (using a telegram bot) saying for example 'Internet reconnected'.

Please could someone suggest a script? As I've been trying to use my telegram bot ChatID and Bot Token to do this but haven't had any luck.

Thanks in advance
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script to monitor internet connection and send telegram message when internet reconnects?

13 Jun 2021, 15:24

These are two separate problems, imho.

I think for checking the internet connection you should find something on these forums.

Sending a message via your Telegram bot didn't work? Then please show your code.
(Of course, remove your personal and bot tokens/IDs first; I can use my own.)
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Script to monitor internet connection and send telegram message when internet reconnects?

13 Jun 2021, 15:26

In case this helps with the "network connect" part: Windows Task Scheduler can actually detect that event. You could then set it to run your script.

network.png
network.png (111.78 KiB) Viewed 1029 times
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script to monitor internet connection and send telegram message when internet reconnects?

13 Jun 2021, 15:29

Thanks, mikeyww. :thumbup: I wasn't aware.

MDean1201, it also appears we discussed the internet (re-)connection part already in 2018: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=50243
MDean1201
Posts: 23
Joined: 11 Dec 2016, 10:28

Re: Script to monitor internet connection and send telegram message when internet reconnects?

13 Jun 2021, 20:39

So I did create an check internet script in the past. However it is a bit clunky, and was wondering if there was a more efficient way to continually check the internet, until the internet reconnects:

Code: Select all

UrlDownloadToVar(URL) {
ComObjError(false)
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", URL)
WebRequest.Send()
Return WebRequest.ResponseText
}
#Persistent
SetTimer, CheckInternet, 500 
Return

CheckInternet:
html := UrlDownloadToVar("http www.google.com ")  Broken Link for safety
if html
    {}
else
	{
	MsgBox,, Title:Internet status, Text:first check = not working will check again in 2mins & send message, 1
	sleep, 25000
	html := UrlDownloadToVar("http www.google.com ")  Broken Link for safety
	if html
		{
		MsgBox,, Title:Internet status, Text:2nd  check = working, 5
						Run "C:\Users\X\Desktop\telegram send.ahk"
						Sleep, 2000
						Process, Close, testprogram.exe
						Sleep, 2000
						Run c:\WINDOWS\system32\rundll32.exe shell32.dll`,#61
						Sleep, 2000
						Send, "C:\Users\X\Documents\testprogram.exe"
						Sleep, 2000
						Send, {Return}
						Return
		}
		
	Else
		MsgBox,, Title:Internet status, Text:2nd  check = not working, 5
		sleep, 25000
			html := UrlDownloadToVar("http www.google.com ")  Broken Link for safety
			if html
				{
				MsgBox,, Title:Internet status, Text:3RD  check = working, 5
						Run "C:\Users\X\Desktop\telegram send.ahk"
						Sleep, 2000
						Process, Close, testprogram.exe
						Sleep, 2000
						Run c:\WINDOWS\system32\rundll32.exe shell32.dll`,#61
						Sleep, 2000
						Send, "C:\Users\X\Documents\testprogram.exe"
						Sleep, 2000
						Send, {Return}
						Return
				}
			else
				MsgBox,, Title:Internet status, Text:3rd  check =  not working, 5
				Sleep, 25000
					html := UrlDownloadToVar("http www.google.com ")  Broken Link for safety
					if html
									{
				MsgBox,, Title:Internet status, Text:4th  check = working, 5
						Run "C:\Users\X\Desktop\telegram send.ahk"
						Sleep, 2000
						Process, Close, testprogram.exe
						Sleep, 2000
						Run c:\WINDOWS\system32\rundll32.exe shell32.dll`,#61
						Sleep, 2000
						Send, "C:\Users\X\Documents\testprogram.exe"
						Sleep, 2000
						Send, {Return}
						Return
				}
			else
						MsgBox,, Title:Internet status, Text:5th  check =  not working, 5
				Sleep, 25000
					html := UrlDownloadToVar("http www.google.com ")  Broken Link for safety
					if html
						{
						MsgBox,, Title:Internet status, Text:5th  check =  working, 5
						Run "C:\Users\X\Desktop\telegram send.ahk"
						Sleep, 2000
						Process, Close, testprogram.exe
						Sleep, 2000
						Run c:\WINDOWS\system32\rundll32.exe shell32.dll`,#61
						Sleep, 2000
						Send, "C:\Users\X\Documents\testprogram.exe"
						Sleep, 2000
						Send, {Return}
						}
			else,
				Return
	}
And this is my telegram script which doesnt seem to work at all. So if someone could advise on how to get this to work, I would really appreciate it

Code: Select all

chatid :=

text := "test message"

param := "chat_id=" chatid "&text=" text

str =https api.telegram.org /botXXXXXXX/sendmessage? Broken Link for safety ;

;msgbox, % url_tovar(str, param) ; this is useful if you need to return data

url_tovar(str, param)

url_tovar(URL, param) {

WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")

WebRequest.Open("POST", URL)

WebRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

WebRequest.Send(param)

res := WebRequest.ResponseText

return res

}
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script to monitor internet connection and send telegram message when internet reconnects?

14 Jun 2021, 00:08

MDean1201 wrote:
13 Jun 2021, 20:39
And this is my telegram script which doesnt seem to work at all. So if someone could advise on how to get this to work, I would really appreciate it
This works for me, if I add my chat ID and bot token at the top:

Code: Select all

chatid :=  1234567890				; <--- change
botToken := "mybotToken"			; <--- change

text := "test message"
param := "chat_id=" chatid "&text=" text
str := "https://api.telegram.org/bot" botToken "/sendmessage?" 

; msgbox % url_tovar(str, param)
url_tovar(str, param)


url_tovar(URL, param) {

WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")

WebRequest.Open("POST", URL)

WebRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

WebRequest.Send(param)

res := WebRequest.ResponseText

return res

}
If it doesn't work for you, uncomment the line with the msgbox ( msgbox % url_tovar(str, param) ) and look at the response. Does it show an error message?
MDean1201
Posts: 23
Joined: 11 Dec 2016, 10:28

Re: Script to monitor internet connection and send telegram message when internet reconnects?

14 Jun 2021, 05:56

thanks for the help. So I tried the script you suggested, however I got these messages when I ran the script (and I tried uncommenting the line wih msgbox)
image.png
image.png (78.21 KiB) Viewed 949 times
Any idea how I can fix this?
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script to monitor internet connection and send telegram message when internet reconnects?

14 Jun 2021, 10:45

Probably has to do with the TLS settings on your computer - as was discussed here: https://www.autohotkey.com/boards/viewtopic.php?t=58051 which probably can be fixed via registry edits (I assume, you already re-started your computer since this error occurred for the first time).
Since I have never encountered this problem personally, I can't give you a better lead at the moment... I have some Telegram bots running now for years, currently on a recent Win10 version. Are you using something older, like Win7 ?
MDean1201
Posts: 23
Joined: 11 Dec 2016, 10:28

Re: Script to monitor internet connection and send telegram message when internet reconnects?

15 Jun 2021, 19:56

thanks @gregster

I think it is indeed something to do with the TLS settings. I looked through the thread you referenced and tried the various suggestions, including SKANs, but to no avail.

I'm running windows 7 still for various reasons.

Think I might have to give up on this one, but thank you for your help.
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Script to monitor internet connection and send telegram message when internet reconnects?

15 Jun 2021, 20:13

I see.

Not sure if it will cause the same problem for you, but this also works for me:

Code: Select all

chatid :=  111111111
botToken := "myBotToken"	

text := "test"
param := "chat_id=" chatid "&text=" text
str := "https://api.telegram.org/bot" botToken "/sendmessage?" 

UrlDownloadToFile, % str . param, tempfile.txt
(The tempfile you could save anywhere, and delete afterwards.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot] and 339 guests