2시간 만에 Qiita와 테브의 트렌드를 LINE에 알리는 프로그램을 써 AWS에 배포한 이야기

소개



출사하여 평소 트렌드 체크의 일과를하고 있다면,
다음 기사가 눈에 띄었습니다.

GitHubActions로 Qiita와 Slack을 연결하여 엔지니어에게 좋은 습관을 만든다.

그러고 보니 자신 이외의 엔지니어가 트렌드 체크하고 있는 것 봐 본 적 없어 🤔

그래서 매일 체크하고 싶은 Qiita 트렌드하테나 북마크 IT 트렌드을 매일 아침 LINE에 통지하는 시스템을 만들었습니다.

LINE 계정을 추가한 사용자에게 이와 같은 알림을 받습니다.


LINE Developer에서 Messaging API 만들기





먼저 숨을 들이마시는 새로운 Messaging API를 만듭니다.
이름과 아이콘은 적절하고 좋습니다.

프로그래밍 시작



그럼 함께 가자.

환경 구축


  • PHP가 좋기 때문에 Laravel 프로젝트를 만듭니다.$ laravel new line-it-trend-news
  • LINE Bot을 PHP로 취급하기 위한 SDK를 설치합니다.$ composer require linecorp/line-bot-sdk
  • 스크래핑하고 싶으므로 DomCrawler를 설치합니다.$ composer require symfony/dom-crawler
  • AWS Lambda에 배포하고 싶으므로 브레프을 설치합니다.$ composer require bref/bref bref/laravel-bridge$ php artisan vendor:publish --tag=serverless-config

  • 네, 이제 환경이 완성되었습니다.

    프로그래밍



    LINE Message API에서 실행되도록 구성 파일을 만듭니다.



    config/linebot.php
    <?php
    return [
        'channel_access_token' => getenv('LINE_CHANNEL_ACCESS_TOKEN', null),
        'channel_secret' => getenv('LINE_CHANNEL_SECRET', null),
    ];
    

    .env
    LINE_CHANNEL_ACCESS_TOKEN=<Messaging APIのチャネルアクセストークンを記述>
    LINE_CHANNEL_SECRET=<Messaging APIのチャネルシークレットを記述>
    

    쿨롱에서 실행하고 싶으므로 명령을 만듭니다.


    $ php artisan make:command SendLineMessageCommand
    내용은 이런 느낌
    정직, 돌관으로 만들고 있으므로 언리 더블입니다.

    app/Console/Commands/SendLineMessageCommand.php
    <?php
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    
    use LINE\LINEBot\HTTPClient\CurlHTTPClient;
    use LINE\LINEBot;
    use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
    use Symfony\Component\DomCrawler\Crawler;
    
    class SendLineMessageCommand extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'cron:send_line_message';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return int
         */
        public function handle()
        {
            $message = sprintf(
                "【Qiitaのトレンド】\n%s\n\n【はてなのトレンド】\n%s",
                $this->createQiitaTrendMessage(),
                $this->createHatenaTrendMessage()
            );
    
            $httpClient = new CurlHTTPClient(config('linebot.channel_access_token'));
            $bot = new LINEBot($httpClient, ['channelSecret' => config('linebot.channel_secret')]);
            $textMessageBuilder = new TextMessageBuilder($message);
            $bot->broadcast($textMessageBuilder);
    
            return 0;
        }
    
        private function createQiitaTrendMessage() {
            $response = \Http::get('https://qiita.com');
    
            $crawler = new Crawler($response->getBody()->getContents());
            $node = $crawler->filter('div[data-hyperapp-app="Trend"]')->eq(0);
            $value = $node->attr('data-hyperapp-props');
    
            $message = collect(json_decode($value, true))
                ->dotc('trend.edges', [])
                ->slice(0, 5)
                ->map(function($row) {
                    $data = collect($row);
                    $subject = $data->dot('node.title');
                    $uuid = $data->dot('node.uuid');
                    $userName = $data->dot('node.author.userName');
                    $url = sprintf('https://qiita.com/%s/items/%s', $userName, $uuid);
    
                    return sprintf('▼ %s%s%s', $subject, "\n", $url);
                })
                ->join("\n\n");
    
            return $message;
        }
    
        private function createHatenaTrendMessage() {
            $response = \Http::get('https://b.hatena.ne.jp/hotentry/it');
    
            $crawler = new Crawler($response->getBody()->getContents());
    
            $list = collect([]);
            $crawler->filter('.entrylist-contents div.entrylist-contents-main')->each(function($node) use($list) {
                $title = $node->filter('h3 a')->eq(0)->attr('title') ?? null;
                $url   = $node->filter('h3 a')->eq(0)->attr('href') ?? null;
                $list->push([$title, $url]);
            });
    
            return $list->slice(0, 5)->map(function($data) {
                list($title, $url) = $data;
                return sprintf('▼ %s%s%s', $title, "\n", $url);
            })
            ->join("\n\n");
        }
    }
    

    app/Console/Kernel.php에 추가하여 지정된 시간에 실행되도록합니다.



    app/Console/Kernel.php
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('cron:send_line_message')->dailyAt('10:00');
        }
    

    serverless.yml에 app/Console/Kernel.php를 실행하는 명령 추가


    events를 추가하면 매분마다 Cloudwatch가 schedule:run 명령을 두드려줍니다.

    serverless.yml
        artisan:
            handler: artisan
            timeout: 120
            layers:
                - ${bref:layer.php-74}
                - ${bref:layer.console}
            # ここを追加
            events:
                - schedule:
                    rate: rate(1 minute)
                    input:
                        cli: schedule:run
    

    배포



    한 명령으로 배포할 수 있습니다.
    프로파일 러, 환경 등은 좋다.
    $ serverless deploy

    출처



    여기에 그대로 공개하고 있습니다.
    기사를 쓸 때와 다를 수 있습니다.
    htps : // 기주 b. 코 m / 유 K1 가메 / ぃ 네 - t t 렌 d - ws

    사이고에게



    · URL을 단축하고 짧게 만들고 싶습니다.
    · 회전식 템플릿을 사용하여 표시하고 싶습니다.

    등 여러가지 하고 싶은 일은 있습니다만,
    즉석으로는 꽤 좋은 느낌이 들지 않았습니까.

    그리고는 이것을 사내의 엔지니어에게 공유하지 않으면…

    좋은 웹페이지 즐겨찾기