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;
|
2020-05-20 15:58:27 +08:00
|
|
|
|
use App\Models\User;
|
2020-05-25 16:39:35 +08:00
|
|
|
|
use App\Utils\Helper;
|
2020-05-17 15:23:39 +08:00
|
|
|
|
|
|
|
|
|
class TelegramController extends Controller
|
|
|
|
|
{
|
2020-05-20 16:11:18 +08:00
|
|
|
|
protected $msg;
|
|
|
|
|
|
2020-05-17 15:23:39 +08:00
|
|
|
|
public function __construct(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
|
|
|
|
|
abort(500, 'authentication failed');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function webhook(Request $request)
|
|
|
|
|
{
|
2020-05-20 16:11:18 +08:00
|
|
|
|
$this->msg = $this->getMessage($request->input());
|
|
|
|
|
if (!$this->msg) return;
|
2020-05-20 15:51:32 +08:00
|
|
|
|
try {
|
2020-05-20 16:11:18 +08:00
|
|
|
|
switch($this->msg->command) {
|
|
|
|
|
case '/bind': $this->bind();
|
2020-05-20 15:51:32 +08:00
|
|
|
|
break;
|
2020-05-25 16:39:35 +08:00
|
|
|
|
case '/traffic': $this->traffic();
|
|
|
|
|
break;
|
2020-06-20 18:08:26 +08:00
|
|
|
|
case '/getlatesturl': $this->getLatestUrl();
|
2020-06-18 16:26:50 +08:00
|
|
|
|
break;
|
2020-05-20 16:11:18 +08:00
|
|
|
|
default: $this->help();
|
2020-05-20 15:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$telegramService = new TelegramService();
|
2020-05-20 16:11:18 +08:00
|
|
|
|
$telegramService->sendMessage($this->msg->chat_id, $e->getMessage());
|
2020-05-20 15:30:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMessage(array $data)
|
|
|
|
|
{
|
2020-05-28 16:10:43 +08:00
|
|
|
|
if (!isset($data['message'])) return false;
|
2020-05-20 15:30:51 +08:00
|
|
|
|
$obj = new \StdClass();
|
|
|
|
|
$obj->is_private = $data['message']['chat']['type'] === 'private' ? true : false;
|
2020-05-28 16:10:43 +08:00
|
|
|
|
if (!isset($data['message']['text'])) return false;
|
2020-05-20 15:30:51 +08:00
|
|
|
|
$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'];
|
2020-05-20 15:30:51 +08:00
|
|
|
|
return $obj;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 16:11:18 +08:00
|
|
|
|
private function bind()
|
2020-05-20 15:30:51 +08:00
|
|
|
|
{
|
2020-05-20 16:11:18 +08:00
|
|
|
|
$msg = $this->msg;
|
2020-05-20 15:30:51 +08:00
|
|
|
|
if (!$msg->is_private) return;
|
2020-05-20 18:33:54 +08:00
|
|
|
|
if (!isset($msg->args[0])) {
|
|
|
|
|
abort(500, '参数有误,请携带订阅地址发送');
|
|
|
|
|
}
|
2020-05-20 15:30:51 +08:00
|
|
|
|
$subscribeUrl = $msg->args[0];
|
|
|
|
|
$subscribeUrl = parse_url($subscribeUrl);
|
2020-05-20 15:57:38 +08:00
|
|
|
|
parse_str($subscribeUrl['query'], $query);
|
|
|
|
|
$token = $query['token'];
|
2020-05-20 15:51:32 +08:00
|
|
|
|
if (!$token) {
|
|
|
|
|
abort(500, '订阅地址无效');
|
|
|
|
|
}
|
|
|
|
|
$user = User::where('token', $token)->first();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
abort(500, '用户不存在');
|
|
|
|
|
}
|
|
|
|
|
$user->telegram_id = $msg->chat_id;
|
|
|
|
|
if (!$user->save()) {
|
|
|
|
|
abort(500, '设置失败');
|
|
|
|
|
}
|
2020-05-20 15:53:23 +08:00
|
|
|
|
$telegramService = new TelegramService();
|
|
|
|
|
$telegramService->sendMessage($msg->chat_id, '绑定成功');
|
2020-05-17 15:23:39 +08:00
|
|
|
|
}
|
2020-05-20 16:11:18 +08:00
|
|
|
|
|
|
|
|
|
private function help()
|
|
|
|
|
{
|
|
|
|
|
$msg = $this->msg;
|
|
|
|
|
if (!$msg->is_private) return;
|
|
|
|
|
$telegramService = new TelegramService();
|
2020-05-20 16:19:29 +08:00
|
|
|
|
$commands = [
|
2020-05-25 16:41:05 +08:00
|
|
|
|
'/bind 订阅地址 - 绑定你的' . config('v2board.app_name', 'V2Board') . '账号',
|
2020-06-18 16:26:50 +08:00
|
|
|
|
'/traffic - 查询流量信息',
|
|
|
|
|
'/getLatestUrl - 获取最新的' . config('v2board.app_name', 'V2Board') . '网址'
|
2020-05-20 16:19:29 +08:00
|
|
|
|
];
|
|
|
|
|
$text = implode(PHP_EOL, $commands);
|
|
|
|
|
$telegramService->sendMessage($msg->chat_id, "你可以使用以下命令进行操作:\n\n$text", 'markdown');
|
2020-05-20 16:11:18 +08:00
|
|
|
|
}
|
2020-05-25 16:39:35 +08:00
|
|
|
|
|
|
|
|
|
private function traffic()
|
|
|
|
|
{
|
|
|
|
|
$msg = $this->msg;
|
|
|
|
|
if (!$msg->is_private) return;
|
|
|
|
|
$user = User::where('telegram_id', $msg->chat_id)->first();
|
2020-05-30 00:09:24 +08:00
|
|
|
|
$telegramService = new TelegramService();
|
2020-05-25 16:39:35 +08:00
|
|
|
|
if (!$user) {
|
|
|
|
|
$this->help();
|
2020-05-30 00:09:24 +08:00
|
|
|
|
$telegramService->sendMessage($msg->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown');
|
2020-05-25 16:39:35 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$transferEnable = Helper::trafficConvert($user->transfer_enable);
|
|
|
|
|
$up = Helper::trafficConvert($user->u);
|
|
|
|
|
$down = Helper::trafficConvert($user->d);
|
|
|
|
|
$remaining = Helper::trafficConvert($user->transfer_enable - ($user->u + $user->d));
|
2020-05-25 16:41:05 +08:00
|
|
|
|
$text = "🚥流量查询\n———————————————\n计划流量:`{$transferEnable}`\n已用上行:`{$up}`\n已用下行:`{$down}`\n剩余流量:`{$remaining}`";
|
2020-05-25 16:39:35 +08:00
|
|
|
|
$telegramService->sendMessage($msg->chat_id, $text, 'markdown');
|
|
|
|
|
}
|
2020-06-18 16:26:50 +08:00
|
|
|
|
|
|
|
|
|
private function getLatestUrl()
|
|
|
|
|
{
|
|
|
|
|
$msg = $this->msg;
|
|
|
|
|
$user = User::where('telegram_id', $msg->chat_id)->first();
|
|
|
|
|
$telegramService = new TelegramService();
|
|
|
|
|
$text = sprintf(
|
|
|
|
|
"%s的最新网址是:%s",
|
|
|
|
|
config('v2board.app_name', 'V2Board'),
|
|
|
|
|
config('v2board.app_url')
|
|
|
|
|
);
|
|
|
|
|
$telegramService->sendMessage($msg->chat_id, $text, 'markdown');
|
|
|
|
|
}
|
2020-05-17 15:23:39 +08:00
|
|
|
|
}
|