Pushover Notifications

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
palinka
Senior user
Senior user
Posts: 4230
Joined: 2017-09-12 17:57

Pushover Notifications

Post by palinka » 2023-09-18 22:19

I've been using SMS for a long time to send notifications about server events, hmail events, etc. Short messages about things I want to know about right away. First I was using an actual modem and sim, then I went virtual with Twilio. But recently the major phone carriers in the US started cracking down on SMS spam and made it difficult for these automated messages to get through, and therefore, I have not received a majority of them lately. So I decided to look for other options and ran across Pushover.net, which is an api driven push message service. You sign up and I think the cost is a one time fee of $5 unless you're a big organization. That is a much better deal than SMS.

Anyway, you sign up and get an api token for the devices you want to send FROM and a user key which is tied to your account. I set to scripting a function to send messages the same way I was sending with SMS and here is what I came up with. They have more options like groups and custom chimes and whatever, but for this purpose - administrative automated notifications - I don't need any of that stuff.

There are only 2 variables: pMsg (message - required) and pUrl (URL - optional). The url is handy for me because I log a lot of stuff to database and have various php scripts to display the contents. I use YoURLs to provide short links suitable for SMS. The url is separate. You could include it in the message, which is what I was doing before, but since they offered it separately....

Anyway, here's the script. It's working. I will add some event logging later.

Code: Select all

Option Explicit

Private Const PushoverApiKey = "lkjasdflkjasdflkjsdflkjljksdfl"
Private Const PushoverUserKey = "lkasjdflkjasdflkjsdflkjsdflkjs"

Function SendPushover(pMsg, pUrl)
	Dim Request, Url, postData
	Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
	Url = "https://api.pushover.net/1/messages.json"
	Request.Open "POST", Url, False
	Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
	postData = "token=" & PushoverApiKey & "&user=" & PushoverUserKey & "&message=" & pMSG & "&url=" & pUrl
	Request.send postData
End Function

Dim pMsg, pUrl
pMsg = "This is the message to send"
pUrl = "https://short.link.tld/Random"    ' can be left blank or left out altogether
Call SendPushover(pMsg, pUrl)

palinka
Senior user
Senior user
Posts: 4230
Joined: 2017-09-12 17:57

Re: Pushover Notifications

Post by palinka » 2023-09-19 09:46

Here's a little event logging to go with it. Not much in the way of json response. Pretty simple stuff.

Code: Select all

Option Explicit

Private Const PushoverApiKey = "lkjasdflkjasdflkjsdflkjljksdfl"
Private Const PushoverUserKey = "lkasjdflkjasdflkjsdflkjsdflkjs"

Function jsonDecode(jsonString)
    Dim oSCtrl
    Set oSCtrl = CreateObject("ScriptControl")  ' https://tablacus.github.io/scriptcontrol_en.html
    oSCtrl.Language = "JScript"
    Set jsonDecode = oSCtrl.Eval("(" & jsonString & ")")
End Function

Function SendPushover(pMsg, pUrl)
	Dim Request, Url, postData, oResponse
	Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
	Url = "https://api.pushover.net/1/messages.json"
	Request.Open "POST", Url, False
	Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
	postData = "token=" & PushoverApiKey & "&user=" & PushoverUserKey & "&message=" & pMSG & "&url=" & pUrl
	Request.send postData
	Set oResponse = jsonDecode(Request.responseText)
	
	If (Request.status <> 200) Then
		EventLog.Write( "================== Notifier ==================" )
		EventLog.Write( ":Status           : " & Request.status )
		EventLog.Write( ":Status Code      : " & oResponse.status )
		EventLog.Write( ":Errors           : " & oResponse.errors )
		EventLog.Write( ":Message          : " & pMsg )
		EventLog.Write( ":URL              : " & pUrl )
		EventLog.Write( ":Request ID       : " & oResponse.request )
		EventLog.Write( "==============================================" )
	Else
		EventLog.Write( "================== Notifier ==================" )
		EventLog.Write( ":Status           : " & Request.status )
		EventLog.Write( ":Message          : " & pMsg )
		EventLog.Write( ":URL              : " & pUrl )
		EventLog.Write( ":Request ID       : " & oResponse.request )
		EventLog.Write( "==============================================" )
	End If

End Function

Call SendPushover("Here's a function in VBS with a link", "http://shortlink.tld/X-T")

Post Reply