-
Notifications
You must be signed in to change notification settings - Fork 1
/
ussdap.php
197 lines (154 loc) · 7 KB
/
ussdap.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
<?php
date_default_timezone_set("Asia/Colombo");
ini_set('error_log', 'ussd-app-error.log');
require 'class/db.php';
require 'libs/MoUssdReceiver.php';
require 'libs/MtUssdSender.php';
require 'libs/sms/SMSSender.php';
require 'class/operationsClass.php';
require 'libs/Log.php';
// Code for LBS importing
include_once 'lbs/libs/LbsClient.php';
include_once 'lbs/libs/LbsRequest.php';
include_once 'lbs/libs/LbsResponse.php';
include_once "lbs/libs/KLogger.php";
include 'lbs/conf/lbs-conf.php';
// import end
$production = true;
$APP_ID = "APP_ID";
$PASSWORD = "APP_PASSWORD";
if ($production == false) {
$ussdserverurl = 'http://localhost:7000/ussd/send';
$LBS_SERVER_URL = "http://localhost:7000/lbs/locate";
$SMS_SERVER_URL = "http://localhost:7000/sms/send";
} else {
$ussdserverurl = 'https://api.dialog.lk/ussd/send';
$LBS_SERVER_URL = 'https://api.dialog.lk/lbs/locate';
$SMS_SERVER_URL = "https://api.dialog.lk/sms/send";
}
$log = new Logger();
$receiver = new UssdReceiver();
$sender = new UssdSender($ussdserverurl, $APP_ID, $PASSWORD);
$operations = new Operations();
$receiverSessionId = $receiver->getSessionId();
$content = $receiver->getMessage(); // get the message content
$address = $receiver->getAddress(); // get the sender's address
$requestId = $receiver->getRequestID(); // get the request ID
$applicationId = $receiver->getApplicationId(); // get application ID
$encoding = $receiver->getEncoding(); // get the encoding value
$version = $receiver->getVersion(); // get the version
$sessionId = $receiver->getSessionId(); // get the session ID;
$ussdOperation = $receiver->getUssdOperation(); // get the ussd operation
// Code for LBS
$request = new LbsRequest($LBS_SERVER_URL);
$request->setAppId($APP_ID);
$request->setAppPassword($PASSWORD);
$request->setSubscriberId($address);
$request->setServiceType($SERVICE_TYPE);
$request->setFreshness($FRESHNESS);
$request->setHorizontalAccuracy($HORIZONTAL_ACCURACY);
$request->setResponseTime($RESPONSE_TIME);
// LBS end
$responseMsg = "I want to Recycle\n";
$response_msg_qry = "SELECT * FROM collectables";
$responseMsg_reslt = mysqli_query($connection, $response_msg_qry);
while ($line = mysqli_fetch_array($responseMsg_reslt)) {
$responseMsg .= $line["id"] . "." . $line["type"] . "\n";
}
$responseMsg .= "10.All\n99.Exit";
if ($ussdOperation == "mo-init") {
try {
// Code for LBS
$lbsClient = new LbsClient();
$lbsResponse = new LbsResponse($lbsClient->getResponse($request));
$lbsResponse->setTimeStamp(getModifiedTimeStamp($lbsResponse->getTimeStamp())); //Changing the timestamp format. Ex: from '2013-03-15T17:25:51+05:30' to '2013-03-15 17:25:51'
// LBS end
$sessionArrary = array("sessionid" => $sessionId, "tel" => $address, "menu" => "main", "pg" => "", "others" => "", "longitude" => $lbsResponse->getLongitude(), "latitude" => $lbsResponse->getLatitude()); // longitude latitude
$operations->setSessions($sessionArrary);
$sender->ussd($sessionId, $responseMsg, $address);
// $sender->ussd($sessionId, $resp, $address);
} catch (Exception $e) {
$sender->ussd($sessionId, 'Sorry error occured try again', $address);
}
} else {
$flag = 0;
$sessiondetails = $operations->getSession($sessionId);
$cuch_menu = $sessiondetails['menu'];
$operations->session_id = $sessiondetails['sessionsid'];
switch ($cuch_menu) {
case "main": // Following is the main menu
if ($receiver->getMessage() == "99") {
$sender->ussd($sessionId, 'Thank you using our service !', $address, 'mt-fin');
} elseif ($receiver->getMessage() == "10") {
$operations->session_menu = "sub";
$operations->saveSesssion();
$operations->res_arry = $operations->getGcolectors("All", $sessiondetails["latitude"], $sessiondetails["longitude"]);
$sender->ussd($sessionId, $operations->setArtoStr($operations->res_arry, $sessionId), $address);
} elseif ($receiver->getMessage() == "") {
$operations->session_menu = "main";
$operations->saveSesssion();
$sender->ussd($sessionId, $responseMsg, $address);
} else {
$operations->session_menu = "sub";
$operations->saveSesssion();
$operations->res_arry = $operations->getGcolectors($receiver->getMessage(), $sessiondetails["latitude"], $sessiondetails["longitude"]);
$sender->ussd($sessionId, $operations->setArtoStr($operations->res_arry, $sessionId, $receiver->getMessage()), $address);
}
break;
case "sub":
$respons_no = $receiver->getMessage();
if ($respons_no == "99") {
$sender->ussd($sessionId, 'Thank you using our service !', $address, 'mt-fin');
break;
} else {
$operations->session_menu = "main";
$operations->saveSesssion();
// $rep_msg = $operations->getGcolDetails($respons_no, $operations->res_arry);
$sender->ussd($sessionId, "Request completed provide the pin number in the sms to the collector.\nThank You !", $address, 'mt-fin');
$rep_msg = $operations->getGcolDetails($respons_no, $sessionId, $operations);
$sms_sender = new SMSSender($SMS_SERVER_URL, $APP_ID, $PASSWORD);
$sms_sender->sms($rep_msg, $address);
$params = array(
"sessionId" => $sessionId
);
httpPost("https://ideamarthosting.dialog.lk/milindalApps/IdeaApp/tocollector.php", $params);
// $sender->ussd($sessionId, "Request completed provide the pin number in the sms to the collector.\nThank You !", $address, 'mt-fin');
break;
}
case "99":
$sender->ussd($sessionId, 'Thank you using our service !', $address, 'mt-fin');
break;
default:
$operations->session_menu = "main";
$operations->saveSesssion();
$sender->ussd($sessionId, 'Incorrect option ' . $cuch_menu, $address);
break;
}
}
function getModifiedTimeStamp($timeStamp) {
try {
$date = new DateTime($timeStamp, new DateTimeZone('Asia/Colombo'));
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
return $date->format('Y-m-d H:i:s');
}
function httpPost($url, $params) {
$postData = '';
//create name value pairs seperated by &
foreach ($params as $k => $v) {
$postData .= $k . '=' . $v . '&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>