-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensuClientTwitter.php
More file actions
223 lines (201 loc) · 7.6 KB
/
SensuClientTwitter.php
File metadata and controls
223 lines (201 loc) · 7.6 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*!
* @file SensuClientTwitter.php
* @author Sensu Development Team
* @date 2018/04/20
* @brief Twitter用Sensuクライアント
*/
require_once __DIR__.'/Config.php';
require_once __DIR__.'/SensuClient.php';
require_once __DIR__.'/RandomString.php';
require __DIR__.'/vendor/autoload.php';
class SensuClientTwitter
{
/*!
* @brief SensuプラットフォームAPIクライアント
*/
private $sensu;
/*!
* @brief Twitter APIクライアント
*/
private $twitter;
/*!
* @brief コンストラクタ
*/
public function __construct()
{
$this->sensu = new \SensuDevelopmentTeam\SensuClient(Config::SENSU_PLATFORM_API_KEY);
$this->twitter = new \mpyw\Cowitter\Client([
Config::TWITTER_API_CONSUMER_KEY,
Config::TWITTER_API_CONSUMER_SECRET,
Config::TWITTER_API_ACCESS_TOKEN,
Config::TWITTER_API_ACCESS_TOKEN_SECRET
]);
}
/*!
* @brief クライアントを実行
*/
public function run()
{
// 自分の情報を取得
$account_info = $this->twitter->get('account/verify_credentials');
// メンションタイムラインをストリーミングAPIより取得
$self = $this;
\mpyw\Co\Co::wait($this->twitter->streamingAsync('user', function ($stream) use ($self, $account_info) {
if (!isset($stream->direct_message)) // メンション
{
// 送信者が設定されていなければ中止
if (!isset($stream->user))
{
return;
}
$sender = $stream->user->id_str;
// 送信者が自分であれば中止
if ($sender === $account_info->id_str)
{
return;
}
// 先頭にメンションがなければ中止
preg_match('/^((\s+|^)@\S+)*\s/', $stream->text, $match);
if (count($match) < 1)
{
return;
}
if (strpos($match[0], '@'.$account_info->screen_name) === false)
{
return;
}
// 先頭のメンションを排除
$command = preg_replace('/^((\s+|^)@\S+)*\s/', '', $stream->text);
// 命令を分解
$command = $self::getCommandFromText($command);
}
else // ダイレクトメッセージ
{
$sender = $stream->direct_message->sender->id_str;
// 送信者が自分であれば中止
if ($sender === $account_info->id_str)
{
return;
}
// 命令を分解
$command = $self::getCommandFromText($stream->direct_message->text);
}
// 投げ銭コマンド
if (isset($command[0]) && strcasecmp($command[0], 'tip') == 0)
{
if (isset($command[3]))
{
try
{
$command[3] = $self->getUserIdFromScreenName(ltrim($command[3], '@'));
}
catch (Exception $e)
{
$command[3] = '';
}
}
}
// 命令を送信
$result = $self->sensu->command($sender, $command);
if (!isset($stream->direct_message)) // メンション
{
if ($result->status === 'COMMAND_NOT_FOUND')
{
return;
}
// 表示用メッセージが設定されていなければ内部エラー
if (!isset($result->message))
{
$self->twitter->post('statuses/update', [
'status' => '@'.$stream->user->screen_name."\n内部エラーが発生しました。\nAn internal error occurred.\n\n".RandomString::generate(4),
'in_reply_to_status_id' => $stream->id
]);
return;
}
// プッシュメッセージ
if (isset($result->push_message))
{
// 投げ銭コマンド
if (isset($command[0]) && strcasecmp($command[0], 'tip') == 0)
{
$self->twitter->post('statuses/update', [
'status' => '@'.$self->getScreenNameFromUserId($command[3])."\n".sprintf($result->push_message, '@'.$stream->user->screen_name)."\n\n".RandomString::generate(4)
]);
}
}
// 返信
$self->twitter->post('statuses/update', [
'status' => '@'.$stream->user->screen_name."\n\n".$result->message."\n\n".RandomString::generate(4),
'in_reply_to_status_id' => $stream->id
]);
}
else // ダイレクトメッセージ
{
// 表示用メッセージが設定されていなければ内部エラー
if (!isset($result->message))
{
$self->twitter->post('direct_messages/new', [
'user_id' => intval($sender),
'text' => "内部エラーが発生しました。\nAn internal error occurred.\n\n".RandomString::generate(4)
]);
return;
}
// プッシュメッセージ
if (isset($result->push_message))
{
// 投げ銭コマンド
if (isset($command[0]) && strcasecmp($command[0], 'tip') == 0)
{
$self->twitter->post('statuses/update', [
'status' => '@'.$self->getScreenNameFromUserId($command[3])."\n".sprintf($result->push_message, '@'.$stream->direct_message->sender->screen_name)."\n\n".RandomString::generate(4)
]);
}
}
// 返信
$self->twitter->post('direct_messages/new', [
'user_id' => intval($sender),
'text' => $result->message."\n\n".RandomString::generate(4)
]);
}
}));
}
/*
* @brief 表示名からユーザーIDを取得
* @param $screen_name 表示名
* @return ユーザーID
*/
public function getUserIdFromScreenName($screen_name)
{
$user_info = $this->twitter->get('users/show', [
'screen_name' => $screen_name
]);
return $user_info->id_str;
}
/*
* @brief ユーザーIDから表示名を取得
* @param $id ユーザーID
* @return 表示名
*/
public function getScreenNameFromUserId($id)
{
$user_info = $this->twitter->get('users/show', [
'user_id' => $id
]);
return $user_info->screen_name;
}
/*!
* @brief 発言本文より命令を取得
* @param $test 発言本文
* @return 命令
*/
private static function getCommandFromText($text)
{
$command = htmlspecialchars_decode($text, ENT_NOQUOTES);
$result = preg_split('/[ \n](?=(?:[^\\"]*\\"[^\\"]*\\")*(?![^\\"]*\\"))/', $command, -1, PREG_SPLIT_NO_EMPTY);
$result = str_replace('"', '', $result);
return $result;
}
}
$client = new SensuClientTwitter();
$client->run();