MqPr.php 3.7 KB

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