-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.picolabs.twilio.sms.krl
More file actions
81 lines (68 loc) · 2.31 KB
/
io.picolabs.twilio.sms.krl
File metadata and controls
81 lines (68 loc) · 2.31 KB
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
ruleset io.picolabs.twilio.sms {
meta {
name "Twilio Module for SMS only"
description "Utility methods for sending SMS with Twilio"
author "Phil Windley"
version "0.1.0"
provides send_sms
shares show_configuration
configure using from_number = "801-555-1212"
account_sid = ""
auth_token = ""
}
global {
show_configuration = function() {
return {"account_sid": ent:account_sid,
"auth_token": ent:auth_token,
"from_number": ent:from_number}
}
// from_number = from_number || show_configuration(){["from_number"]}
// account_sid = account_sid || show_configuration(){["account_sid"]}
// auth_token = auth_token || show_configuration(["auth_token"] )
// base_url = <<https://#{account_sid}:#{auth_token}@api.twilio.com/2010-04-01/Accounts/#{account_sid}/>>
base_url = <<https://api.twilio.com/2010-04-01/Accounts/#{ent:account_sid}/>>
//outgoing actions
send_sms = defaction(message, to, from=ent:from_number){
http:post((base_url + "Messages.json"),
form = {
"From":from,
"To":to,
"Body":message
},
auth = {
"username": ent:account_sid,
"password": ent:auth_token
}) setting (resp);
return resp
};
}
rule save_config {
select when sensor configuration
pre {
auth_token = event:attr("twilio_auth_token")
account_sid = event:attr("twilio_account_sid")
from_number = event:attr("twilio_from_number") // number twilio gives you in E.164 format
}
if not (auth_token.isnull()
|| account_sid.isnull()
|| from_number.isnull()
) then noop()
fired {
log info "Configuring twilio";
ent:account_sid := account_sid;
ent:auth_token := auth_token;
ent:from_number := from_number;
}
}
// to use create a channel that allows the twilio domain; delete channel when done testing for security
rule test_config {
select when twilio test
pre {
msg = event:attr("msg")
}
send_sms(<<Test message: #{msg}>>, "+18013625611") setting(resp)
always {
log info <<Test message sent: #{msg}; #{resp.klog("Response")} >>
}
}
}