WebhookWechat.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Tools\Tools;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Http;
  6. use Illuminate\Support\Facades\Log;
  7. class WebhookWechat extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'webhook:wechat {url} {title} {message}';
  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. if (Tools::isStop()) {
  38. return 0;
  39. }
  40. $url = $this->argument('url');
  41. $title = $this->argument('title');
  42. $content = $this->argument('message');
  43. $strMessage = "# {$title}\n\n$content";
  44. $param = [
  45. 'msgtype' => 'markdown',
  46. 'markdown' => [
  47. 'content' => $strMessage,
  48. ],
  49. ];
  50. $this->info("url={$url}");
  51. try {
  52. $response = Http::post($url, $param);
  53. if ($response->successful()) {
  54. return 0;
  55. } else {
  56. return 1;
  57. }
  58. } catch (\Exception $e) {
  59. Log::error($e);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. }