send message by Telegram bot when an application is closed, monitor by telnet port

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
asusgtx
Posts: 32
Joined: 24 Feb 2020, 23:27

send message by Telegram bot when an application is closed, monitor by telnet port

05 Aug 2021, 20:28

Hello, how can I make the scripts, monitor an application that has an open port by telnet (which is running) send a message by telegram by bot "Service ON", and if it is down (telnet port closed or application closed ), send the message by telegram by the bot "service OFF"

PS: Excuse my English, it is very bad, I am using the google translator = (

Code: Select all

run ping 11.11.11.12 -t
run telnet 11.11.11.12 3333

chatid :=  -123456789				; <--- change
botToken := "123456789"			; <--- change

text := "%E2%9D%8C Server ON"
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

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

Re: send message by Telegram bot when an application is closed, monitor by telnet port

06 Aug 2021, 21:07

In this post by mikeyww you can see how to get the result of a ping into the clipboard. Then, you could parse that result with some string functions or regex for the information you need.

Depending on the result you could just do an if/else to decide about the message you send to telegram (sending to telegram should already work with your code and your credentials - or, does it not? -> syntax alternative)
asusgtx
Posts: 32
Joined: 24 Feb 2020, 23:27

Re: send message by Telegram bot when an application is closed, monitor by telnet port

08 Aug 2021, 00:15

gregster wrote:
06 Aug 2021, 21:07
In this post by mikeyww you can see how to get the result of a ping into the clipboard. Then, you could parse that result with some string functions or regex for the information you need.

Depending on the result you could just do an if/else to decide about the message you send to telegram (sending to telegram should already work with your code and your credentials - or, does it not? -> syntax alternative)


Thanks for the help gregster, I almost have it, I just need a step.
It doesn't work for me or I don't know how to do it, by the time everything is ok and the server responds and is online, the "server online" message.
When it is down (it does not detect the open port by telenet, it works fine and sends the message from server offline to telegram)
But I can't make it send the Server On message when it detects it online (telnet port open)

Code: Select all

site = 11.11.11.12

SetTimer, one, 30000

SoundBeep, 1500
;Sleep, 1000
;Process, Close, Telnet.exe

Run, "%ComSpec%" /c "telnet %site% 3333",, Hide
ClipWait, 0

If ErrorLevel
{
SoundBeep, 1000 + 500 * found
UrlDownloadToFile, https://api.telegram.org/bot123456789I/sendMessage?chat_id=-123456789&text=Server OFF, result.log

}

if else
MsgBox, Server ON.

one:

F3::
Reload
Return
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: send message by Telegram bot when an application is closed, monitor by telnet port

10 Aug 2021, 22:44

Afaics, you are not checking anywhere what the telnet command actually responded. That's why I was saying, capture the response to the clipboard and then analyze its contents.

Since I forgot everything about telnet (haven't used it since the '90s) I am using a ping to google.com for demonstration purposes:

Code: Select all

botToken := "somethingsomething"					
chatID := 0000000001		; fill in your credentials for bot token and chat ID		
site := "google.com"			; also try non-existing "google.coma"
SetTimer , pingIt, 20000

pingIt:
	Clipboard := ""
	text := "tbd"		; reset text
	
	RunWait, "%ComSpec%" /c "ping %site% | clip",, Hide		;  "| clip" will pipe the result to the clipboard
	ClipWait, 0
	If ErrorLevel
		MsgBox, 48, Error, An error occurred while waiting for the clipboard., 5		; msgbox times out after 5 seconds

	If Instr(clipboard, "nicht finden")							; check the ping result for a certain string
		text := "Host "  site " could not be found!"
	else
		text := "Host " site " was found."
	tooltip, % clipboard		; for testing

	str := "https://api.telegram.org/bot" botToken "/sendmessage?" 
	param := "chat_id=" chatid "&text=" text
	UrlDownloadToFile, % str . param, result.log					; log file contains response from telegram; check if message was sent or error occurred
return

Esc::ExitApp
Please note that I get a ping message in german on my computer. So, if the host can't be found, the message will contain the string "nicht finden" (meaning "not found") - please change it accordingly in If Instr(clipboard, "nicht finden") to the fail message you want to detect. Of course, I could also parse the success message and extract the milliseconds or whatever... this is just an example. google.com should usually be online ;) , but you can try site := "google.coma" for a failing ping. (I have no idea what the telnet command will actually respond.)
The tooltip will show you the last ping result, for testing (you can remove it, if you want).

Of course, piping the result to the clipboard via | clip could interfere with your normal clipboard usage. I guess SKAN's RunCmd() could be an option to capture the result to some other variable instead, and to keep the clipboard available for you. Or, you could probably redirect the result to a text file and then analyze that. Another option would be RunWaitOne() from Run-example #7 , but that won't hide the cmd window, afaik. It depends on your requirements.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ConnorMcLoud and 173 guests