├── README.md └── door_chatbot.php /README.md: -------------------------------------------------------------------------------- 1 | # Doorman script will send a chat message to a rocket chat channel when a door was opened 2 | by [Christian Haschek](https://blog.haschek.at) 3 | 4 | ![The result](https://www.pictshare.net/a3939806c5.jpg) 5 | 6 | ## How to install and use in 4 small steps 7 | 8 | Checkout my post: https://blog.haschek.at/2016/my-door-sends-me-chat-messages.html -------------------------------------------------------------------------------- /door_chatbot.php: -------------------------------------------------------------------------------- 1 | BOT_PASSWORD, 'user' => BOT_USERNAME)); 52 | $token = $login['data']['authToken']; 53 | $user = $login['data']['userId']; 54 | 55 | //join room 56 | makeRequest(ROCKETCHAT_URL.'/api/rooms/'.BOT_CHANNEL.'/join',array(),array('X-Auth-Token: '.$token,'X-User-Id: '.$user)); 57 | 58 | //send message 59 | makeRequest(ROCKETCHAT_URL.'/api/rooms/'.BOT_CHANNEL.'/send',array('msg'=>$message),array('X-Auth-Token: '.$token,'X-User-Id: '.$user)); 60 | } 61 | 62 | function makeRequest($url,$data,$headers=false,$post=true) 63 | { 64 | $headers[] = 'Content-type: application/x-www-form-urlencoded'; 65 | $options = array( 66 | 'http' => array( 67 | 'header' => $headers, 68 | 'method' => $post?'POST':'GET', 69 | 'content' => http_build_query($data) 70 | ) 71 | ); 72 | $context = stream_context_create($options); 73 | $result = file_get_contents($url, false, $context); 74 | if ($result === FALSE) { /* Handle error */ } 75 | 76 | return json_decode($result,true); 77 | } 78 | 79 | function translateSecondsToNiceString($secs) 80 | { 81 | $units = array( 82 | "year" => 365*24*3600, 83 | "month" => 30*24*3600, 84 | "week" => 7*24*3600, 85 | "day" => 24*3600, 86 | "hour" => 3600, 87 | "minute" => 60, 88 | "second" => 1, 89 | ); 90 | 91 | if ( $secs == 0 ) return "0 seconds"; 92 | $s = ""; 93 | foreach ( $units as $name => $divisor ) { 94 | if ( $quot = intval($secs / $divisor) ) { 95 | $s .= "$quot $name"; 96 | $s .= (abs($quot) > 1 ? "s" : "") . ", "; 97 | $secs -= $quot * $divisor; 98 | } 99 | } 100 | return substr($s, 0, -2); 101 | } --------------------------------------------------------------------------------