-
Notifications
You must be signed in to change notification settings - Fork 11
/
pushbullet.coffee
122 lines (91 loc) · 3.95 KB
/
pushbullet.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
# Pushbullet Plugin
# This is an plugin to send push notifications via pushbullet
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
util = env.require 'util'
M = env.matcher
PushBullet = require('pushbullet');
Promise.promisifyAll(PushBullet.prototype)
pusherService = null
class PushbulletPlugin extends env.plugins.Plugin
init: (app, @framework, config) =>
apikey = config.apikey
env.logger.debug "apikey= #{apikey}"
pusherService = new PushBullet(apikey)
@framework.ruleManager.addActionProvider(new PushbulletActionProvider @framework, config)
plugin = new PushbulletPlugin()
class PushbulletActionProvider extends env.actions.ActionProvider
constructor: (@framework, @config) ->
return
parseAction: (input, context) =>
defaultTitle = @config.title
defaultMessage = @config.message
defaultDevice = @config.device
defaultType = @config.type
if @config.channeltag != "" then defaultDevice = {channel_tag: @config.channeltag}
# Helper to convert 'some text' to [ '"some text"' ]
strToTokens = (str) => ["\"#{str}\""]
# Helper to convert [ '"some text"' ] to 'some text'
tokensToStr = (tokens) => tokens[0].replace(/\'|\"/g, "")
titleTokens = strToTokens defaultTitle
messageTokens = strToTokens defaultMessage
typeTokens = strToTokens defaultType
device = defaultDevice
setTitle = (m, tokens) => titleTokens = tokens
setMessage = (m, tokens) => messageTokens = tokens
setType = (m, tokens) => typeTokens = tokens
setChannel = (m, tokens) => device = {channel_tag: tokensToStr tokens}
m = M(input, context)
.match('send ', optional: yes)
.match(['push','pushbullet','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(' type:').matchStringWithVars(setType)
if next.hadMatch() then m = next
next = m.match(' channel:').matchStringWithVars(setChannel)
if next.hadMatch() then m = next
if m.hadMatch()
match = m.getFullMatch()
assert Array.isArray(titleTokens)
assert Array.isArray(messageTokens)
assert Array.isArray(typeTokens)
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new PushbulletActionHandler(
@framework, titleTokens, messageTokens, typeTokens, device
)
}
class PushbulletActionHandler extends env.actions.ActionHandler
constructor: (@framework, @titleTokens, @messageTokens, @typeTokens, @device) ->
executeAction: (simulate, context) ->
Promise.all( [
@framework.variableManager.evaluateStringExpression(@titleTokens)
@framework.variableManager.evaluateStringExpression(@messageTokens)
@framework.variableManager.evaluateStringExpression(@typeTokens)
]).then( ([title, message, type]) =>
switch type
when "file"
if simulate
return __("would send file \"%s\" with title \"%s\"", message, title)
else
try
return pusherService.fileAsync(@device, message, title).then( =>
__("pushbullet file sent successfully")
)
catch e
return __("File not found!") if e.code is "ENOENT"
when "note"
if simulate
return __("would push message \"%s\" with title \"%s\"", message, title)
else
return pusherService.noteAsync(@device, title, message).then( =>
__("pushbullet message sent successfully")
)
)
module.exports.PushbulletActionHandler = PushbulletActionHandler
# and return it to the framework.
return plugin