/
ThatFamily.php
113 lines (89 loc) · 2.55 KB
/
ThatFamily.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
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
<?php
/**
* Created by PhpStorm.
* User: heyqule
* Date: 30/01/16
* Time: 9:40 PM
*/
namespace Slackbot;
require_once "ThatBot.php";
abstract class ThatFamily extends ThatBot {
protected $_wordTriggers = array();
protected $_excludedUsers = array();
public function __construct()
{
parent::__construct();
if(empty($this->_wordTriggers))
{
$this->_canRun = false;
return;
}
$this->_canRun = true;
}
public function run()
{
if(!$this->_canRun)
{
return;
}
$channelId = $this->_getChannel();
$option['oldest'] = $this->_getMessageInterval();
$api = new Api();
//Get the latest 100 messages in the past minute
$data = array_merge($option,array(
'channel' => $channelId,
'inclusive' => 1.
));
$result = $api->getChannelMessages($data);
if(!$result->ok || empty($result->messages))
{
return;
}
//Apply filters
$filteredMessages = $api->filterMessages($result->messages,$this->_wordTriggers,$this->_excludedUsers);
if(count($filteredMessages))
{
$userList = new SlackUserCollection();
$selectedUsers = array();
foreach($filteredMessages as $msg)
{
$member = $userList->getMemberById($msg->user);
$selectedUsers[$member->id] = $userList->getName($member);
}
$userStr = implode(', ',$selectedUsers);
$message = "hey ".$userStr." ".$this->_getMessage();
$data = array(
'channel' => $channelId,
'text' => $message,
'username' => $this->_user,
'parse' => 'full',
'link_names' => 1,
);
if($this->_userIcon)
{
$data['icon_url'] = $this->_userIcon;
}
$this->_postMessage($data);
}
}
protected function _getMessage()
{
$userList = new SlackUserCollection();
$user = $userList->getMemberRandomly();
$userName = $userList->getName($user);
$messages = file(__DIR__.'/'.$this->_listFile);
shuffle($messages);
return str_replace("{username}",$userName,$messages[0]);
}
protected function _getMessageInterval()
{
if(Setting::TEST)
{
return time()-60*60*24;
}
else
{
return time()-60;
}
}
}