WebHookArticleNew.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if(!empty($row['title'])){
  50. $title = str_replace("\n","",$row['title']);
  51. }else{
  52. $title = $row['toc'];
  53. }
  54. $link = env('APP_URL',"http://127.0.0.1:8000")."/app/article/index.php?view=chapter&book={$book}&par={$para}&channel={$channel_id}";
  55. $message .= "1. [{$title}]({$link})\n";
  56. }
  57. $link = env('APP_URL',"http://127.0.0.1:8000")."/app/palicanon";
  58. $message .= "\n [更多]({$link})";
  59. $this->info($message);
  60. $url = $this->argument('host');
  61. switch ($this->argument('type')) {
  62. case "dingtalk":
  63. $param = [
  64. "markdown"=> [
  65. "title"=> $title,
  66. "text"=> $message,
  67. ],
  68. "msgtype"=>"markdown"
  69. ];
  70. break;
  71. case "wechat":
  72. $param = [
  73. "msgtype"=>"markdown",
  74. "markdown"=> [
  75. "content"=> $message,
  76. ],
  77. ];
  78. break;
  79. }
  80. $response = Http::post($url, $param);
  81. }else{
  82. $this->error("章节数据获取错误");
  83. }
  84. return 0;
  85. }
  86. }