-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.rules.inc
242 lines (233 loc) · 6.55 KB
/
twilio.rules.inc
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* @file
* Rules module integration
*/
/**
* Implements hook_rules_data_info().
*/
function twilio_rules_data_info() {
return array(
'sms' => array(
'label' => t('Twilio incoming SMS'),
'wrap' => TRUE,
'property info' => array(
'number' => array(
'type' => 'text',
'label' => t('Number'),
'description' => t('The phone number of the incoming SMS.'),
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'number_twilio' => array(
'type' => 'text',
'label' => t('Twilio Number'),
'description' => t('The Twilio phone number that receieved the message.'),
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'message' => array(
'type' => 'text',
'label' => t('Message'),
'description' => t('The message of the incoming SMS.'),
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'media' => array(
'type' => 'text',
'label' => t('Media'),
'description' => t('The media attached to the incoming SMS.'),
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
),
),
'voice' => array(
'label' => t('Twilio incoming Voice'),
'wrap' => TRUE,
'property info' => array(
'number' => array(
'type' => 'text',
'label' => t('Number'),
'description' => t('The phone number of the incoming call.'),
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
),
),
);
}
/**
* Implements hook_event_info().
*/
function twilio_rules_event_info() {
$defaults = array(
'group' => t('Twilio'),
'module' => 'twilio',
);
return array(
'twilio_sms_incoming' => $defaults + array(
'label' => t('Drupal receives an SMS'),
'variables' => array(
'sms' => array(
'type' => 'sms',
'label' => t('Twilio incoming SMS'),
),
),
),
'twilio_voice_incoming' => $defaults + array(
'label' => t('Drupal receives a voice call'),
'variables' => array(
'voice' => array(
'type' => 'voice',
'label' => t('Twilio incoming Voice'),
),
),
),
);
}
/**
* Implements hook_rules_condition_info().
*/
function twilio_rules_condition_info() {
return array(
'twilio_rules_condition_number_belongs_to_user' => array(
'label' => t('Number belongs to a valid user'),
'arguments' => array(
'number' => array(
'type' => 'text',
'label' => t('Phone Number'),
),
),
'group' => t('Twilio'),
'module' => 'twilio',
),
'twilio_rules_condition_user_has_verified_number' => array(
'label' => t('User has a verified number'),
'arguments' => array(
'account' => array(
'type' => 'user',
'label' => t('User'),
),
),
'group' => t('Twilio'),
'module' => 'twilio',
),
);
}
/**
* Implements hook_rules_action_info().
*/
function twilio_rules_action_info() {
$defaults = array(
'group' => t('Twilio'),
'module' => 'twilio',
);
$actions['twilio_send_sms_to_user'] = $defaults + array(
'label' => t('Send an SMS to a user'),
'base' => 'twilio_rules_action_send_sms_to_user',
'parameter' => array(
'account' => array(
'type' => 'user',
'label' => t('User'),
'save' => TRUE,
),
'message' => array(
'type' => 'text',
'label' => t('Message'),
),
'media' => array(
'type' => 'text',
'optional' => TRUE,
'label' => t('Media path'),
),
),
);
$actions['twilio_send_sms_to_all_users'] = $defaults + array(
'label' => t('Send an SMS to all users'),
'base' => 'twilio_rules_action_send_sms_to_all_users',
'parameter' => array(
'message' => array(
'type' => 'text',
'label' => t('Message'),
),
'media' => array(
'type' => 'text',
'optional' => TRUE,
'label' => t('Media path'),
),
),
);
$actions['twilio_send_sms_to_number'] = $defaults + array(
'label' => t('Send an SMS to a number'),
'base' => 'twilio_rules_action_send_sms_to_number',
'parameter' => array(
'country' => array(
'type' => 'text',
'label' => t('Country code'),
'options list' => 'twilio_country_codes',
),
'number' => array(
'type' => 'text',
'label' => t('Number'),
),
'message' => array(
'type' => 'text',
'label' => t('Message'),
),
'media' => array(
'type' => 'text',
'optional' => TRUE,
'label' => t('Media path'),
),
),
);
return $actions;
}
/**
* Action: Send an SMS to a user.
*/
function twilio_rules_action_send_sms_to_user($account, $message, $media) {
if (!empty($account->twilio_user['number']) && $account->twilio_user['status'] == 2) {
twilio_send($account->twilio_user['number'], $message, $account->twilio_user['country'], $media);
}
}
/**
* Action: Send an SMS to all users.
*/
function twilio_rules_action_send_sms_to_all_users($message, $media) {
// Fetch all site users with confirmed numbers.
$results = db_select('twilio_user', 't')
->fields('t')
->condition('t.status', TWILIO_USER_CONFIRMED)
->execute()
->fetchAll();
// If we have results, iterate through and send the message.
if (!empty($results)) {
foreach ($results as $result) {
twilio_send($result->number, $message, $result->country, $media);
}
}
}
/**
* Action: Send an SMS to a number.
*/
function twilio_rules_action_send_sms_to_number($country, $number, $message, $media) {
twilio_send($number, $message, $country, $media);
}
/**
* Condition: Validate if a number belongs to a user on the site.
*/
function twilio_rules_condition_number_belongs_to_user($number) {
return twilio_verify_number($number);
}
/**
* Condition: Validate if the user has a verified number.
*/
function twilio_rules_condition_user_has_verified_number($user) {
// Check if the user has TWILIO_USER_CONFIRMED status.
if (!empty($user->twilio_user) && $user->twilio_user['status'] == TWILIO_USER_CONFIRMED) {
return TRUE;
}
return FALSE;
}