TgController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: solu
  5. * Date: 2019/3/26
  6. * Time: 4:59 PM
  7. */
  8. class TgController extends BaseController {
  9. protected $ajaxLoginActions = [
  10. 'sync',
  11. 'doSync',
  12. ];
  13. /**
  14. * Webhook
  15. * @author solu
  16. * @param $args
  17. */
  18. public function actionWebhook($args) {
  19. $content = file_get_contents("php://input");
  20. $update = json_decode($content, true);
  21. // file_put_contents('/tmp/telegram_message.log', var_export($update, true), FILE_APPEND);
  22. if (isset($update["message"])) {
  23. try {
  24. Telegram::processMessage($update["message"]);
  25. } catch (Exception $e) {
  26. Response::error(CODE_NORMAL_ERROR, $e->getMessage());
  27. }
  28. }
  29. }
  30. /**
  31. * 同步确认页面
  32. * @author solu
  33. * @param $args
  34. * @return array
  35. */
  36. public function actionSync($args) {
  37. $rules = [
  38. 'admin_id' => ['int', 'desc' => 'Telegram管理员id'],
  39. 'group_id' => ['int', 'desc' => 'Telegram群id'],
  40. 'auth_date' => ['int', 'desc' => '校验时间'],
  41. 'hash' => ['string', 'desc' => '校验码'],
  42. ];
  43. Param::checkParam2($rules, $args);
  44. try {
  45. $args = ThirdApi::checkTelegramAuthorization($args);
  46. } catch (Exception $e) {
  47. Response::error(CODE_SIGN_ERROR, $e->getMessage());
  48. }
  49. $adminId = intval($args['admin_id']);
  50. $tgGroupId = intval($args['group_id']);
  51. $tgGroupInfo = ThirdApi::getTelegramChatInfo($tgGroupId);
  52. $tgGroupTitle = $tgGroupInfo['title'] ?: '';
  53. $userId = (new UserBindInfo())->getUserIdByTgId($args['admin_id']);
  54. $meeChatId = User::getUserId();
  55. // 判断是不是管理员自己
  56. if ($userId !== 0 && $userId != $meeChatId) {
  57. Response::error(CODE_NO_PERMITION, 'no permission');
  58. }
  59. $objGroup = new GroupInfo();
  60. // 已经绑定
  61. $alreadySync = $objGroup->objTable->getCount(['tg_group_id' => $tgGroupId]);
  62. // 未绑定的群
  63. $groupIds = (new UserGroup())->objTable->getCol(['user_id' => $meeChatId, 'is_admin' => 1, 'state' => UserGroup::STATE_IN_GROUP], ['_field' => 'group_id']);
  64. $groups = $objGroup->objTable->getAll(['group_id' => $groupIds, 'tg_group_id' => 0], [
  65. '_field' => 'group_id, group_title, group_name',
  66. '_sortKey' => 'group_id DESC',
  67. ]);
  68. return compact('alreadySync', 'adminId', 'tgGroupId', 'tgGroupTitle', 'userId', 'groups');
  69. }
  70. /**
  71. * 绑定操作
  72. * @author solu
  73. * @param $args
  74. * @throws DB_Exception
  75. */
  76. public function actionDoSync($args) {
  77. $rules = [
  78. 'group_id' => ['int', 'desc' => 'MeeChat群id'],
  79. 'tg_group_id' => ['int', 'desc' => 'Telegram群id'],
  80. ];
  81. Param::checkParam2($rules, $args);
  82. $userId = User::getUserId();
  83. $tgGroupId = intval($args['tg_group_id']);
  84. $objGroup = new GroupInfo();
  85. if (!(new UserGroup())->isAdmin($args['group_id'], $userId)) {
  86. Response::error(CODE_NO_PERMITION, 'no permission');
  87. }
  88. if ($objGroup->objTable->getCount(['tg_group_id' => $tgGroupId])) {
  89. Response::error(CODE_NORMAL_ERROR, 'already sync other group');
  90. }
  91. $group = $objGroup->objTable->getRow(['group_id' => $args['group_id']]);
  92. try {
  93. $objGroup->joinGroup(TG_BOT_ID, $args['group_id'], $group);
  94. } catch (Exception $e) {
  95. Response::error($e->getCode(), $e->getMessage());
  96. }
  97. Telegram::setGroupByTG($tgGroupId, $args['group_id']);
  98. $objGroup->objTable->updateObject(['tg_group_id' => $tgGroupId], ['group_id' => $args['group_id']]);
  99. // 绑定成功提示
  100. $url = "https://{$_SERVER['HTTP_HOST']}/s/{$group['group_name']}";
  101. $text = "该群已成功关联并同步MeeChat群组 \"{$url}\"
  102. Group already synced with \"{$url}\"";
  103. try {
  104. Telegram::apiRequest('sendMessage', [
  105. 'chat_id' => $tgGroupId,
  106. 'text' => $text,
  107. // 'parse_mode' => 'HTML',
  108. ]);
  109. } catch (Exception $e) {
  110. var_log($e->getMessage());
  111. }
  112. }
  113. }