MqPr.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Api\Mq;
  5. use App\Models\Sentence;
  6. use App\Models\WebHook;
  7. use App\Models\PaliSentence;
  8. use App\Tools\WebHook as WebHookSend;
  9. use App\Http\Api\MdRender;
  10. class MqPr extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'mq:pr';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'push pr message to mq';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $exchange = 'router';
  41. $queue = 'suggestion';
  42. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  43. Mq::worker($exchange,$queue,function ($message){
  44. /**生成消息内容 */
  45. $msgTitle = '修改建议';
  46. $username = $message->editor->nickName;
  47. $palitext = PaliSentence::where('book',$message->book)
  48. ->where('paragraph',$message->paragraph)
  49. ->where('word_begin',$message->word_start)
  50. ->where('word_end',$message->word_end)
  51. ->value('text');
  52. $prtext = mb_substr($message->content,0,140,"UTF-8");
  53. $sent_num = "{$message->book}-{$message->paragraph}-{$message->word_start}-{$message->word_end}";
  54. $link = "https://next.wikipali.org/pcd/article/para/{$message->book}-{$message->paragraph}";
  55. $link .= "?book={$message->book}&par={$message->paragraph}&channel={$message->channel->id}";
  56. $msgContent = "{$username} 就文句`{$palitext}`提出了修改建议:";
  57. /*
  58. >内容摘要:<font color=\"comment\">{$prtext}</font>,\n
  59. >句子编号:<font color=\"info\">{$sent_num}</font>\n
  60. 欢迎大家[点击链接]({$link})查看并讨论。";
  61. */
  62. $webhooks = WebHook::where('res_id',$message->channel->id)
  63. ->where('status','active')
  64. ->get();
  65. foreach ($webhooks as $key => $hook) {
  66. $event = json_decode($hook->event);
  67. if(!in_array('pr',$event)){
  68. continue;
  69. }
  70. $command = '';
  71. $whSend = new WebHookSend;
  72. switch ($hook->receiver) {
  73. case 'dingtalk':
  74. $ok = $whSend->dingtalk($hook->url,$msgTitle,$msgContent);
  75. break;
  76. case 'wechat':
  77. $ok = $whSend->wechat($hook->url,null,$msgContent);
  78. break;
  79. default:
  80. $ok=2;
  81. break;
  82. }
  83. $this->info("{$command} ok={$ok}");
  84. if($ok===0){
  85. WebHook::where('id',$hook->id)->increment('success');
  86. }else{
  87. WebHook::where('id',$hook->id)->increment('fail');
  88. }
  89. }
  90. });
  91. $callback = function ($msg) {
  92. $message = json_decode($msg->body);
  93. };
  94. $channel->basic_consume('suggestion', '', false, true, false, false, $callback);
  95. while ($channel->is_open()) {
  96. $channel->wait();
  97. }
  98. return 0;
  99. }
  100. }