forked from makah/pushover-vba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pushover.bas
76 lines (61 loc) · 3.11 KB
/
Pushover.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Module pushover
'Attribute VB_Name = "Pushover"
'PushOver request. see: https://pushover.net/api
Const PUSHOVER_URL As String = "https://api.pushover.net/1/messages.json"
' Sends a post via PushOver with optional options
' @param In app as String: The application's token
' @param In user/group as String: The user/group token
' @param In message as String: The message that you want to send
' @return as String(): True if the message was sent successfully, otherwise false
Public Function Post(ByVal app As String, ByVal group As String, ByVal message As String) As Boolean
Dim response As String
response = PostOp(app, group, message)
If InStr(response, """status"":1") > 0 Then
Post = True
Else
Debug.WriteLine(response)
End If
End Function
' Sends a post via PushOver with optional options
' @param In app as String: The application's token
' @param In user/group as String: The user/group token
' @param In message as String: The message that you want to send
' @param In options as String Array: Other options that you can send to Pushover message like 'title' and 'sound'.
' @return as String(): The post response
' @remark see more about parameters on: https://pushover.net/api
Public Function PostOp(ByVal app As String, ByVal group As String, ByVal message As String, ParamArray options() As Object) As String
Dim xhttp As Object, params As String, i As Integer
If (UBound(options) + 1) Mod 2 = 1 Then
Debug.Print("option needs to have [key, value]")
PostOp = vbNull
Exit Function
End If
params = StringFormat("token={0}&user={1}&message={2}", app, group, message)
Debug.WriteLine(params)
For i = 0 To UBound(options) Step 2
params = params & StringFormat("&{0}={1}", options(i), options(i + 1))
Next i
xhttp = CreateObject("MSXML2.ServerXMLHTTP")
With xhttp
.Open("POST", PUSHOVER_URL, False)
.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
.send(params)
PostOp = .responseText
End With
End Function
' ----- copied from ExcelUtils.
' ----- from: https://github.com/makah/ExcelHelper
'
' Generates a string using .NET format, i.e. {0}, {1}, {2} ...
' @param In strValue as String: A composite format string that includes one or more format items
' @param In arrParames as Variant: Zero or more objects to format.
' @return as String: A copy of format in which the format items have been replaced by the string representations of the corresponding arguments.
' @example: Debug.Print StringFormat("My name is {0} {1}. Hey!", "Mauricio", "Arieira")
Private Function StringFormat(ByVal strValue As String, ParamArray arrParames() As Object) As String
Dim i As Integer
For i = LBound(arrParames) To UBound(arrParames)
strValue = Replace(strValue, "{" & CStr(i) & "}", CStr(arrParames(i)))
Next
StringFormat = strValue
End Function
End Module