WebHookArticleNew.php 2.8 KB

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