WebHookArticleNew.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if(\App\Tools\Tools::isStop()){
  36. return 0;
  37. }
  38. # 获取最新文章数据
  39. $url = config('app.url')."/api/v2/progress?view=chapter&channel_type=translation";
  40. $response = Http::get($url);
  41. if($response->successful()){
  42. $this->info("get data ok");
  43. $data = $response['data']['rows'];
  44. $title = "2022-7-3更新";
  45. $message = "# wikipali:最新更新\n\n";
  46. for ($i=0; $i < 4; $i++) {
  47. # code...
  48. $row = $data[$i];
  49. $book = $row['book'];
  50. $para = $row['para'];
  51. $channel_id = $row['channel_id'];
  52. if(!empty($row['title'])){
  53. $title = str_replace("\n","",$row['title']);
  54. }else{
  55. $title = $row['toc'];
  56. }
  57. $link = config('app.url')."/app/article/index.php?view=chapter&book={$book}&par={$para}&channel={$channel_id}";
  58. $message .= "1. [{$title}]({$link})\n";
  59. }
  60. $link = config('app.url')."/app/palicanon";
  61. $message .= "\n [更多]({$link})";
  62. $this->info($message);
  63. $url = $this->argument('host');
  64. switch ($this->argument('type')) {
  65. case "dingtalk":
  66. $param = [
  67. "markdown"=> [
  68. "title"=> $title,
  69. "text"=> $message,
  70. ],
  71. "msgtype"=>"markdown"
  72. ];
  73. break;
  74. case "wechat":
  75. $param = [
  76. "msgtype"=>"markdown",
  77. "markdown"=> [
  78. "content"=> $message,
  79. ],
  80. ];
  81. break;
  82. }
  83. $response = Http::post($url, $param);
  84. }else{
  85. $this->error("章节数据获取错误");
  86. }
  87. return 0;
  88. }
  89. }