This repository has been archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SMSController.php
428 lines (322 loc) · 13.6 KB
/
SMSController.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class SMSController extends Controller
{
public $success = 1;
public $found = 1;
public $id = 0;
public function process_received($phone, $message){
//log message
$this->id = $this->log_received($phone, $message);
$response = null;
//check if has phone number
if($phone=="") {
return null;
}
//clean message
$message = trim($message);
//check if message is empty
if(strlen($message)<1){
$response = $this->error_message(null, true);
}
//if has nhif keywords: get location
if($this->has_nhif_keywords(strtolower($message))){
$response = $this->find_nhif_coverage($message);
var_dump($response);
}else if($this->has_doctor_keywords(strtolower($message))){
$response = $this->find_doctor($message);
}else{
$specialty = $this->has_medical_service_tags($message);
if($specialty!=false){
$response = $this->find_facilities($message, $specialty);
} else {
//If else fails try Artificial Intelligence
$found_entities = $this->find_entities($message);
if ($found_entities != null) {
//sort by relevance
if (count($found_entities) == 0) {
$response = $this->error_message();
} else {
$found = false;
foreach ($found_entities as $item) {
if ($item["type"] == "Person") {
$response = $this->find_doctor_by_name($item["text"]);
$found = true;
break;
} else if (($item["type"] == "Facility") || ($item["type"] == "Organization") || ($item["type"] == "Company")) {
$response = $this->find_hospital_by_name($item["text"]);
$found = true;
break;
} else if (in_array($item["type"], $this->location_tags())) {
//if has location return nearest facilities
$response = $this->find_facilities_by_location("", "", $item["text"]);
$found = true;
break;
}
}
//if nothing found, error
if ($found == false) {
$response = $this->error_message();
}
}
} else {
$response = $this->error_message();
}
}
}
return array("id"=>$this->id, "response"=>$response, "success"=>$this->success, "found"=>$this->found);
}
public function location_tags(){
return array("City", "GeographicFeature", "AdministrativeDivision","BodyOfWater", "Brand", "CityTown", "CompanyDivision", "CompanyShareholder", "Country", "Cuisine", "GeographicFeature", "GovernmentalJurisdiction", "HumanLanguage", "Island", "IslandGroup", "Kingdom", "Location", "MilitaryPost", "MountainRange", "Organization", "PoliticalDistrict", "Region", "USCounty", "USState");
}
public function error_message($type=null, $isEmpty=false){
$examples = "Example query formats:\n";
$examples .= "1. Doctor James Gicheru\n";
$examples .= "2. Hospital in Kiambu\n";
$examples .= "3. NHIF in Nairobi\n";
if($isEmpty){
$response = $examples;
}else if($type != null){
if ($type == 1) {
$response = "Could not find a doctor with that name.";
} else if ($type == 2) {
$response = "Sorry, location could not be understood. Check for spelling mistakes.";
} else if ($type == 3) {
$response = "No facility found matching your request.";
} else {
$response = "Sorry, location could not be understood. Check for spelling mistakes.";
}
}else{
$response = "Could not understand your request. Please try the web services at http://health.the-star.co.ke\n".$examples;
$this->success = 0;
}
return $response;
}
public function find_nhif_coverage($message){
$location = $this->get_location($message);
if($location == null){
$this->success = 0;
return $this->error_message(2);
}else{
//Get NHIF coverage
$result = NHIFController::coverage(null, "", $location, true);
return $this->process_result($result, 1);
}
}
public function find_doctor($message){
$name = $this->process_for_doctor_name(strtolower($message));
return $this->find_doctor_by_name($name);
}
public function get_location($message){
$found = false;
$found_location = $this->find_entities($message);
if ($found_location != null) {
if (count($found_location) != 0) {
foreach ($found_location as $item) {
if (in_array($item["type"], $this->location_tags())) {
//return first location
$response = $item["text"];
foreach ($this->services_keywords() as $service) {
foreach (explode(",", $service) as $value) {
$response = str_replace(trim($value)." ", "", $response);
}
};
$found = true;
break;
}
}
}
}
if($found==false && $this->has_location_adj($message)){
$response = $this->process_for_location($message);
return $response;
}else if($found == false) {
return null;
}
return $response;
}
public function find_facilities($message, $specialty){
//medical service tag has been found, now find location
$location = $this->get_location($message);
if($location == null){
$this->success = 0;
return $this->error_message(4);
}else{
//we have location, so output results
return $this->find_facilities_by_location($specialty, "", $location);
}
}
/**
* @param $name
* @return string
*/
public function find_doctor_by_name($name){
if($name == null){
$this->success = 0;
return $this->error_message(1);
}else{
$result = (new DoctorsController())->getData($name, true);
return $this->process_result($result, 1);
}
}
/**
* @param $name
* @return string
*/
public function find_facility_by_name($name){
if($name == null){
$this->success = 0;
return $this->error_message(1);
}else{
$result = (new HospitalsController())->getClinic($name, true);
return $this->process_result($result, 1);
}
}
public function process_result($result, $type){
if($result == false){
$this->found = 0;
return $this->error_message($type);
}else{
return $result;
}
}
public function find_facilities_by_location($specialty, $address, $location){
$result = HospitalsController::specialty($specialty, $address, $location, true);
return $this->process_result($result, 3);
}
public function has_medical_service_tags($message){
$services_keywords = $this->services_keywords();
//return $this->array_element_in_string($message, $services_keywords);
$i = 0;
foreach($services_keywords as $key){
//explode by comma first
$keys = explode(", ", $key);
foreach($keys as $word) {
if($this->string_in_string(strtolower($message), strtolower($word))){
$service_abbr = $this->services_abbr()[$i];
return $service_abbr;
break;
}
}
$i++;
}
return false;
}
public function services_abbr(){
return array("ANC", "ART", "BEOC", "BLOOD", "CAES SEC", "CEOC", "C-IMCI", "EPI", "FP", "GROWM", "HBC", "HCT", "IPD", "OPD", "OUTREACH", "PMTCT", "RAD/XRAY", "RHTC/RHDC", "TB DIAG", "TB LABS", "TB TREAT", "YOUTH", "ALL");
}
public function services_keywords(){
return array("Antenatal Care, antenatal, pregnant, pregnancy", "Anteretroviral, anteretrovials, ARV, ARvs", "Beoc", "Blood", "Caeserean section, Caeserean", "Ceoc", "C-IMCI", "Epidemiology", "Family planning", "GROWM", "Heamogram, blood test", "Heamatocrit, anaemia", "In-patient, inpatient", "Out-patient, outpatien", "Outreach", "Prevention of mother to child transmission HIV/AIDS", "Radiology, x-ray, xray, x ray", "Reproductive health, reproductive", "Tuberculosis diagnosis, TB, T.B, T.B.", "Tuberculosis laboratory work up, TB, T.B, T.B.", "Tuberculosis treatment, TB", "Youth", "Hospital, clinic");
}
public function find_entities($message){
$alchemyapi = new \AlchemyAPI();
$response = $alchemyapi->entities('text', 'There is '.$message, null);
if ($response['status'] == 'OK') {
/*//sort by relevance
usort($response['entities'], function($a, $b) {
return $a['relevance'] - $b['relevance'];
});
*/
return $response['entities'];
} else {
return null;
}
}
public function has_doctor_keywords($message){
$doctor_keywords = $this->doctor_keywords();
return $this->array_element_in_string($message, $doctor_keywords);
}
public function doctor_keywords(){
return array("doctor", "daktari", "laktar", "dr.", "daktar", "dr");
}
public function nhif_keywords(){
return array("nhif", "nihf", "nhfi");
}
public function has_nhif_keywords($message){
$nhif_keywords = $this->nhif_keywords();
return $this->array_element_in_string($message, $nhif_keywords);
}
public function has_location_adj($message){
$loc_adjs = $this->location_adjectives();
return $this->array_element_in_string($message, $loc_adjs);
}
public function array_element_in_string($string, $array){
foreach($array as $key){
if($this->string_in_string($string, $key)){
return true;
break;
}
}
return false;
}
public function string_in_string($string, $key){
$string = strtolower($string);
$key = strtolower($key);
if (strpos($string, $key) !== false){
return true;
}else{
return false;
}
}
public function location_adjectives(){
return array("in", "near", "around", "next to", "at");
}
public function process_for_location($message){
//Hopefully it doesn't come to this
$response = $this->find_string_after_keyword($message, $this->location_adjectives(), "location");
return $response;
}
public function process_for_doctor_name($message){
$response = $this->find_string_after_keyword($message, $this->doctor_keywords(), "doctor");
return $response;
}
public function find_string_after_keyword($message, $array_of_keywords, $type){
$response = null;
for($i=0; $i<count($array_of_keywords); $i++){
$separator = $array_of_keywords[$i];
if($this->string_in_string($message, $separator)){
if($type == "location"){
$parts = explode(" ".$separator." ", $message);
}else{
//type is doc
$parts = explode($separator." ", $message);
}
if(count($parts)>1){
$response = $parts[1];
}else{
$response = $parts[0];
}
}
}
return $response;
}
public function send_response($phoneNumber, $result){
$message = $result["response"];
$url = config('custom_config.sms_send_url');
$user = config('custom_config.sms_user');
$pass = config('custom_config.sms_pass');
$messageID = "";
$shortCode = config('custom_config.sms_shortCode');
$options = array("user"=>$user, "pass"=>$pass, "messageID"=>$this->id, "shortCode"=>$shortCode, "MSISDN"=>$phoneNumber, "MESSAGE"=>$message);
$url .= http_build_query($options,'','&');
$page = file_get_contents($url);
//$data = json_decode($page, TRUE);
//TODO: Ask provider to provide json reponse
//log sent message
$this->log_sent($result);
return $result;
}
public function log_received($phone, $message){
$now = date('Y-m-d H:i:s');//TODO: Find out why migration timestamps() doesn't work
$id = DB::table('sms')->insertGetId(
array('query' => $message, 'phone_number' => $phone, 'created_at' => $now)
);
return $id;
}
public function log_sent($result){
DB::table('sms')
->where('id', $result["id"])
->update(array('found' => $result["found"], "successfull"=>$result["success"], "responded"=>1, "response"=>$result["response"]));
}
}