MqDiscussion.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $webhook = WebHook::where('res_id',$sentence->channel_uid)
  50. ->where('status','active')
  51. ->first();
  52. if(!$webhook){
  53. return 0;
  54. }
  55. $event = json_decode($webhook->event);
  56. if(!in_array('discussion',$event)){
  57. return 0;
  58. }
  59. switch ($webhook->receiver) {
  60. case 'dingtalk':
  61. $ok = $this->call('webhook:dingtalk',['url'=>$webhook->url,
  62. 'title'=>'讨论',
  63. 'message'=>'句子:添加新的讨论',
  64. ]);
  65. $this->info("Received ok=".$ok);
  66. break;
  67. default:
  68. # code...
  69. break;
  70. }
  71. break;
  72. default:
  73. # code...
  74. break;
  75. }
  76. };
  77. $channel->basic_consume('discussion', '', false, true, false, false, $callback);
  78. while ($channel->is_open()) {
  79. $channel->wait();
  80. }
  81. return 0;
  82. }
  83. }