-
Notifications
You must be signed in to change notification settings - Fork 2
/
ntxuva_sms.module
266 lines (208 loc) · 7.24 KB
/
ntxuva_sms.module
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
// ntxuva_sms.module
/**
* Implements hook_menu().
*/
function ntxuva_sms_menu() {
$items = array();
$items['admin/config/ntxuva/open311-sms'] = array(
'title' => 'Configure Ntxuva Open311 SMS',
'description' => 'Settings for Ntxuva Open311 SMS Module',
'page callback' => 'drupal_get_form',
'page arguments' => array('ntxuva_sms_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['ntxuva/open311-sms'] = array(
'access callback' => TRUE,
'page callback' => 'ntxuva_sms_handle',
'access arguments' => array('view published content'),
'type' => MENU_CALLBACK,
// 'delivery callback' => 'drupal_json_output',
);
return $items;
}
?>
<?php
/**
* Ntxuva Open311 SMS config settings
*
*/
function ntxuva_sms_form($form, &$form_state) {
global $base_url;
$ntxuva_default_endpoint = $base_url . "georeport/v2/";
$form['ntxuva_sms_secret_key'] = array(
'#type' => 'textfield',
'#title' => t('Enter your secret key'),
'#default_value' => variable_get('ntxuva_sms_secret_key', 'ntxuva-default-password'),
'#size' => 50,
'#maxlength' => 50,
'#description' => t('Secret key for secure communication with Telerivet'),
'#required' => TRUE,
);
$form['nxtuva_open311_endpoint'] = array(
'#type' => 'textfield',
'#title' => t('Enter Open311 GeoReport v2 endpoint'),
'#default_value' => variable_get('nxtuva_open311_endpoint', $ntxuva_default_endpoint),
'#size' => 100,
'#maxlength' => 100,
'#description' => t('Open311 GeoReport v2 API Endpoint for SMS interaction'),
'#required' => TRUE,
);
return system_settings_form($form);
}
?>
<?php
/**
* Ntxuva Open311 SMS Handler
*
*/
function ntxuva_sms_handle() {
if (empty($_POST)) { // check if request is ok
header('HTTP/1.1 403 Forbidden');
echo "Empty post";
return;
}
$secret = variable_get('ntxuva_sms_secret_key', "ntxuva-maputo");
if ($secret !== $_POST['secret']) { // check secret key
header('HTTP/1.1 403 Forbidden');
echo "Invalid secret provided. \n";
echo "Post key:" . $_POST['secret'];
return;
}
if ($_POST['event'] == 'incoming_message')
{
watchdog("ntxuva_sms", "New request: " .print_r($_POST,true)); // save request for debugging
// Check first word and decide what user wants:
// 1) submit a new service_request or
// 2) find status of an existing one.
$content = str_word_count($_POST['content'], 1, 'àáãéíóúóèç1234567890-');
switch($content[0]) {
case "estado":
ntxuva_sms_query_request();
break;
case "ajuda":
break;
default:
ntxuva_sms_create_request();
}
}
return;
}
?>
<?php
/**
* Deals with query from user
*
*/
function ntxuva_sms_query_request() {
// Find out API endpint
global $base_url;
$ntxuva_default_endpoint = $base_url . "/georeport/v2/";
$endpoint = variable_get('nxtuva_open311_endpoint', $ntxuva_default_endpoint);
$content = str_word_count($_POST['content'], 1, 'àáãéíóúóèç1234567890-');
// Validation: Look for request_id
$request_id = $content[1]; // ignore the rest
$url = "http://{$endpoint}requests/{$request_id}.json";
// Request status from Open311 Endpoint
$json = file_get_contents($url);
$res = json_decode($json, true);
// If successful, send status
if (isset($res[0]['status'])) {
$reply = "Obrigado pela sua mensagem! O estado do seu pedido $request_id é: ". $res[0]['status'];
watchdog("ntxuva_sms", "Got response from Open311: " .print_r($res,true)); // save request for debugging
}
// If not, apologize
else {
$reply = "Lamentavelmente, não conseguimos encontrar o seu pedido numero '$request_id'.";
watchdog("ntxuva_sms", "Didn't got response from Open311. URL: '$url' Request: '$request_id' "); // save request for debugging
}
// Send reply to user
ntxuva_sms_reply($reply);
}
?>
<?php
function ntxuva_sms_create_request() {
global $base_url;
$ntxuva_default_endpoint = $base_url . "/georeport/v2/";
$endpoint = variable_get('nxtuva_open311_endpoint', $ntxuva_default_endpoint);
$url = "http://{$endpoint}requests.json";
$from_number = $_POST['from_number']."@sms.mz"; // to overcome Mark-a-spot's request for email
// Store information from SMS
$content = str_word_count($_POST['content'], 1, 'àáãéíóúóèç1234567890-');
$location = $content[0];
$location_result = taxonomy_get_term_by_name($location,'points');
$category = $content[1];
$category_result = taxonomy_get_term_by_name($category,'categories');
// Validate location
if(empty($location_result)) {
$reply = "Lamentavelmente, não conseguimos introduzir o seu pedido. O seu ponto de recolha não foi reconhecido.";
ntxuva_sms_reply($reply);
return;
}
else {
$tid = key($location_result);
$location_result = $location_result[$tid];
$loc = entity_metadata_wrapper('taxonomy_term', $location_result);
$lat = $loc->field_lat->value();
$long = $loc->field_long->value();
}
// Look for category by SMS code
if(empty($category_result)) {
$reply = "Lamentavelmente, não conseguimos introduzir o seu pedido. A categoria não foi reconhecida.";
ntxuva_sms_reply($reply);
return;
}
else {
$tid = key($category_result);
$category_result = $category_result[$tid];
$cat = entity_metadata_wrapper('taxonomy_term', $category_result);
$service_code = $cat->field_service_code->value();
}
// Prepare JSON
$request = array(
'email' => $from_number,
'long' => $long,
'lat' => $lat,
'description' => $_POST['content'],
'service_code' => $service_code
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $request ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
watchdog("ntxuva_sms", "Preparing JSON: " . print_r($request,true)); // save request for debugging
$context = stream_context_create( $options );
// Send JSON POST to Submit issue
$result = file_get_contents($url, false, $context);
$res = json_decode($result, true);
// Define reply message
// If error, send verbose message to user.
if (isset($res[0]['service_request_id'])) {
$reply = "Obrigado pela sua mensagem! O seu pedido foi guardado com o código: " . $res[0]['service_request_id'];
ntxuva_sms_reply($reply);
watchdog("ntxuva_sms", "Open311: New service request succesfully submitted: " .print_r($res,true)); // save request for debugging
}
// If not, apologize
else {
watchdog("ntxuva_sms", "Open311: could not create request"); // save request for debugging
ntxuva_sms_reply("Lamentavelmente, não conseguimos introduzir o seu pedido, por favor tente mais tarde'.");
}
return;
}
?>
<?php
function ntxuva_sms_reply($reply) {
header("Content-Type: application/json");
echo json_encode(array('messages' =>
array(
array('content' => $reply)
)
));
return;
}
?>