TelegramProcess.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\User;
  4. use App\Services\Config;
  5. class TelegramProcess
  6. {
  7. private static function needbind_method($bot, $message, $command, $user, $reply_to = null)
  8. {
  9. if ($user != null) {
  10. switch ($command) {
  11. case 'cq':
  12. $bot->sendMessage($message->getChat()->getId(), "您当月流量状况:
  13. 今日已使用 ".$user->TodayusedTraffic()." ".number_format(($user->u+$user->d-$user->last_day_t)/$user->transfer_enable*100, 2)."%
  14. 总共已使用 ".$user->LastusedTraffic()." ".number_format($user->last_day_t/$user->transfer_enable*100, 2)."%
  15. 剩余 ".$user->unusedTraffic()." ".number_format(($user->transfer_enable-($user->u+$user->d))/$user->transfer_enable*100, 2)."%
  16. 当前在线设备数 ".$user->online_ip_count()." 台
  17. ", $parseMode = null, $disablePreview = false, $replyToMessageId = $reply_to);
  18. break;
  19. case 'qd':
  20. if (!$user->isAbleToCheckin()) {
  21. $bot->sendMessage($message->getChat()->getId(), "仟佰星云:您已经签到过了哦", $parseMode = null, $disablePreview = false, $replyToMessageId = $reply_to);
  22. break;
  23. }
  24. $traffic = rand(Config::get('checkinMin'), Config::get('checkinMax'));
  25. $user->transfer_enable = $user->transfer_enable + Tools::toMB($traffic);
  26. $user->last_check_in_time = time();
  27. $user->save();
  28. $bot->sendMessage($message->getChat()->getId(), "走出屏障直达星云之上,只为了与你相遇!你获得了 ".$traffic." MB 流量!", $parseMode = null, $disablePreview = false, $replyToMessageId = $reply_to);
  29. break;
  30. default:
  31. $bot->sendMessage($message->getChat()->getId(), "???", $parseMode = null, $disablePreview = false, $replyToMessageId = $reply_to);
  32. }
  33. } else {
  34. $bot->sendMessage($message->getChat()->getId(), "您未绑定仟佰星云账号。请/help查看绑定步骤", $parseMode = null, $disablePreview = false, $replyToMessageId = $reply_to);
  35. }
  36. }
  37. public static function telegram_process($bot, $message, $command)
  38. {
  39. $bot->sendChatAction($message->getChat()->getId(), 'typing');
  40. $user = User::where('telegram_id', $message->getFrom()->getId())->first();
  41. if ($message->getChat()->getId() > 0) {
  42. //个人
  43. switch ($command) {
  44. case 'ping':
  45. $bot->sendMessage($message->getChat()->getId(), 'Pong!仟佰星云的 群组ID 是 '.$message->getChat()->getId().',这个一般人没啥用!');
  46. break;
  47. case 'tb':
  48. $bot->sendMessage($message->getChat()->getId(),"大爷,不支持淘宝了,请直接网站充值!");
  49. // $bot->sendMessage($message->getChat()->getId(), Tuling::chat($message->getFrom()->getId(), substr($message->getText(), 5)));
  50. break;
  51. case 'cq':
  52. TelegramProcess::needbind_method($bot, $message, $command, $user);
  53. break;
  54. case 'qd':
  55. TelegramProcess::needbind_method($bot, $message, $command, $user, $message->getMessageId());
  56. break;
  57. case 'help':
  58. $help_list = "命令列表:
  59. /ping 获取群组ID
  60. /tb 淘宝购买
  61. /cq 查询流量
  62. /qd 签到获取流量
  63. /help 获取帮助信息
  64. 绑定提示:您可以在网站里点击->资料编辑->滑到页面最下方->Telegram绑定->把二维码拍下来单独发送给TG机器人->绑定成功,机器人扫码登陆,数字登陆、签到等更多精彩功能等着您去发掘。
  65. ";
  66. $bot->sendMessage($message->getChat()->getId(), $help_list);
  67. break;
  68. default:
  69. if ($message->getPhoto() != null) {
  70. $bot->sendMessage($message->getChat()->getId(), "正在解码,请稍候。。。");
  71. $bot->sendChatAction($message->getChat()->getId(), 'typing');
  72. $photos = $message->getPhoto();
  73. $photo_size_array = array();
  74. $photo_id_array = array();
  75. $photo_id_list_array = array();
  76. foreach ($photos as $photo) {
  77. $file = $bot->getFile($photo->getFileId());
  78. $real_id = substr($file->getFileId(), 0, 36);
  79. if (!isset($photo_size_array[$real_id])) {
  80. $photo_size_array[$real_id] = 0;
  81. }
  82. if ($photo_size_array[$real_id] < $file->getFileSize()) {
  83. $photo_size_array[$real_id] = $file->getFileSize();
  84. $photo_id_array[$real_id] = $file->getFileId();
  85. if (!isset($photo_id_list_array[$real_id])) {
  86. $photo_id_list_array[$real_id] = array();
  87. }
  88. array_push($photo_id_list_array[$real_id], $file->getFileId());
  89. }
  90. }
  91. foreach ($photo_id_array as $key => $value) {
  92. $file = $bot->getFile($value);
  93. $qrcode_text = QRcode::decode("https://api.telegram.org/file/bot".Config::get('telegram_token')."/".$file->getFilePath());
  94. if ($qrcode_text == null) {
  95. foreach ($photo_id_list_array[$key] as $fail_key => $fail_value) {
  96. $fail_file = $bot->getFile($fail_value);
  97. $qrcode_text = QRcode::decode("https://api.telegram.org/file/bot".Config::get('telegram_token')."/".$fail_file->getFilePath());
  98. if ($qrcode_text != null) {
  99. break;
  100. }
  101. }
  102. }
  103. if (substr($qrcode_text, 0, 11) == 'mod://bind/' && strlen($qrcode_text) == 27) {
  104. $uid = TelegramSessionManager::verify_bind_session(substr($qrcode_text, 11));
  105. if ($uid != 0) {
  106. $user = User::where('id', $uid)->first();
  107. $user->telegram_id = $message->getFrom()->getId();
  108. $user->im_type = 4;
  109. $user->im_value = $message->getFrom()->getUsername();
  110. $user->save();
  111. $bot->sendMessage($message->getChat()->getId(), "绑定成功。邮箱:".$user->email);
  112. } else {
  113. $bot->sendMessage($message->getChat()->getId(), "绑定失败,二维码无效。".substr($qrcode_text, 11));
  114. }
  115. }
  116. if (substr($qrcode_text, 0, 12) == 'mod://login/' && strlen($qrcode_text) == 28) {
  117. if ($user != null) {
  118. $uid = TelegramSessionManager::verify_login_session(substr($qrcode_text, 12), $user->id);
  119. if ($uid != 0) {
  120. $bot->sendMessage($message->getChat()->getId(), "登录验证成功。邮箱:".$user->email);
  121. } else {
  122. $bot->sendMessage($message->getChat()->getId(), "登录验证失败,二维码无效。".substr($qrcode_text, 12));
  123. }
  124. } else {
  125. $bot->sendMessage($message->getChat()->getId(), "登录验证失败,您未绑定仟佰星云账号。".substr($qrcode_text, 12));
  126. }
  127. }
  128. break;
  129. }
  130. } else {
  131. if (is_numeric($message->getText()) && strlen($message->getText()) == 6) {
  132. if ($user != null) {
  133. $uid = TelegramSessionManager::verify_login_number($message->getText(), $user->id);
  134. if ($uid != 0) {
  135. $bot->sendMessage($message->getChat()->getId(), "登录验证成功。邮箱:".$user->email);
  136. } else {
  137. $bot->sendMessage($message->getChat()->getId(), "登录验证失败,数字无效。");
  138. }
  139. } else {
  140. $bot->sendMessage($message->getChat()->getId(), "登录验证失败,您未绑定仟佰星云账号。");
  141. }
  142. break;
  143. }
  144. $bot->sendMessage($message->getChat()->getId(), Tuling::chat($message->getFrom()->getId(), $message->getText()));
  145. }
  146. }
  147. } else {
  148. //群组
  149. if (Config::get('telegram_group_quiet') == 'true') {
  150. return;
  151. }
  152. switch ($command) {
  153. case 'ping':
  154. $bot->sendMessage($message->getChat()->getId(), 'Pong!这个群组的 ID 是 '.$message->getChat()->getId().'!', $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  155. break;
  156. case 'tb':
  157. $bot->sendMessage($message->getChat()->getId(),"大爷,不支持淘宝了,请直接捐献!");
  158. // if ($message->getChat()->getId() == Config::get('telegram_chatid')) {
  159. // $bot->sendMessage($message->getChat()->getId(), Tuling::chat($message->getFrom()->getId(), substr($message->getText(), 5)), $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  160. // } else {
  161. // $bot->sendMessage($message->getChat()->getId(), '不约,叔叔我们不约。', $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  162. // }
  163. break;
  164. case 'cq':
  165. TelegramProcess::needbind_method($bot, $message, $command, $user, $message->getMessageId());
  166. break;
  167. case 'qd':
  168. TelegramProcess::needbind_method($bot, $message, $command, $user, $message->getMessageId());
  169. break;
  170. case 'help':
  171. $help_list_group = "命令列表:
  172. /ping 获取群组ID
  173. /tb 淘宝购买
  174. /cq 查询流量
  175. /qd 签到获取流量
  176. /help 获取帮助信息
  177. 绑定提示:您可以在网站里点击->资料编辑->滑到页面最下方->Telegram绑定->把二维码拍下来单独发送给TG机器人->绑定成功,机器人扫码登陆,数字登陆、签到等更多精彩功能等着您去发掘。
  178. ";
  179. $bot->sendMessage($message->getChat()->getId(), $help_list_group, $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  180. break;
  181. default:
  182. if ($message->getText() != null) {
  183. if ($message->getChat()->getId() == Config::get('telegram_chatid')) {
  184. $bot->sendMessage($message->getChat()->getId(), Tuling::chat($message->getFrom()->getId(), $message->getText()), $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  185. } else {
  186. $bot->sendMessage($message->getChat()->getId(), '不约,叔叔我们不约。', $parseMode = null, $disablePreview = false, $replyToMessageId = $message->getMessageId());
  187. }
  188. }
  189. }
  190. }
  191. $bot->sendChatAction($message->getChat()->getId(), '');
  192. }
  193. public static function process()
  194. {
  195. try {
  196. $bot = new \TelegramBot\Api\Client(Config::get('telegram_token'));
  197. // or initialize with botan.io tracker api key
  198. // $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');
  199. $command_list = array("ping", "tb" ,"cq", "help", "qd");
  200. foreach ($command_list as $command) {
  201. $bot->command($command, function ($message) use ($bot, $command) {
  202. TelegramProcess::telegram_process($bot, $message, $command);
  203. });
  204. }
  205. $bot->on($bot->getEvent(function ($message) use ($bot) {
  206. TelegramProcess::telegram_process($bot, $message, '');
  207. }), function () {
  208. return true;
  209. });
  210. $bot->run();
  211. } catch (\TelegramBot\Api\Exception $e) {
  212. $e->getMessage();
  213. }
  214. }
  215. }