2020-05-17 15:23:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Guest;
|
|
|
|
|
2020-05-20 15:51:32 +08:00
|
|
|
use App\Services\TelegramService;
|
2020-05-17 15:23:39 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
class TelegramController extends Controller
|
|
|
|
{
|
2020-05-20 16:11:18 +08:00
|
|
|
protected $msg;
|
2022-01-27 01:46:26 +08:00
|
|
|
protected $commands = [];
|
2022-07-28 15:07:10 +08:00
|
|
|
protected $telegramService;
|
2020-05-20 16:11:18 +08:00
|
|
|
|
2020-05-17 15:23:39 +08:00
|
|
|
public function __construct(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
|
2022-01-27 01:46:26 +08:00
|
|
|
abort(401);
|
2020-05-17 15:23:39 +08:00
|
|
|
}
|
2022-07-28 15:07:10 +08:00
|
|
|
|
|
|
|
$this->telegramService = new TelegramService();
|
2020-05-17 15:23:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function webhook(Request $request)
|
|
|
|
{
|
2022-07-28 15:07:10 +08:00
|
|
|
$this->formatMessage($request->input());
|
|
|
|
$this->formatChatJoinRequest($request->input());
|
2022-01-27 01:46:26 +08:00
|
|
|
$this->handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-07-28 15:07:10 +08:00
|
|
|
if (!$this->msg) return;
|
2022-01-27 01:46:26 +08:00
|
|
|
$msg = $this->msg;
|
2022-02-17 20:58:47 +08:00
|
|
|
$commandName = explode('@', $msg->command);
|
|
|
|
|
|
|
|
// To reduce request, only commands contains @ will get the bot name
|
|
|
|
if (count($commandName) == 2) {
|
|
|
|
$botName = $this->getBotName();
|
|
|
|
if ($commandName[1] === $botName){
|
|
|
|
$msg->command = $commandName[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:51:32 +08:00
|
|
|
try {
|
2022-01-27 01:46:26 +08:00
|
|
|
foreach (glob(base_path('app//Plugins//Telegram//Commands') . '/*.php') as $file) {
|
|
|
|
$command = basename($file, '.php');
|
|
|
|
$class = '\\App\\Plugins\\Telegram\\Commands\\' . $command;
|
|
|
|
if (!class_exists($class)) continue;
|
|
|
|
$instance = new $class();
|
|
|
|
if ($msg->message_type === 'message') {
|
|
|
|
if (!isset($instance->command)) continue;
|
|
|
|
if ($msg->command !== $instance->command) continue;
|
|
|
|
$instance->handle($msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($msg->message_type === 'reply_message') {
|
|
|
|
if (!isset($instance->regex)) continue;
|
|
|
|
if (!preg_match($instance->regex, $msg->reply_text, $match)) continue;
|
|
|
|
$instance->handle($msg, $match);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-20 15:51:32 +08:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2022-07-28 15:07:10 +08:00
|
|
|
$this->telegramService->sendMessage($msg->chat_id, $e->getMessage());
|
2020-08-03 16:27:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 20:58:47 +08:00
|
|
|
public function getBotName()
|
|
|
|
{
|
2022-07-28 15:07:10 +08:00
|
|
|
$response = $this->telegramService->getMe();
|
2022-02-17 20:58:47 +08:00
|
|
|
return $response->result->username;
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:07:10 +08:00
|
|
|
private function formatMessage(array $data)
|
2020-05-20 15:30:51 +08:00
|
|
|
{
|
2022-07-28 15:07:10 +08:00
|
|
|
if (!isset($data['message'])) return;
|
|
|
|
if (!isset($data['message']['text'])) return;
|
2020-05-20 15:30:51 +08:00
|
|
|
$obj = new \StdClass();
|
|
|
|
$text = explode(' ', $data['message']['text']);
|
2020-05-20 15:53:23 +08:00
|
|
|
$obj->command = $text[0];
|
|
|
|
$obj->args = array_slice($text, 1);
|
|
|
|
$obj->chat_id = $data['message']['chat']['id'];
|
|
|
|
$obj->message_id = $data['message']['message_id'];
|
2022-07-28 15:07:10 +08:00
|
|
|
$obj->message_type = 'message';
|
2020-08-03 16:27:37 +08:00
|
|
|
$obj->text = $data['message']['text'];
|
2022-01-27 01:46:26 +08:00
|
|
|
$obj->is_private = $data['message']['chat']['type'] === 'private';
|
2022-07-28 15:07:10 +08:00
|
|
|
if (isset($data['message']['reply_to_message']['text'])) {
|
|
|
|
$obj->message_type = 'reply_message';
|
2020-08-03 16:27:37 +08:00
|
|
|
$obj->reply_text = $data['message']['reply_to_message']['text'];
|
|
|
|
}
|
2022-07-28 15:07:10 +08:00
|
|
|
$this->msg = $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function formatChatJoinRequest(array $data)
|
|
|
|
{
|
|
|
|
if (!isset($data['chat_join_request'])) return;
|
|
|
|
if (!isset($data['chat_join_request']['from']['id'])) return;
|
|
|
|
if (!isset($data['chat_join_request']['chat']['id'])) return;
|
|
|
|
$user = \App\Models\User::where('telegram_id', $data['chat_join_request']['from']['id'])
|
|
|
|
->first();
|
|
|
|
if (!$user) {
|
|
|
|
$this->telegramService->declineChatJoinRequest(
|
|
|
|
$data['chat_join_request']['chat']['id'],
|
|
|
|
$data['chat_join_request']['from']['id']
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$userService = new \App\Services\UserService();
|
|
|
|
if (!$userService->isAvailable($user)) {
|
|
|
|
$this->telegramService->declineChatJoinRequest(
|
|
|
|
$data['chat_join_request']['chat']['id'],
|
|
|
|
$data['chat_join_request']['from']['id']
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$userService = new \App\Services\UserService();
|
|
|
|
$this->telegramService->approveChatJoinRequest(
|
|
|
|
$data['chat_join_request']['chat']['id'],
|
|
|
|
$data['chat_join_request']['from']['id']
|
|
|
|
);
|
2020-05-20 15:30:51 +08:00
|
|
|
}
|
2020-05-17 15:23:39 +08:00
|
|
|
}
|