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