「3步」釋出 Laravel 外掛 (二)

coding01發表於2018-02-21

延續上一篇「12步」製作 Laravel 外掛 (一),今天我們就來完成後續的步驟:寫好的外掛部署到「Packagist」上。

具體只需要以下「3」個步驟:

1. 註冊 Packagist 賬號

What is Packagist? Packagist is the default Composer package repository. It lets you find packages and lets Composer know where to get the code from. You can use Composer to manage your project or libraries' dependencies.

You can find the packagist.org source on GitHub.

摘錄:https://packagist.org/about

由於外掛程式碼選擇託管到 Github 上,所以建議直接使用 Github 賬號登入。

「3步」釋出 Laravel 外掛 (二)

註冊成功後,就可以拿到使用者名稱和 API Token,用於關聯 Github 外掛程式碼。

「3步」釋出 Laravel 外掛 (二)

2. 提交程式碼到 GitHub

新建 Github 專案,最好與外掛同名:

「3步」釋出 Laravel 外掛 (二)

在 Laravel 專案同級路徑下 packages 放著就是我們的外掛程式碼「fanly/msgrobot」

「3步」釋出 Laravel 外掛 (二)

提交程式碼:

-l

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

提交程式碼後,在 Github 專案 setting 中增加「Packagist」服務

「3步」釋出 Laravel 外掛 (二)

這裡就是填入「Packagist」的使用者名稱和 API Token。

「3步」釋出 Laravel 外掛 (二)

3. 釋出外掛

只要在 Github 專案中關聯了Packagist 服務,就可以在 Packagist 後臺「submit」該外掛

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

輸入外掛 git 地址,就可以提交了;

「3步」釋出 Laravel 外掛 (二)

此時,如果沒對程式碼進行 tag 控制,得到外掛是「dev-master」版本的。所以需要對外掛進行版本設定。

在「git push」程式碼後,繼續

「3步」釋出 Laravel 外掛 (二)

這樣我們就釋出了0.0.1版本的外掛了。

測試

有了外掛,我們自然需要測試,看看執行效果了。

我們選擇利用 Lumen 專案試試,安裝外掛

「3步」釋出 Laravel 外掛 (二)

在 bootstrap/app.php 引入註冊外掛 ServiceProvider:

$app->register(Fanly\Msgrobot\FanlyMsgrobotServiceProvider::class);
複製程式碼

我們寫一個 test,試試效果,先建立獨立跳轉 ActionCard 型別訊息,然後給已建立的機器人對應的「access_token」釘釘群推送此訊息

// text
        $text = new Text('hello fanly/msgrobot package');
        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($text)
            ->send();

        // link
        $link = new Link([
            'text' => 'link text',
            'title' => 'link title',
            'picUrl' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg',
            'messageUrl' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
        ]);
        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($link)
            ->send();


        // markdown
        $md = new Markdown([
            'title' => 'link text',
            'text' => "#### 杭州天氣 @156xxxx8827\n > 9度,西北風1級,空氣良89,相對溫度73%\n\n> ![screenshot](http://image.jpg)\n> ###### 10點20分發布 [天氣](http://www.thinkpage.cn/) \n"
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($md)
            ->send();

        // Single ActionCard
        $sac = new SingleActionCard([
            'title' => 'link title',
            'text' => '![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 賈伯斯 20 年前想打造的蘋果咖啡廳 
 Apple Store 的設計正從原來滿滿的科技感走向生活化,而其生活化的走向其實可以追溯到 20 年前蘋果一個建立咖啡館的計劃',
            'hideAvatar' => 0,
            'btnOrientation' => 0,
            'singleTitle' => '閱讀原文',
            'singleURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($sac)
            ->send();

        // More ActionCard
        $btns = [
            new ActionCardBtn([
                'title' => '內容不錯',
                'actionURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
            ]),
            new ActionCardBtn([
                'title' => '不感興趣',
                'actionURL' => 'http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg'
            ])
        ];

        $mac = new MoreActionCard([
            'title' => 'link title',
            'text' => '![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 賈伯斯 20 年前想打造的蘋果咖啡廳 
 Apple Store 的設計正從原來滿滿的科技感走向生活化,而其生活化的走向其實可以追溯到 20 年前蘋果一個建立咖啡館的計劃',
            'hideAvatar' => 0,
            'btnOrientation' => 0,
            'btns' => $btns
        ]);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($mac)
            ->send();

        // FeedCard
        $links = [
            new FeedCardLink([
                'title' => "時代的火車向前開",
                "messageURL" => "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL" => "http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg"
            ]),
            new FeedCardLink([
                'title' => "時代的火車向前開",
                "messageURL" => "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL" => "http://f.hiphotos.baidu.com/image/pic/item/503d269759ee3d6db032f61b48166d224e4ade6e.jpg"
            ])
        ];

        $fc = new FeedCard($links);

        Msgrobot::accessToken('cb36a3c3cab1242b94516d026a02d909f1611ec048d89c93cb3e1132f08b4e')
            ->message($fc)
            ->send();
複製程式碼

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

「3步」釋出 Laravel 外掛 (二)

總結

通過對這兩篇文章的梳理和學習,相信我們自己對如何製作 Laravel 外掛有了比較直觀的瞭解了,至於外掛程式碼的質量和 Laravel 技術的理解的深度,有賴於繼續專研。

--此外掛--更多作為學習使用

最後,今天是大年初六,祝大家今年六六六!!!

參考

1. 微信 SDK for Laravel, 基於 overtrue/wechat:https://github.com/overtrue/laravel-wechat

2. 本文章外掛地址:https://packagist.org/packages/fanly/msgrobot

3. 本文章外掛原始碼:https://github.com/fanly/msgrobot

相關文章