WebHookArticleNew.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. class WebHookArticleNew extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'message:webhookarticlenew {host} {type}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '发送消息到一个服务器机器人';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. # 获取最新文章数据
  36. $url = env('APP_URL',"http://127.0.0.1:8000")."/api/v2/progress?view=chapter&channel_type=translation";
  37. $response = Http::get($url);
  38. if($response->successful()){
  39. $this->info("get data ok");
  40. $data = $response['data']['rows'];
  41. $title = "2022-7-3更新";
  42. $message = "# wikipali:最新更新\n\n";
  43. for ($i=0; $i < 4; $i++) {
  44. # code...
  45. $row = $data[$i];
  46. $book = $row['book'];
  47. $para = $row['para'];
  48. $channel_id = $row['channel_id'];
  49. $title = $row['toc'];
  50. $link = env('APP_URL',"http://127.0.0.1:8000")."/app/article/index.php?view=chapter&book={$book}&par={$para}&channel={$channel_id}";
  51. $message .= "1. [{$title}]({$link})\n";
  52. }
  53. $this->info($message);
  54. $url = $this->argument('host');
  55. switch ($this->argument('type')) {
  56. case "dingtalk":
  57. $param = [
  58. "markdown"=> [
  59. "title"=> $title,
  60. "text"=> $message,
  61. ],
  62. "msgtype"=>"markdown"
  63. ];
  64. break;
  65. case "wechat":
  66. $param = [
  67. "msgtype"=>"markdown",
  68. "markdown"=> [
  69. "content"=> $message,
  70. ],
  71. ];
  72. break;
  73. }
  74. $response = Http::post($url, $param);
  75. }else{
  76. $this->error("章节数据获取错误");
  77. }
  78. return 0;
  79. }
  80. }