WebhookDingtalk.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 WebhookDingtalk extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'webhook:dingtalk {url} {title} {message}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'send webhook message to dingtalk';
  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. $param = ['markdown' => [
  42. 'title' => $this->argument('title'),
  43. 'text' => $this->argument('message'),
  44. ],
  45. 'msgtype' => 'markdown',
  46. ];
  47. $this->info("url={$url}");
  48. try {
  49. $response = Http::post($url, $param);
  50. if ($response->successful()) {
  51. return 0;
  52. } else {
  53. return 1;
  54. }
  55. } catch (\Exception $e) {
  56. Log::error($e);
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. }