iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 8
0
自我挑戰組

用 laravel 尻出自己形狀的 line bot,還要撐三十天!系列 第 8

【第八天】完成當季動畫通知推送!

【第八天】完成當季動畫通知推送!

大家晚上好~
今天不再拖了!就把這個趕快做完吧!
前幾天我們做完了推送通知和簡易爬蟲,今天就把他們參在一起做撒尿牛丸!
https://ithelp.ithome.com.tw/upload/images/20171213/201073809veWmoTGVY.jpg

請服用!

所以我們就直接來囉!

php artisan make:command PushAnimationNotification
<?php

namespace App\Console\Commands;

use App\Services\CrawlerService;
use App\Services\LineBotService;
use Illuminate\Console\Command;

class PushAnimationNotification extends Command
{
    (...略)
    private $path;

    /** @var CrawlerService  */
    private $crawlerService;

    /** @var LineBotService  */
    private $lineBotService;

    /**
     * PushAnimationNotification constructor.
     * @param CrawlerService $crawlerService
     * @param LineBotService $lineBotService
     */
    public function __construct(CrawlerService $crawlerService, LineBotService $lineBotService)
    {
        parent::__construct();
        $this->path = config('services.url.baHa');
        $this->crawlerService = $crawlerService;
        $this->lineBotService = $lineBotService;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $originalData = $this->crawlerService->getOriginalData($this->path);
        $list = $this->crawlerService->getNewAnimationFromBaHa($originalData);

        $today = today()->format('m/d');
        $existedList = [];
        $list = array_map(function ($d) use ($today, &$existedList) {
            if (false === strpos($d['date'], $today) ||
                in_array($d['label'], $existedList, true)
            ) {
                return null;
            }
            $existedList[] = $d['label'];

            if (mb_strlen($d['label'], 'UTF-8') > 12) {
                $d['label'] = mb_substr($d['label'], 0, 9, 'UTF-8') . '...';
            }
            return $d;
        }, $list);

        $target = array_filter($list, function ($d) {
            return null !== $d;
        });

        $message = "{$today} 最新動畫來囉!";
        $messageBuilders = $this->lineBotService->buildTemplateMessageBuilder($target, $message);

        foreach ($messageBuilders as $target) {
            $this->lineBotService->pushMessage($target);
        }

        echo "Good luck!\n";
    }
}

我們可以很快速的稍微調整一下爬取的資訊,接著就直接推送訊息!
但要怎樣加入排程在Heroku呢?

很簡單,來看圖說故事囉~
https://ithelp.ithome.com.tw/upload/images/20171213/20107380N5j3Y5KK6G.png
按一按之後,會到下圖!
https://ithelp.ithome.com.tw/upload/images/20171213/20107380zjZnItr4vv.png

沒有錯!就直接加入排程進去即可!最短可以設定為每十分鐘呼叫一次!

php artisan schedule:push-baha-animation

原本以為會需要設定Scheduler,但看來都不用做這些了,透過Heroku的WEB介面就可以達成了!
現在大家可以在收到最新的動漫後去洗彈幕啦~
https://ithelp.ithome.com.tw/upload/images/20171213/20107380EkLNOeTwik.png

原本的排程資訊有些不夠使用,有興趣可以去看一下連結,在推送通知上有做些小調整!
今天沒什麼重點,就專心實作吧~


上一篇
【第七天】支線任務:XPath
下一篇
【第九天】第一次爬漫畫就上手
系列文
用 laravel 尻出自己形狀的 line bot,還要撐三十天!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言