-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.php
More file actions
203 lines (174 loc) · 7.88 KB
/
Copy pathcallback.php
File metadata and controls
203 lines (174 loc) · 7.88 KB
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
<?php
// callback file for SMS reply TEST GIT
error_reporting(0);
//ini_set('display_errors', TRUE);
//ini_set('display_startup_errors', TRUE);;
include('db.php');
// Get the PHP helper library from twilio.com/docs/php/install
require_once 'vendor/autoload.php'; // Loads the library
use SignalWire\Rest\Client;
use SignalWire\LaML;
include_once('./twilio_config.php');
$client = new Client(SID, TOKEN, array("signalwireSpaceUrl" => SIGNALWIREURL) );
$from = $_REQUEST['From'];// '+919510212332';
$body = trim(strtolower($_REQUEST['Body']));
$to = $_REQUEST['To'];// '+15005550006';
//$content = http_build_query($_REQUEST);
//file_put_contents('./log.txt', $content,FILE_APPEND);
//exit;
if ($body != '') {
$statement = $connection->prepare("SELECT * FROM member WHERE phone = '" . $from . "' ORDER BY 1 DESC LIMIT 1");
$statement->execute();
$member = $statement->fetch(PDO::FETCH_ASSOC);
$statement = $connection->prepare("SELECT * FROM send_sms WHERE m_to = '" . $_REQUEST['From'] . "' ORDER BY 1 DESC LIMIT 1");
$statement->execute();
$user = $statement->fetch(PDO::FETCH_ASSOC);
$reply_arr = array('yes', 'no');
$sub_reply_arr = array('start', 'stop');
if ($body == 'start') {
if (!empty($member)) {
//already member want to confirm
$statement = $connection->prepare(" UPDATE member SET is_active = 'Y', modify_date = '" . date("Y-m-d H:i:s") . "' WHERE uid = '" . $member["uid"] . "' ");
$result = $statement->execute();
} else {
$statement = $connection->prepare("INSERT INTO member (phone, get_call,is_active, created_date) VALUES (:phone, :get_call, :is_active, :created_date) ");
$result = $statement->execute(
array(
':phone' => $from,
':get_call' => 'N',
':is_active' => 'Y',
':created_date' => date("Y-m-d H:i:s"),
)
);
}
if (!empty($result)) {
$reply_text = CONFIRMED_SUB_START;
}
} elseif ($body == 'stop' and !empty($member)) {
$statement = $connection->prepare(" UPDATE member SET is_active = 'N', modify_date = '" . date("Y-m-d H:i:s") . "' WHERE uid = '" . $member["uid"] . "' ");
$result = $statement->execute();
if (!empty($result)) {
$reply_text = CONFIRMED_SUB_STOP;
}
} elseif ($body == 'call' and !empty($member)) {
$statement = $connection->prepare(" UPDATE member SET get_call = 'Y', modify_date = '" . date("Y-m-d H:i:s") . "' WHERE uid = '" . $member["uid"] . "' ");
$result = $statement->execute();
if (!empty($result)) {
$reply_text = CONFIRMED_SUB_CALL;
}
} elseif ($body == 'status' or $body == 'stats'){
$date = date('Y-m-d') . ' 00:00:00';
$statement = $connection->prepare("SELECT distinct seid FROM reply where reply_msg = 'yes' and created_date >= '$date'");
$statement->execute();
$count_yes = $statement->rowCount();
$statement = $connection->prepare("SELECT * FROM guest_count WHERE date = '".date('Y-m-d')."' ");
$statement->execute();
$guest_counter = $statement->fetch(PDO::FETCH_ASSOC);
$guest_count = $guest_counter['count'];
$total_join = ($count_yes + $guest_count);
$reply_text = str_replace('{COUNT}', $total_join, NOTIFY_MESSAGE) ;
} elseif (in_array($body, $reply_arr) and !empty($member)) {
$statement = $connection->prepare("SELECT * FROM reply WHERE seid = '" . $user["seid"] . "' ORDER BY 1 DESC LIMIT 1");
$statement->execute();
$reply = $statement->fetch(PDO::FETCH_ASSOC);
if (empty($reply)) {
$statement = $connection->prepare(" INSERT INTO reply (seid, m_from,reply_msg, created_date) VALUES (:seid, :m_from,:reply_msg, :created_date) ");
$result = $statement->execute(
array(
':seid' => $user["seid"],
':reply_msg' => $body,
':m_from' => $from,
':created_date' => date("Y-m-d H:i:s"),
)
);
} else {
$statement = $connection->prepare(" UPDATE reply SET reply_msg = :reply_msg WHERE seid = '" . $user["seid"] . "' ");
$result = $statement->execute(
array(
':reply_msg' => $body
//':created_date' => date("Y-m-d H:i:s"),
)
);
}
// check if 10 reply go then inform all members
$date = date('Y-m-d 00:00:00');
$statement = $connection->prepare("SELECT distinct m_to FROM send_sms JOIN reply ON reply.seid = send_sms.seid where reply.created_date >= '" . $date . "' AND reply.reply_msg = 'yes'");
$statement->execute();
$get_msgs = $statement->fetchAll(PDO::FETCH_ASSOC);
$get_msg_count = $statement->rowCount();
$statement = $connection->prepare("SELECT * FROM guest_count WHERE date = '" . date('Y-m-d') . "' ");
$statement->execute();
$guest_counter = $statement->fetch(PDO::FETCH_ASSOC);
$guest_count = $guest_counter['count'];
$statement = $connection->prepare("SELECT is_sent FROM is_confirm_sent WHERE date = '" . date('Y-m-d') . "' ");
$statement->execute();
$guest_counter = $statement->fetch(PDO::FETCH_ASSOC);
$is_sms_sent = (int)$guest_counter['is_sent'];
$total_join = $get_msg_count + $guest_count;
if ($total_join >= MINIMUM_REPLY_LIMIT && $is_sms_sent == 0) {
$statement = $connection->prepare("
INSERT INTO is_confirm_sent (date, is_sent )
VALUES (:date, :is_sent)
");
$result = $statement->execute(
array(
':date' => date("Y-m-d"),
':is_sent' => 1
)
);
$statement = $connection->prepare("SELECT distinct m_to FROM send_sms WHERE date_created >= '$date'");
$statement->execute();
$all_members = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($all_members as $member) {
$client->messages->create(
$member['m_to'],
array(
'from' => FROM_NUMBER,
'body' => CONFIRM_MESSAGE
)
);
}
// send sms to admin
$member_name = array();
foreach ($get_msgs as $phone_number) {
$statement = $connection->prepare("SELECT name FROM member WHERE phone = '$phone_number' ");
$statement->execute();
$member_info = $statement->fetch(PDO::FETCH_ASSOC);
$member_name[] = $member_info['name'];
}
$member_name = array_unique($member_name);
$admin_msg = "Today's Respondents:" . "\n";
$admin_msg .= implode(',', $member_name);
foreach (ADMIN_NUMBER as $adminNumber ){
$client->messages->create(
$adminNumber,
array(
'from' => FROM_NUMBER,
'body' => $admin_msg
)
);
}
}
} elseif (empty($member)) {
$reply_text = NOT_MEMBER;
} else {
$reply_text = NOT_YESNO_REPLY;
//someone not a member and not start
$message = $_REQUEST['From'] . ":" . $_REQUEST['Body'];
foreach (ADMIN_NUMBER as $adminNumber){
$client->messages->create(
$adminNumber,
array(
'from' => FROM_NUMBER,
'body' => $message,
)
);
}
}
// response with empty
$response = new LaML;
if (isset($reply_text)){
$response->message($reply_text);
}
echo $response;
}