MqDiscussion.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 MqDiscussion extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'mq:discussion';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  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('discussion', false, true, false, false);
  40. $this->info(" [*] Waiting for wbw-analyses. To exit press CTRL+C");
  41. $callback = function ($msg) {
  42. $message = json_decode($msg->body);
  43. switch ($message->res_type) {
  44. case 'sentence':
  45. $sentence = Sentence::where('uid',$message->res_id)->first();
  46. if(!$sentence){
  47. return 0;
  48. }
  49. /**生成消息内容 */
  50. $msgTitle = $message->editor->nickName;
  51. if($message->parent){
  52. $parentTitle = Discussion::where('id',$message->parent)->value('title');
  53. $msgTitle .= '回复了 '.$parentTitle;
  54. }else{
  55. $msgTitle .= '创建了讨论';
  56. }
  57. $msgContent = '';
  58. if($message->title){
  59. $msgContent = $message->title.'\n\n';
  60. }
  61. if($message->content){
  62. $msgContent .= $message->content;
  63. }
  64. $webhooks = WebHook::where('res_id',$sentence->channel_uid)
  65. ->where('status','active')
  66. ->get();
  67. foreach ($webhooks as $key => $hook) {
  68. $event = json_decode($hook->event);
  69. if(!in_array('discussion',$event)){
  70. continue;
  71. }
  72. $command = '';
  73. switch ($hook->receiver) {
  74. case 'dingtalk':
  75. $command = 'webhook:dingtalk';
  76. break;
  77. case 'wechat':
  78. $command = 'webhook:wechat';
  79. break;
  80. default:
  81. # code...
  82. break;
  83. }
  84. $ok = $this->call($command,['url'=>$hook->url,
  85. 'title'=>$msgTitle,
  86. 'message'=>$msgContent,
  87. ]);
  88. $this->info("{$command} ok={$ok}");
  89. if($ok===0){
  90. WebHook::where('id',$hook->id)->increment('success');
  91. }else{
  92. WebHook::where('id',$hook->id)->increment('fail');
  93. }
  94. }
  95. break;
  96. default:
  97. # code...
  98. break;
  99. }
  100. };
  101. $channel->basic_consume('discussion', '', false, true, false, false, $callback);
  102. while ($channel->is_open()) {
  103. $channel->wait();
  104. }
  105. return 0;
  106. }
  107. }