-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc-automator.php
65 lines (51 loc) · 1.9 KB
/
rc-automator.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
<?php
/**
* A simple rocket chat message schedulator.
* @author loviuz
*/
include __DIR__.'/vendor/autoload.php';
include __DIR__.'/config.inc.php';
// Loop through users
foreach( $users as $user => $params ){
echo '[*] Checking messages for '.$user.'... ';
// Checking if a message should be send
$when = 'Y-m-d H:i';
// When we want to send?
$when = str_replace( 'Y', $params['year'] == '*' ? date('Y') : $params['year'], $when );
$when = str_replace( 'm', $params['month'] == '*' ? date('m') : $params['month'], $when );
$when = str_replace( 'd', $params['day'] == '*' ? date('d') : $params['day'], $when );
$when = str_replace( 'H', $params['hour'] == '*' ? date('H') : $params['hour'], $when );
$when = str_replace( 'i', $params['minute'] == '*' ? date('i') : $params['minute'], $when );
// Send at exact moment by day or by day of week
$send = false;
if( date('Y-m-d H:i', strtotime($when)) == date('Y-m-d H:i') && $params['daysOfWeek'] == '*' ){
$send = true;
}
foreach( explode(',', $params['daysOfWeek']) as $dayOfWeek ){
if( date('H:i', strtotime($when)) == date('H:i') && (int)$dayOfWeek == (int)date('N') ){
$send = true;
}
}
if( $send ){
echo 'sending message... ';
$response = \Httpful\Request::post($params['webhook'], json_encode($params['message']))
->expectsJson()
->send();
if( $response->code == 200 ){
if( $response->body->success ){
echo "sent!\n";
} else {
echo $response->error."\n";
}
} else {
echo 'error '.$response->code.'! ';
// Additional details
if( !empty($response->body->error) ){
echo $response->body->error;
}
echo "\n";
}
} else {
echo "no message to send.\n";
}
}