2016年12月19日 星期一

使用 Perl 建立 Line API 運用環境

LAB 平台

#cat /etc/redhat-release
CentOS release 6.8 (Final)

#perl -v

This is perl 5, version 24, subversion 0 (v5.24.0) built for x86_64-linux

Copyright 1987-2016, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

參考 SDK


   
LAB 目地的為建立一個自己專用的 line bot webhook URL
並透過 webhook 的機制取得 Line bot 專用的  line id (UID)

接下來才可以用 Push Message 的方式法做 Send Line Message
Reply API 等等運用


Push APIReply API
Push API指的是Bot能夠在任何時間點對用戶傳送訊息的API
Reply API指的則是公司或經營者針對用戶傳來的訊息進行回覆的API




























關於 line bot


在開始之前可先看一下這個小筆記

Line API 使用小筆記
line 已經推出官方版的 API
所以提供這個簡單的小筆記供需要的人參考

先去官網上申請一個 API 的帳號
且建立一個 Developer Trial 的帳號
並確認該帳號為 LINE@Developer


程式開發可參閱

=====================
Push Line Message 可參閱


PHP 為例

Push message API example

============

$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient('<channel access token>');
$bot = new \LINE\LINEBot($httpClient, 'channelSecret' => '<channel secret>');

$textMessageBuilder = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder('hello');
$response = $bot->pushMessage('UID', $textMessageBuilder); "

echo $response->getHTTPStatus() . ' ' . $response->getRawBody(); "

=================

程式就是這麼簡單.


安裝看上面這個 URL 的下方有寫到如何安裝這個 PHP SDK

在寫 API 時會用到的資訊皆在官網上登入的頁面中可取得.
或者是透過 webhook .

其中重要的資訊如下所示: (1) (2) (3)

(1)<channel access token> 官網上登入可取得
(2) <channel secret> 官網上登入可取得
(3) UID
用這支去改
可取得




參考資訊


輸入Line Channel Access Token :

輸入監聽email :

產生 webhook url , 並在 API 官網上輸入亦可取得,透過 E-Mail 本文


或參考本文介紹的用 perl 建立 webhook URL


Line API

Push message


shell

Push message API example
curl -X POST \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer {ENTER_ACCESS_TOKEN}' \
-d '{
"to": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"messages":[
{
"type":"text",
"text":"Hello, world1"
},
{
"type":"text",
"text":"Hello, world2"
}
]
}' api.line.me/v2/bot/message/push









































本文開始 perl 建立 webhook URL

OS 安裝上新版的 perl


Perl Source

Perl compiles on over 100 platforms, if you want to install from a binary instead see the ports page (especially for Windows).

How to install from source

     wget http://www.cpan.org/src/5.0/perl-5.24.0.tar.gz
     tar -xzf perl-5.24.0.tar.gz
     cd perl-5.24.0
     ./Configure -des -Dprefix=$HOME/localperl
     make
     make test
     make install
Read both INSTALL and README.yoursystem in the perl-5.24.0 directory for more detailed information.


# mv + ln –s 的方式取代OS上內建的 perl ,確保系統預設使甪的 perl 5.24

 系統預設的 Perl 版本為 5.10.1

# perl.old -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Copyright 1987-2009, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

透過 上述 方式安裝後 perl 的版本變更為 5.24.0
#perl -v

This is perl 5, version 24, subversion 0 (v5.24.0) built for x86_64-linux

Copyright 1987-2016, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
  

安裝 cpanm


cpanm: 快速輕便的 CPAN 模組安裝程式
Fri, 26 Feb 2010 22:23:23 +0100
cpanm 是個十分快速的小工具,不用任何設定,就可以開始安裝 cpan module。比起完整的 CPAN.pm Shell 而言,它有一點點不足之處,但實測幾次之後發現,那些都是比較無關痛養的部份。

安裝它
cpanm 是個單一的執行檔,只要下載下來放進 $PATH 裡就可以用:

curl -LO http://bit.ly/cpanm
chmod +x cpanm
sudo cp cpanm /usr/local/bin
使用方式
常用的使用形式如下:

cpanm Jifty
cpanm MIYAGAWA/Plack-0.99_05.tar.gz
cpanm http://backpan.perl.org/authors/id/L/LD/LDS/CGI.pm-3.20.tar.gz
cpanm ~/dists/MyCompany-Enterprise-1.00.tar.gz
參數可接模組名稱、帶版號的 Tarball 檔名、Tarball 網址、或是 Tarball 在本機上的路徑。可調整的選項有五個:

-v, --verbose : 輸出完整的資訊
-f, --force : 強制安裝。就算測試過程有錯誤也照樣安裝
-n, --notest : 不跑測試
-S, --sudo : 安裝時以 sudo 指令進行
--installdeps : 僅安裝依存的模組
這就是全部了。無論是新裝機器或是臨時要裝個 CPAN 模組,都十分的好用。



安裝 LINE::Bot::API & PLack ….

#cpanm install PLack & LINE::Bot::API…..


[root@bbb line]# cpanm install Plack
install is up to date. (0.01)
Plack is up to date. (1.0042)
[root@bbb line]#

[root@bbb bin]# cpanm  Plack
Plack is up to date. (1.0042)
[root@bbb bin]#

[root@bbb bin]#  cpanm install LINE::Bot::API
install is up to date. (0.01)
LINE::Bot::API is up to date. (1.01)
[root@bbb bin]#

[root@bbb bin]#   cpanm LINE::Bot::API
LINE::Bot::API is up to date. (1.01)
[root@bbb bin]#


Line WebHook 建立 參考 URL

關於 Plack


NAME

Plack - Perl Superglue for Web frameworks and Web Servers (PSGI toolkit)

DESCRIPTION

Plack is a set of tools for using the PSGI stack. It contains middleware components, a reference server and utilities for Web application frameworks. Plack is like Ruby's Rack or Python's Paste for WSGI.

See PSGI for the PSGI specification and PSGI::FAQ to know what PSGI and Plack are and why we need them.

如何運行 PSGI

.psgi
  plackup --host 127.0.0.1 --port 9090 /path/to/app.psgi

參考 echo.psgi webhook Source Code




全抄就對了.


  echo.psgi 該頁 的下方式有提到使用方式

    $ export CHANNEL_SECRET=YOUR CHANNEL SECRET
    $ export CHANNEL_ACCESS_TOKEN=YOUR CHANNEL ACCESS TOKEN
    $ plackup eg/echo.psgi

#plackup --host 127.0.0.1 --port 9090 echo.psgi  

比照辦理 準沒錯!




在網路上有 google 到有高手談到 ngrok 這個方式.
簡單的說透過它可以達到內網的主機,直接打通 Firewall + DNS URL,
讓外面的人直接進入內網的測試主機.進行相關測試.


# ./ngrok http 9090



設定  line webhook URL


  

Test webHook (echo bot)









































其它運用 Push Message



Push message API example

use LINE::Bot::API;
use LINE::Bot::API::Builder::SendMessage;

my $bot = LINE::Bot::API->new(
    channel_secret       => "<channel secret>",
    channel_access_token => "<channel access token>",
);

my $messages = LINE::Bot::API::Builder::SendMessage->new(
)->add_text(
    text => 'hello',
);
my $res = $bot->push_message("<to>", $messages->build);
unless ($res->is_success) {
    # error handling
    ....
}