mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 17:31:49 +08:00 
			
		
		
		
	update: telegram bot modularization
This commit is contained in:
		
							
								
								
									
										38
									
								
								app/Plugins/Telegram/Commands/Bind.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								app/Plugins/Telegram/Commands/Bind.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Models\User; | ||||
| use App\Plugins\Telegram\Telegram; | ||||
|  | ||||
| class Bind extends Telegram { | ||||
|     public $command = '/bind'; | ||||
|     public $description = '将Telegram账号绑定到网站'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         if (!$message->is_private) return; | ||||
|         if (!isset($message->args[0])) { | ||||
|             abort(500, '参数有误,请携带订阅地址发送'); | ||||
|         } | ||||
|         $subscribeUrl = $message->args[0]; | ||||
|         $subscribeUrl = parse_url($subscribeUrl); | ||||
|         parse_str($subscribeUrl['query'], $query); | ||||
|         $token = $query['token']; | ||||
|         if (!$token) { | ||||
|             abort(500, '订阅地址无效'); | ||||
|         } | ||||
|         $user = User::where('token', $token)->first(); | ||||
|         if (!$user) { | ||||
|             abort(500, '用户不存在'); | ||||
|         } | ||||
|         if ($user->telegram_id) { | ||||
|             abort(500, '该账号已经绑定了Telegram账号'); | ||||
|         } | ||||
|         $user->telegram_id = $message->chat_id; | ||||
|         if (!$user->save()) { | ||||
|             abort(500, '设置失败'); | ||||
|         } | ||||
|         $telegramService = $this->telegramService; | ||||
|         $telegramService->sendMessage($message->chat_id, '绑定成功'); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										21
									
								
								app/Plugins/Telegram/Commands/GetLatestUrl.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								app/Plugins/Telegram/Commands/GetLatestUrl.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Models\User; | ||||
| use App\Plugins\Telegram\Telegram; | ||||
|  | ||||
| class GetLatestUrl extends Telegram { | ||||
|     public $command = '/getlatesturl'; | ||||
|     public $description = '将Telegram账号绑定到网站'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         $telegramService = $this->telegramService; | ||||
|         $text = sprintf( | ||||
|             "%s的最新网址是:%s", | ||||
|             config('v2board.app_name', 'V2Board'), | ||||
|             config('v2board.app_url') | ||||
|         ); | ||||
|         $telegramService->sendMessage($message->chat_id, $text, 'markdown'); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										22
									
								
								app/Plugins/Telegram/Commands/Help.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/Plugins/Telegram/Commands/Help.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Plugins\Telegram\Telegram; | ||||
|  | ||||
| class Help extends Telegram { | ||||
|     public $command = '/help'; | ||||
|     public $description = '获取帮助'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         if (!$message->is_private) return; | ||||
|         $commands = [ | ||||
|             '/bind 订阅地址 - 绑定你的' . config('v2board.app_name', 'V2Board') . '账号', | ||||
|             '/traffic - 查询流量信息', | ||||
|             '/getlatesturl - 获取最新的' . config('v2board.app_name', 'V2Board') . '网址', | ||||
|             '/unbind - 解除绑定' | ||||
|         ]; | ||||
|         $text = implode(PHP_EOL, $commands); | ||||
|         $this->telegramService->sendMessage($message->chat_id, "你可以使用以下命令进行操作:\n\n$text", 'markdown'); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										38
									
								
								app/Plugins/Telegram/Commands/ReplyTicket.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								app/Plugins/Telegram/Commands/ReplyTicket.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Models\User; | ||||
| use App\Plugins\Telegram\Telegram; | ||||
| use App\Services\TicketService; | ||||
|  | ||||
| class ReplyTicket extends Telegram { | ||||
|     public $regex = '/[#](.*)/'; | ||||
|     public $description = '获取帮助'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         if (!$message->is_private) return; | ||||
|         $this->replayTicket($message, $match[1]); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     private function replayTicket($msg, $ticketId) | ||||
|     { | ||||
|         $user = User::where('telegram_id', $msg->chat_id)->first(); | ||||
|         if (!$user) { | ||||
|             abort(500, '用户不存在'); | ||||
|         } | ||||
|         $ticketService = new TicketService(); | ||||
|         if (!$msg->text) return; | ||||
|         if ($user->is_admin || $user->is_staff) { | ||||
|             $ticketService->replyByAdmin( | ||||
|                 $ticketId, | ||||
|                 $msg->text, | ||||
|                 $user->id | ||||
|             ); | ||||
|         } | ||||
|         $telegramService = $this->telegramService; | ||||
|         $telegramService->sendMessage($msg->chat_id, "#`{$ticketId}` 的工单已回复成功", 'markdown'); | ||||
|         $telegramService->sendMessageWithAdmin("#`{$ticketId}` 的工单已由 {$user->email} 进行回复", true); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										30
									
								
								app/Plugins/Telegram/Commands/Traffic.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								app/Plugins/Telegram/Commands/Traffic.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Models\User; | ||||
| use App\Plugins\Telegram\Telegram; | ||||
| use App\Utils\Helper; | ||||
|  | ||||
| class Traffic extends Telegram { | ||||
|     public $command = '/traffic'; | ||||
|     public $description = '查询流量信息'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         $telegramService = $this->telegramService; | ||||
|         if (!$message->is_private) return; | ||||
|         $user = User::where('telegram_id', $message->chat_id)->first(); | ||||
|         if (!$user) { | ||||
|             $help = new Help(); | ||||
|             $help->handle($message); | ||||
|             $telegramService->sendMessage($message->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown'); | ||||
|             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)); | ||||
|         $text = "🚥流量查询\n———————————————\n计划流量:`{$transferEnable}`\n已用上行:`{$up}`\n已用下行:`{$down}`\n剩余流量:`{$remaining}`"; | ||||
|         $telegramService->sendMessage($message->chat_id, $text, 'markdown'); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										28
									
								
								app/Plugins/Telegram/Commands/UnBind.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								app/Plugins/Telegram/Commands/UnBind.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Plugins\Telegram\Commands; | ||||
|  | ||||
| use App\Models\User; | ||||
| use App\Plugins\Telegram\Telegram; | ||||
|  | ||||
| class UnBind extends Telegram { | ||||
|     public $command = '/unbind'; | ||||
|     public $description = '将Telegram账号从网站解绑'; | ||||
|  | ||||
|     public function handle($message, $match = []) { | ||||
|         if (!$message->is_private) return; | ||||
|         $user = User::where('telegram_id', $message->chat_id)->first(); | ||||
|         $telegramService = $this->telegramService; | ||||
|         if (!$user) { | ||||
|             $help = new Help(); | ||||
|             $help->handle($message); | ||||
|             $telegramService->sendMessage($message->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown'); | ||||
|             return; | ||||
|         } | ||||
|         $user->telegram_id = NULL; | ||||
|         if (!$user->save()) { | ||||
|             abort(500, '解绑失败'); | ||||
|         } | ||||
|         $telegramService->sendMessage($message->chat_id, '解绑成功', 'markdown'); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user