-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnorris.class.php
70 lines (54 loc) · 2.11 KB
/
norris.class.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
<?php
/*
* simple class to post random fake facts from Chuck Norris to Slack channels
* uses a predefined slack bot to handle publishing on Slack
* needs php curl
*
* author: Henrik Algmark (GitHub @henkealg)
*
*/
class norris {
private $slackToken = '';
var $slackChannel = '#general'; // always prefix channels with #
var $execLog = false; // log the results of slack posts to a local log. logfile must have write permissions
var $factsStore = 'facts.txt';
var $slackEndpoint = 'https://slack.com/api/chat.postMessage';
var $userName = 'Chuck Norris';
var $userIconUrl = 'https://s3-us-west-2.amazonaws.com/slack-files2/avatars/2015-06-03/5203069031_bc99a616508794628cf4_48.jpg';
// =============================================
public function postChuckFact()
{
// get a random chuck fact
$facts = file( $this->factsStore );
$theFact = rand(0, (count($facts)-1));
// now post the amazing, important fact to the selected Slack #channel
$pl = array('token' => $this->slackToken,
'username' => $this->userName,
'icon_url' => $this->userIconUrl,
'channel' => $this->slackChannel,
'text' => $facts[$theFact] );
$post = $this->curlPost($pl);
// log the response from Slack if set
if($this->execLog) $this->execLog($post);
}
private function curlPost($payload=array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->slackEndpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
return $response;
}
private function execLog($event, $filename='exec')
{
// open
$logfile = @fopen($filename . '.log', "a+");
// write log
@fwrite($logfile, date("Y-m-d H:i:s") . ' | ' . $event . "\n");
// close file
@fclose($logfile);
}
}