SendTgMessage.php.bak 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Telegram\Bot\Api;
  6. use App\Models\Telegram\TelegramBotAd;
  7. use App\Models\Transit\TransitWalletCoin;
  8. class SendTgMessage extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'SendTgMessage:notice';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '定时tg消息';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $time = now();
  39. $data = TelegramBotAd::from('telegram_bot_ad as a')
  40. ->Join('telegram_bot as b','a.bot_rid','b.rid')
  41. ->where('a.status',0)
  42. ->select('a.rid','b.bot_token','a.last_notice_time','a.notice_cycle','a.notice_obj','a.notice_ad','a.bot_rid','a.notice_photo')
  43. ->get();
  44. if($data->count() > 0){
  45. foreach ($data as $k => $v) {
  46. //通知周期:1每分钟,2每10分钟,3每30分钟,4每小时,5每天
  47. $notice_cycle = $v->notice_cycle;
  48. $send = 'N';
  49. //每分钟,且上次通知已过了一分钟
  50. if($notice_cycle == 1 && strtotime($v->last_notice_time) + 60 <= strtotime($time)){
  51. $send = 'Y';
  52. //每10分钟
  53. }elseif($notice_cycle == 2 && strtotime($v->last_notice_time) + 600 <= strtotime($time)){
  54. $send = 'Y';
  55. //每30分钟
  56. }elseif($notice_cycle == 3 && strtotime($v->last_notice_time) + 1800 <= strtotime($time)){
  57. $send = 'Y';
  58. //每小时
  59. }elseif($notice_cycle == 4 && strtotime($v->last_notice_time) + 3600 <= strtotime($time)){
  60. $send = 'Y';
  61. //每天
  62. }elseif($notice_cycle == 5 && strtotime($v->last_notice_time) + 86400 <= strtotime($time)){
  63. $send = 'Y';
  64. }
  65. //添加联系老板
  66. //内联按钮
  67. $keyboard = [
  68. 'inline_keyboard' => [
  69. [
  70. ['text' => '联系老板', 'callback_data' => '客服']
  71. ]
  72. ]
  73. ];
  74. $encodedKeyboard = json_encode($keyboard);
  75. #过了周期
  76. if($send == 'Y'){
  77. $save_data = [];
  78. $save_data['last_notice_time'] = $time;
  79. TelegramBotAd::where('rid',$v->rid)->update($save_data);
  80. $bot_token = $v->bot_token;
  81. $notice_obj = $v->notice_obj; #多个逗号隔开
  82. $telegram = new Api($bot_token);
  83. //定时通知的消息
  84. $replytext = $v->notice_ad;
  85. $replyphoto = $v->notice_photo;
  86. if (strpos($replytext, 'trxusdtrate') !== false || strpos($replytext, 'trxusdtwallet') !== false) {
  87. //替换变量
  88. $walletcoin = TransitWalletCoin::from('transit_wallet_coin as a')
  89. ->join('transit_wallet as b','a.transit_wallet_id','b.rid')
  90. ->where('b.bot_rid', $v->bot_rid)
  91. ->where('in_coin_name','usdt')
  92. ->where('out_coin_name','trx')
  93. ->select('a.exchange_rate','b.receive_wallet')
  94. ->first();
  95. if(!empty($walletcoin)){
  96. $paraData = [
  97. 'trxusdtrate' => $walletcoin->exchange_rate,
  98. 'trxusdtwallet' => $walletcoin->receive_wallet
  99. ];
  100. //检查参数是否匹配
  101. preg_match_all('/\${.*?}/', $replytext, $matches);
  102. $params = $matches[0];
  103. $values = [];
  104. foreach ($params as $param) {
  105. $key = str_replace(['${', '}'], '', $param);
  106. $values[$param] = $paraData[$key];
  107. }
  108. $replytext = strtr($replytext, $values);
  109. //替换结束
  110. }
  111. }
  112. $receivelist = explode(',',$notice_obj);
  113. foreach ($receivelist as $x => $y) {
  114. if(!empty($replyphoto)){
  115. $response = $telegram->sendPhoto([
  116. 'chat_id' => $y,
  117. 'photo' => $replyphoto,
  118. 'caption' => $replytext,
  119. 'parse_mode' => 'HTML',
  120. 'reply_markup' => $encodedKeyboard
  121. ]);
  122. }else{
  123. $response = $telegram->sendMessage([
  124. 'chat_id' => $y,
  125. 'text' => $replytext,
  126. 'parse_mode' => 'HTML',
  127. 'reply_markup' => $encodedKeyboard
  128. ]);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }