-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelegramWebhook.php
179 lines (163 loc) · 5.58 KB
/
TelegramWebhook.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
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
<?php
/*!
* @file TelegramWebhook.php
* @author Sensu Development Team
* @date 2018/02/24
* @brief Telegram用Sensuクライアント
*/
require_once __DIR__.'/Config.php';
require_once __DIR__.'/SensuClient.php';
require __DIR__.'/vendor/autoload.php';
class TelegramWebhook
{
/*!
* @brief SensuプラットフォームAPIクライアント
*/
private $sensu;
/*!
* @brief Telegram APIクライアント
*/
private $telegram;
/*!
* @brief コンストラクタ
*/
public function __construct()
{
$this->sensu = new \SensuDevelopmentTeam\SensuClient(Config::SENSU_PLATFORM_API_KEY);
$loop = \React\EventLoop\Factory::create();
$handler = new \unreal4u\TelegramAPI\HttpClientRequestHandler($loop);
$client = new \unreal4u\TelegramAPI\TgLog(Config::TELEGRAM_API_TOKEN, $handler);
$this->telegram = array(
'client' => $client,
'loop' => $loop
);
}
/*!
* @brief フック
*/
public function hook()
{
// 受信したJSONをパース
$request = file_get_contents('php://input');
$request = json_decode($request);
if(!$request)
{
return;
}
// メッセージでなければ中止
if (!isset($request->message))
{
return;
}
// 接頭辞
$prefix = '/';
// 本文の先頭が接頭辞でなければ中止
if (strncmp($request->message->text, $prefix, strlen($prefix)))
{
return;
}
// 送信者の情報を取得
$get_chat_member = new \unreal4u\TelegramAPI\Telegram\Methods\GetChatMember();
$get_chat_member->chat_id = $request->message->chat->id;
$get_chat_member->user_id = $request->message->from->id;
$sender = \Clue\React\Block\await($this->telegram['client']->performApiRequest($get_chat_member), $this->telegram['loop'])->user;
// 接頭辞を削除
$command = substr($request->message->text, strlen($prefix), strlen($request->message->text) - strlen($prefix));
// 命令を分解
$command = $this::getCommandFromText($command);
// 投げ銭コマンド
if (isset($command[0]) && strcasecmp($command[0], 'tip') == 0)
{
if (isset($command[3]))
{
try
{
$command[3] = strval($this::getIdFromUserName($command[3]));
}
catch (Exception $e)
{
$command[3] = '';
}
}
}
// 命令を送信
$result = $this->sensu->command(strval($request->message->from->id), $command);
$sender_mention = '[';
if (!empty($sender->username))
{
$sender_mention .= '@'.$sender->username;
}
else
{
$sender_mention .= $sender->first_name;
}
$sender_mention .= '](tg://user?id=';
$sender_mention .= $request->message->from->id;
$sender_mention .= ')';
// プッシュメッセージ
if (isset($result->push_message))
{
// 投げ銭コマンド
if (isset($command[0]) && strcasecmp($command[0], 'tip') == 0)
{
$send_message_push = new \unreal4u\TelegramAPI\Telegram\Methods\SendMessage();
$send_message_push->chat_id = intval($command[3]);
$send_message_push->text = sprintf($result->push_message, $sender_mention);
$send_message_push->parse_mode = 'Markdown';
\Clue\React\Block\await($this->telegram['client']->performApiRequest($send_message_push), $this->telegram['loop']);
}
}
// 返信
$send_message = new \unreal4u\TelegramAPI\Telegram\Methods\SendMessage();
$send_message->chat_id = $request->message->chat->id;
// 表示用メッセージが設定されていなければ内部エラー
if (!isset($result->message))
{
$send_message->text = $sender_mention."\n内部エラーが発生しました。\nAn internal error occurred.";
}
else
{
$send_message->text .= $sender_mention."\n".$result->message;
}
$send_message->parse_mode = 'Markdown';
\Clue\React\Block\await($this->telegram['client']->performApiRequest($send_message), $this->telegram['loop']);
}
/*!
* @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;
}
/*
* @brief ユーザー名からIDを取得
* @param $user_nameユーザー名
* @return ID
*/
public static function getIdFromUserName($user_name)
{
$ch = curl_init('https://api.pwrtelegram.xyz/bot'.Config::TELEGRAM_API_TOKEN.'/getChat?chat_id='.$user_name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
curl_close($ch);
if (!$response->ok)
{
throw new Exception('API error.');
}
return $response->result->id;
}
}
try
{
$webhook = new TelegramWebhook();
$webhook->hook();
}
catch (Exception $e)
{
// Webhook再リクエスト防止の為何もしない
}