-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.php
100 lines (60 loc) · 2.54 KB
/
test.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
<?php
/**
* Created by PhpStorm.
* User: Michael Marshal
* Date: 10/4/2017
* Time: 4:10 PM
*/
ini_set('error_log', 'error.log');
include 'lib/SMSSender.php';
include 'lib/SMSReceiver.php';
$Happycount=0;
$Shapecount=0;
$Sadcount=0;
date_default_timezone_set("Asia/Colombo");
$password= "";
$applicationId = "";
$serverurl= "https://api.dialog.lk/sms/send";
try{
$receiver = new SMSReceiver(file_get_contents('php://input'));
$content =$receiver->getMessage();
$content=preg_replace('/\s{2,}/',' ', $content);
$address = $receiver->getAddress();
$requestId = $receiver->getRequestID();
$applicationId = $receiver->getApplicationId();
$sender = new SMSSender($serverurl, $applicationId, $password);
list($key, $second) = explode(" ",$content);
if ($second=="food") {
$boradmsg = substr($content,9);
$url = "https://language.googleapis.com/v1/documents:analyzeSentiment?key=<key>";
$ch = curl_init($url);
$json=array("encodingType"=> "UTF8", "document"=> array("type"=> "PLAIN_TEXT", "content"=> $boradmsg));
$encode_data=json_encode($json,JSON_NUMERIC_CHECK | JSON_FORCE_OBJECT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encode_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
$decode=json_decode($response,true);
curl_close($ch);
error_log("Broadcast Message ".$content);
$emotion=$decode['documentSentiment']['score'];
if($emotion>0.25){
$Happycount++;
$sender->sendMessage("Please come again and let the word spread",$address);
}elseif ($emotion<0.25&&$emotion>-0.25){
$Shapecount++;
$sender->sendMessage("We are happy to have you, please come again",$address);
}elseif ($emotion<-0.25){
$Sadcount++;
$sender->sendMessage("Oops! We will improve the quality of the food.",$address);
}
$response=$sender->broadcastMessage($boradmsg);
}else{
error_log("Message received ".$content);
$sender->sendMessage("Oops! Check the format. It should be SHOP<space>food<space>and enter your feeling about the food.".$second,$address);
}
}catch (SMSServiceException $e){
error_log("Passed Exception-not working ".$e);
$sender->sendMessage("Oops! something went wrong in the server side make sure you complain to them!",$address);
}