forked from thexperiments/pimatic-pushover
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pushover.coffee
187 lines (148 loc) · 6.41 KB
/
pushover.coffee
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# #Pushover Plugin
# This is an plugin to send push notifications via pushover
# ##The plugin code
# Your plugin must export a single function, that takes one argument and returns a instance of
# your plugin class. The parameter is an environment object containing all pimatic related functions
# and classes. See the [startup.coffee](http://sweetpi.de/pimatic/docs/startup.html) for details.
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
util = env.require 'util'
M = env.matcher
# Require the [pushover-notifications](https://github.com/qbit/node-pushover) library
Pushover = require 'pushover-notifications'
Promise.promisifyAll(Pushover.prototype)
pushoverService = null
# ###Pushover class
class PushoverPlugin extends env.plugins.Plugin
# ####init()
init: (app, @framework, config) =>
user = config.user
token = config.token
env.logger.debug "pushover: user= #{user}"
env.logger.debug "pushover: token = #{token}"
pushoverService = new Pushover( {
user: user,
token: token,
onerror: (message) => env.logger.error("pushover error: #{message}")
})
@framework.ruleManager.addActionProvider(new PushoverActionProvider @framework, config)
# Create a instance of my plugin
plugin = new PushoverPlugin()
class PushoverActionProvider extends env.actions.ActionProvider
constructor: (@framework, @config) ->
return
parseAction: (input, context) =>
defaultTitle = @config.title
defaultMessage = @config.message
defaultPriority = @config.priority
defaultSound = @config.sound
defaultDevice = @config.device
defaultRetry = @config.retry
defaultExpire = @config.expire
defaultCallbackurl = @config.callbackurl
defaultHtml = if @config.html then 1 else 0
# Helper to convert 'some text' to [ '"some text"' ]
strToTokens = (str) => ["\"#{str}\""]
titleTokens = strToTokens defaultTitle
messageTokens = strToTokens defaultMessage
priority = defaultPriority
soundTokens = strToTokens defaultSound
urlTokens = undefined
deviceTokens = strToTokens defaultDevice
retry = defaultRetry
expire = defaultExpire
callbackurlTokens = strToTokens defaultCallbackurl
html = defaultHtml
setTitle = (m, tokens) => titleTokens = tokens
setMessage = (m, tokens) => messageTokens = tokens
setPriority = (m, p) => priority = p
setDevice = (m, tokens) => deviceTokens = tokens
setSound = (m, tokens) => soundTokens = tokens
setUrl = (m, tokens) => urlTokens = tokens
setRetry = (m, d) => retry = d
setExpire = (m, d) => expire = d
setCallbackurl = (m, tokens) => callbackurlTokens = tokens
setHtml = (m, d) => html = d
m = M(input, context)
.match('send ', optional: yes)
.match(['push','pushover','notification'])
next = m.match(' title:').matchStringWithVars(setTitle)
if next.hadMatch() then m = next
next = m.match(' message:').matchStringWithVars(setMessage)
if next.hadMatch() then m = next
next = m.match(' priority:').matchNumber(setPriority)
if next.hadMatch() then m = next
next = m.match(' device:').matchStringWithVars(setDevice)
if next.hadMatch() then m = next
next = m.match(' sound:').matchStringWithVars(setSound)
if next.hadMatch() then m = next
next = m.match(' url:').matchStringWithVars(setUrl)
if next.hadMatch() then m = next
next = m.match(' retry:').matchNumber(setRetry)
if next.hadMatch() then m = next
next = m.match(' expire:').matchNumber(setExpire)
if next.hadMatch() then m = next
next = m.match(' callbackurl:').matchStringWithVars(setCallbackurl)
if next.hadMatch() then m = next
next = m.match(' html:').matchNumber(setHtml)
if next.hadMatch() then m = next
if m.hadMatch()
match = m.getFullMatch()
assert Array.isArray(titleTokens)
assert Array.isArray(messageTokens)
assert(not isNaN(priority))
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new PushoverActionHandler(
@framework, titleTokens, messageTokens, priority, soundTokens, urlTokens, deviceTokens, retry, expire, callbackurlTokens, html
)
}
class PushoverActionHandler extends env.actions.ActionHandler
constructor: (@framework, @titleTokens, @messageTokens, @priority, @soundTokens, @urlTokens, @deviceTokens, @retry, @expire, @callbackurlTokens, @html) ->
executeAction: (simulate, context) ->
Promise.all( [
@framework.variableManager.evaluateStringExpression(@titleTokens)
@framework.variableManager.evaluateStringExpression(@messageTokens)
@framework.variableManager.evaluateStringExpression(@soundTokens)
if @urlTokens? then @framework.variableManager.evaluateStringExpression(@urlTokens) else Promise.resolve
@framework.variableManager.evaluateStringExpression(@deviceTokens)
@framework.variableManager.evaluateStringExpression(@callbackurlTokens)
]).then( ([title, message, sound, url, device, callbackurl]) =>
if simulate
# just return a promise fulfilled with a description about what we would do.
return __("would push message \"%s\" with title \"%s\"", message, title)
else
if @priority is "2"
env.logger.debug "pushover debug: priority=2"
msg = {
message: message
title: title
device: device
sound: sound
url: url
priority: @priority
retry: @retry
expire: @expire
callbackurl: callbackurl
html: @html
}
else
env.logger.debug "pushover debug: priority=xxx"
msg = {
message: message
title: title
device: device
sound: sound
url: url
priority: @priority
html: @html
}
return pushoverService.sendAsync(msg).then( =>
__("pushover message sent successfully")
)
)
module.exports.PushoverActionHandler = PushoverActionHandler
# and return it to the framework.
return plugin