iT邦幫忙

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

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

【第十三天】你開過變形金剛嗎?

【第十三天】你開過變形金剛嗎?

我們已經知道slack自己組積木的強大魔力了,既然我們可以組出各式各樣的訊息Template,那在組的過程勢必就不能像之前一樣直接硬組,不然會變得很雜亂!
所以我們又要站在巨人的肩膀上了!

composer require yish/generators

使用前請詳閱說明書

度的!沒錯!就是要用變形金剛 Transformer
(但我比較喜歡環太平洋)
Yes

    php artisan make:transformer Slack/PushAnimationTransformer

我們先以這張圖為概念!
https://ithelp.ithome.com.tw/upload/images/20171218/20107380DYNPiJpgJp.png
沒錯!這是單純輸入網址後會自己拓出來的preview!(可惡啊!為什麼機器人吐出來就沒有!)
算了!這種東西我也組的出來!所以就刻出了這樣的變形金剛!
PushAnimationTransformer.php

<?php

namespace App\Transformers\Slack;

use Yish\Generators\Foundation\Transform\TransformContract;

class PushAnimationTransformer implements TransformContract
{
    
    public function transform($data)
    {
        return [
            //TODO Author
            'author_name' => 'Test',
            'author_link' =>  'https://www.youtube.com/',
            'author_icon' =>  ':chicken01:',
            //TODO Title and link
            'title' => self::class,
            'title_link' => 'https://tai-service.slack.com/home',
            //TODO Info 雖然有點宅但這是堅持
            'text' => "`Testing`  ```This\nshit ``` \n ",
            //TODO Image 這也是種堅持
            'image_url' => 'http://img.94201314.net/comicui/170.jpg',
        ];
    }
}

恩~看來當初的爬蟲要再調整不少了!

好的!完成了!有點臨時拼湊,大概會是將原本的改成這樣!
至於怎麼爬這邊就不贅述了,可以回頭看這裡,雖然頁面不同但做法大同小異~
CrawlerService.php

    /**
     * @param Crawler $crawler
     * @return array
     */
    public function getNewAnimationFromBaHa(Crawler $crawler): array
    {
        $target = $crawler->filterXPath('//div[contains(@class, "newanime")]')
            ->each(function (Crawler $node) {
                $date = $this->getDateForNewAnimationFromBaHa($node);
                $link = $this->getLinkForNewAnimationFromBaHa($node);
                $image = $this->getImageForNewAnimationFromBaHa($node);

                $response = [
                    'date' => array_first($date),
                    'directUri' => array_first($link),
                    'imagePath' => array_first($image),
                ];

                if (null !== $response['directUri']) {
                    $crawler = $this->getOriginalData($response['directUri']);
                    $response = array_merge($response, $this->getAuthorInformationFromBaHa($crawler));
                    $response['label'] = $this->getTitleForNewAnimationFromBaHa($crawler);
                    $response['text'] = $this->getTextForNewAnimationFromBaHa($crawler);
                }

                return in_array(null, array_values($response), true) ? null : $response;
            });
        $target = array_filter($target, function ($d) {
            return null !== $d;
        });
        return $target;
    }
    
    private function getTextForNewAnimationFromBaHa(Crawler $node)
    {
        return $node->filterXPath('//div[@class="data_intro"]/p')->text();
    }

    private function getAuthorInformationFromBaHa(Crawler $node)
    {
        $authorLink = $node->filterXPath('//div[@class="logo"]/a')->attr('href');
        $authorIcon = $node->filterXPath('//div[@class="logo"]/a/img')->attr('src');

        return [
            'authorName' => str_replace('//', '', $authorLink),
            'authorLink' => str_replace('//', 'https://', $authorLink),
            'authorIcon' => $authorIcon,
        ];
    }

    private function getTitleForNewAnimationFromBaHa(Crawler $node)
    {
        return $node->filterXPath('//div[@class="anime_name"]/h1')->text();
    }

那麼現在我們就只差把它組上去囉~
給你的變形金剛塞上火種源

    public function transform($data)
    {
        return [
            'author_name' => $data['authorName'],
            'author_link' =>  $data['authorLink'],
            'author_icon' =>  $data['authorIcon'],
            'title' => $data['label'],
            'title_link' => $data['directUri'],
            'text' => $data['text'],
            'image_url' => $data['imagePath'],
        ];
    }

接著就是把它結合起來啦!
這邊要記得在你的composer.json裡面加上這些
可以參考

    "extra": {
      "component": "package",
      "branch-alias": {
        "dev-master": "1.1.x-dev"
      },
      "laravel": {
        "providers": [
          "Yish\\Generators\\GeneratorsServiceProvider"
        ]
      }
    },

以上詳細操作可以參考這份PR

今天的成果大概是長這樣!這樣就算再用電腦也可以接收到動漫的通知啦哈哈!
https://ithelp.ithome.com.tw/upload/images/20171218/20107380i0vYYjAkoz.png

快被自己宅死了


上一篇
【第十二天】襪!Slack很有趣欸!
下一篇
【第十四天】今天簡單做
系列文
用 laravel 尻出自己形狀的 line bot,還要撐三十天!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言