Bind.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Plugins\Telegram\Commands;
  3. use App\Models\User;
  4. use App\Plugins\Telegram\Telegram;
  5. class Bind extends Telegram {
  6. public $command = '/bind';
  7. public $description = '将Telegram账号绑定到网站';
  8. public function handle($message, $match = []) {
  9. if (!$message->is_private) return;
  10. if (!isset($message->args[0])) {
  11. abort(500, '参数有误,请携带订阅地址发送');
  12. }
  13. $subscribeUrl = $message->args[0];
  14. $subscribeUrl = parse_url($subscribeUrl);
  15. parse_str($subscribeUrl['query'], $query);
  16. $token = $query['token'];
  17. if (!$token) {
  18. abort(500, '订阅地址无效');
  19. }
  20. $user = User::where('token', $token)->first();
  21. if (!$user) {
  22. abort(500, '用户不存在');
  23. }
  24. if ($user->telegram_id) {
  25. abort(500, '该账号已经绑定了Telegram账号');
  26. }
  27. $user->telegram_id = $message->chat_id;
  28. if (!$user->save()) {
  29. abort(500, '设置失败');
  30. }
  31. $telegramService = $this->telegramService;
  32. $telegramService->sendMessage($message->chat_id, '绑定成功');
  33. }
  34. }