-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemindBotCommand.php
More file actions
146 lines (137 loc) · 6.41 KB
/
Copy pathRemindBotCommand.php
File metadata and controls
146 lines (137 loc) · 6.41 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
<?php
/**
* Class RemindBotCommand
* This class schedules a reminder for the sender.
*
* Usage:
* REMIND <message> ON <date and time in YYYY-MM-DD hh:mm(:ss) format>
* REMIND <message> AFTER <number> <SECONDS | MINUTES | HOURS>
*
* @author: Angelito Sardez, Jr.
* @date: 17/11/2017
*/
class RemindBotCommand extends BotCommand {
public function __construct($sender, $user) {
parent::__construct("REMIND", $sender, $user);
}
protected function executeCommand($parameter) {
if (trim($parameter) == "") {
$this->sendTextWithHelp("Sorry ".$this->user->getFirstName().", you need to specify the date and time, or a number and the unit of time when you want me to remind you, using either of these formats:".
chr(10).
"REMIND <message> ON <date and time in YYYY-MM-DD hh:mm format>".
chr(10).
"REMIND <message> AFTER <number> <SECONDS | MINUTES | HOURS>");
return;
}
date_default_timezone_set('Asia/Manila');
if (!$this->validateCommand($parameter)) {
$sampleDateTime = new DateTime(date('Y-m-d H:i'));
$sampleDateTime->add(new DateInterval('PT5M'));
$this->sendTextWithHelp("Sorry ".$this->user->getFirstName().", I didn't get that. Please send your command in either of the following formats:".
chr(10).
"REMIND <message> ON <future date and time in YYYY-MM-DD hh:mm format> (e.g. REMIND me to send an email ON ".$sampleDateTime->format('Y-m-d H:i').")".
chr(10).
"REMIND <message> AFTER <number> <SECONDS | MINUTES | HOURS> (e.g. REMIND me to have a break AFTER 30 MINUTES)");
}
else {
$isDatePartSpecific = $this->getIsTargetDate($parameter);
$paramPart = explode($isDatePartSpecific ? " on " : " after ", strtolower($parameter));
$dateTime = trim($paramPart[sizeof($paramPart) - 1]);
if ($isDatePartSpecific) {
if (!$this->validateDateTime($dateTime)) {
$this->send("Sorry ".$this->user->getFirstName().", I didn't get that date and time. Please give me a future date and time in YYYY-MM-DD hh:mm format.");
return;
}
if (!$this->validateDateTimeIfInFuture($dateTime)) {
$this->send("Hey ".$this->user->getFirstName().", the date and time you have given me is from the past. Please give me a future date and time for your reminder.");
return;
}
}
else {
$fexibleDatePart = explode(" ", $dateTime);
$num = $fexibleDatePart[0];
$unitOfTime = trim($fexibleDatePart[sizeof($fexibleDatePart) - 1]);
if (!$this->validateNumber($num)) {
$this->send("Sorry ".$this->user->getFirstName().", the number of seconds, minutes, or hours I need should be a valid number and should be greater than 0.");
return;
}
if (!$this->validateUnitOfTime($unitOfTime)) {
$this->send("Sorry ".$this->user->getFirstName().", I do not recognize the unit of time you have given me. I only know second(s), minute(s), and hour(s), and their abbreviations.");
return;
}
$dateTime = $this->translateToDateTime($num, $unitOfTime);
}
$delay = strtotime($dateTime) - strtotime(date("Y-m-d H:i:s"));
shell_exec('php /var/www/html/bot/ReminderScript.php '.$this->sender->getSenderId().' '.$this->sender->getAccessToken().' '.$this->user->getFirstName().' '.$delay.' '.urlencode('Remind '.$parameter).' > /dev/null 2>/dev/null &');
$this->send("Got it, ".$this->user->getFirstName()."! I'll remind you when it's time.");
}
}
function validateCommand($parameter) {
return (strpos(strtolower($parameter), ' on ') !== false) || (strpos(strtolower($parameter), ' after ') !== false);
}
function getIsTargetDate($parameter) {
return (strpos(strtolower($parameter), ' on ') !== false) ? true : false;
}
function validateDateTime($dateTime) {
$formattedDateTime = DateTime::createFromFormat('Y-m-d H:i', $dateTime);
$formattedDateTimeWithSec = DateTime::createFromFormat('Y-m-d H:i:s', $dateTime);
return ($formattedDateTime && $formattedDateTime->format('Y-m-d H:i') === (new DateTime($dateTime))->format('Y-m-d H:i')) ||
($formattedDateTimeWithSec && $formattedDateTimeWithSec->format('Y-m-d H:i:s') === (new DateTime($dateTime))->format('Y-m-d H:i:s'));
}
function validateDateTimeIfInFuture($dateTime) {
date_default_timezone_set('Asia/Manila');
$givenDateTime = new DateTime($dateTime);
$currentDateTime = new DateTime();
return $givenDateTime > $currentDateTime;
}
function validateNumber($numberPart) {
return is_numeric($numberPart) && $numberPart > 0;
}
function validateUnitOfTime($unitOfTime) {
switch (strtolower(trim($unitOfTime))) {
case 's':
case 'sec':
case 'secs':
case 'second':
case 'seconds':
case 'min':
case 'mins':
case 'minute':
case 'minutes':
case 'hr':
case 'hrs':
case 'hour':
case 'hours':
return true;
break;
default:
return false;
}
}
function translateToDateTime($num, $unitOfTime) {
date_default_timezone_set('Asia/Manila');
$dateTime = new DateTime(date('Y-m-d H:i:s'));
switch (strtolower(trim($unitOfTime))) {
case 's':
case 'sec':
case 'secs':
case 'second':
case 'seconds':
$dateTime->add(new DateInterval('PT'.$num.'S'));
break;
case 'min':
case 'mins':
case 'minute':
case 'minutes':
$dateTime->add(new DateInterval('PT'.$num.'M'));
break;
case 'hr':
case 'hrs':
case 'hour':
case 'hours':
$dateTime->add(new DateInterval('PT'.$num.'H'));
break;
}
return $dateTime->format('Y-m-d H:i:s');
}
}