顯示具有 Perl 標籤的文章。 顯示所有文章
顯示具有 Perl 標籤的文章。 顯示所有文章

2024年11月24日 星期日

使用 Ngrok + Perl 串接 LINE BOT Webhook

 使用 Ngrok + Perl 串接  LINE BOT Webhook


復習一下舊筆記 [使用 Perl 建立 Line API 運用環境]

https://xrcd2.blogspot.com/2016/12/perl-line-api.html

一樣是用 Linux + Ngrok + Perl 組合而來,

只是將 CentOS Linux 6.8 + Perl 5.25 (2016年)

改成 Oracle Linux 9.5 + Perl 5.32 (2024年)   

參考 URL

https://github.com/line/line-bot-sdk-perl

https://github.com/line/line-bot-sdk-perl/blob/master/eg/echo.psgi


筆記如下:


dnf install perl-App-cpanminus.noarch

cpanm install  Plack

cpanm install LINE::Bot::API


修改 echo.psgi  這一支程式,

在 62 行處    $messages->add_text( text => $event->text );

下方插入這行  $messages->add_text( text => $from_id); 


讓 Line BOT Webhook 回傳 User_ID / Group_IP 資訊,可供程式傳送訊息使用.

其 User_ID 在 登入 https://developers.line.biz 後應該也是可以找到它的.

但 Group_ID 應該是僅能透過 Webhook 取出.(印象中)

所以才會有這個筆記的,而這個筆記取得的Group_ID,以後是用來發 Alert 用的.


另外也是要透過 https://ngrok.com 取得 ngrok 這支程式,

用它來將內部主機執行 echo.psgi 產出的  LINE BOT Webhook 服務

轉成一個在 Internet 上的 LINE BOT Webhook 服務.


如果不修改程式,也是可以在  plackup echo.psgi  後看到它. 

看到的資訊如下所示:



[root@node1 tmp]# plackup --host 127.0.0.1 --port 9090  echo.psgi


HTTP::Server::PSGI: Accepting connections at http://127.0.0.1:9090/

message_id=536187989014348097, type=message(U74ea4XXXXXXXXXXXXXXXXXXXXXX866b1), 

reply_token=774fa992d5bb449da5e829402b5b101e, timestamp=1732424250192 at /tmp/line-bot.pl line 50.

127.0.0.1 - - [24/Nov/2024:12:57:31 +0800] "POST /perl/callback HTTP/1.1" 200 2 "-" "LineBotWebhook/2.0"


message_id=536188075936579603, type=message(C39bdbXXXXXXXXXXXXXXXXXXXXXX32dea), 

reply_token=bce83ae760d8480f8823d47c0a2ab5b4, timestamp=1732424302131 at /tmp/line-bot.pl line 50.

127.0.0.1 - - [24/Nov/2024:12:58:22 +0800] "POST /perl/callback HTTP/1.1" 200 2 "-" "LineBotWebhook/2.0"



開二個 SSH Session (1) / (2) 


(1) 執行  echo.psgi

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


(2) 執行 ngrok 

ngrok config add-authtoken 2pHSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1EU7g

ngrok http 9090 


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


ngrok                                                                                                                                                                                                                                                         (Ctrl+C to quit)

                                                                                                                                                                                                                                                                              

Sign up to try new private endpoints https://ngrok.com/new-features-update?ref=private                                                                                                                                                                                        

                                                                                                                                                                                                                                                                              

Session Status                online                                                                                                                                                                                                                                        

Account                       XXXXXXXXXXX@gmail.com (Plan: Free)                                                                                                                                                                                                             

Version                       3.18.4                                                                                                                                                                                                                                          

Region                        Japan (jp)                                                                                                                                                                                                                                      

Latency                       39ms                                                                                                                                                                                                                                            

Web Interface                 http://127.0.0.1:4040                                                                                                                                                                                                                           

Forwarding                    https://992c-XXX-XXX-XXX-XXX.ngrok-free.app -> http://localhost:9090                                                                                                                                                                             

                                                                                                                                                                                                                                                                              

Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                                                                                                     

                              0       0       0.00    0.00    0.00    0.00                                                                                                                                                                                                    

                                                                                                                                                                                                                                                                              

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


將 Forwarding   https://992c-XXX-XXX-XXX-XXX.ngrok-free.app 資訊填入   https://developers.line.biz  內.

即可完成   使用 Ngrok + Perl 串接  LINE BOT Webhook                                                                                                                                                                          

Demo                                                                                           









2024年10月25日 星期五

Execute Perl WEB CGI in Oracle Linux 9


dnf install oracle-epel-release-el9.x86_64

dnf install  mod_perl

dnf install  perl-CGI perl-CPAN perl-HTTP-Tiny.noarch perl-JSON perl-Encode.x86_64 


vi /etc/httd/conf.d/perl.conf


#PerlModule ModPerl::Registry

##PerlModule ModPerl::PerlRun


Alias /cgi /var/www/html/cgi

<Directory /var/www/html/cgi>

    #SetHandler perl-script

    AddHandler perl-script .pl

    AddHandler perl-script .cgi

    PerlResponseHandler ModPerl::Registry

    ## PerlResponseHandler ModPerl::PerlRun

    PerlOptions +ParseHeaders

    Options +ExecCGI

    Order allow,deny

    Allow from all

</Directory>


systemctl restart httpd 


Test reference


https://hostingultraso.com/help/centos/implementing-cgi-perl-ruby-centos

Creating your first Perl CGI script

Now create the following Perl CGI script file by opening the new file vi /var/www/cgi-bin/perl-test.cgi and putting in the following content:
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
print header;
my $now = localtime;
print start_html(-title=>'Server time via Perl CGI'),
h1('Time'), p("The time is $now"),
end_html;



https://www.geeksforgeeks.org/perl-get-vs-post-in-cgi/


Example:

<html> 
<head></head> 
<body> 
<b> Search Your Query:</b><br> 
<FORM action="Gfg_get.pl" method = "GET"> 
<input type="text" name="q" size="20" maxlength="120"> 
<input type="submit" value="Search"><br> 
<input type="radio" name="l" value="Web" checked>Web 
<input type="radio" name="l" value="India">IND 
</FORM> 
</body> 
</html> 




Perl-CGI script for the above GET method form:


#!"c:\xampp\perl\bin\perl.exe" 
    
$buffer = $ENV{'QUERY_STRING'}; 
#split information into key/value pairs 
@pairs = split(/&/, $buffer); 
foreach $pair (@pairs)  
{ 
    ($name, $value) = split(/=/, $pair); 
    $value =~ tr/+/ /; 
    $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; 
    $value =~ s/~!/ ~!/g; 
    $FORM{$name} = $value; 
} 
  
$SearchTerm = $FORM{'q'}; 
$Location = $FORM{'l'}; 
  
print "Content-type:text/html\r\n\r\n"; 
print "<html>"; 
print "<head>"; 
print "<title>GeeksForGeeks - Get Method</title>"; 
print "</head>"; 
print "<body>"; 
print "<h3>Hello You searched '$Location' for '$SearchTerm'<br> 
Few Matches Found!<br> 
<br> 
Match 1<br> 
Match 2<br> 
Match 3<br> 
Match 4<br> 
etc.....</h3>"; 
print "</body>"; 
print "</html>"; 
  
1; 



My LAB Test Demo








2017年8月9日 星期三

2017年8月7日 星期一

Perl for Windows

Perl for windows

依據 https://www.perl.org/ 這裡的介紹,我們可以知道 Perl 可以在不同的平台上使用,
如 AIX/Solaris/Linux/FreeBSD/Windows/MAC OS.....

但在 Windows 平台上並沒有內建 Perl 的,所以得另外安裝,目前較多人使用有:

Strawberry Perl ( http://strawberryperl.com/ )

ActivePerl ( https://www.activestate.com/activeperl )

以 ActivePerl 為例可透過 ppm 去安裝 perl module ,
ppm 是一個 Windows UI 介面的安裝
如下圖中的 ActivePerl  為 5.20.2 (目前官網上已有更新至其它較新版本)

Microsoft Windows [版本 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\user>perl -v

This is perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x86-multi-thread-64int
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2015, Larry Wall

Binary build 2001 [298913] provided by ActiveState http://www.ActiveState.com
Built Mar 19 2015 15:26:52

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.


C:\Users\user>


另外 如使用  Strawberry Perl  則可使用 cpan or cpanm 去安裝,
下面 Demo 為  Strawberry Perl ( 5.26.0 )

Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\system32>perl -v

This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x86-multi-thread-64int

Copyright 1987-2017, 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.


C:\Windows\system32>cpanm
Usage: cpanm [options] Module [...]

Try `cpanm --help` or `man cpanm` for more options.

C:\Windows\system32>cpanm LINE::Bot::API
--> Working on LINE::Bot::API
Fetching http://www.cpan.org/authors/id/Y/YA/YAPPO/LINE-Bot-API-1.04.tar.gz ...
OK
Configuring LINE-Bot-API-1.04 ... OK
==> Found dependencies: Furl
--> Working on Furl
Fetching http://www.cpan.org/authors/id/T/TO/TOKUHIROM/Furl-3.11.tar.gz ... OK
Configuring Furl-3.11 ... OK
==> Found dependencies: HTTP::Parser::XS, Test::TCP
--> Working on HTTP::Parser::XS
Fetching http://www.cpan.org/authors/id/K/KA/KAZUHO/HTTP-Parser-XS-0.17.tar.gz .
.. OK
Configuring HTTP-Parser-XS-0.17 ... OK
Building and testing HTTP-Parser-XS-0.17 ... OK
Successfully installed HTTP-Parser-XS-0.17
--> Working on Test::TCP
Fetching http://www.cpan.org/authors/id/T/TO/TOKUHIROM/Test-TCP-2.19.tar.gz ...
OK
Configuring Test-TCP-2.19 ... OK
==> Found dependencies: Test::SharedFork
--> Working on Test::SharedFork
Fetching http://www.cpan.org/authors/id/E/EX/EXODIST/Test-SharedFork-0.35.tar.gz
 ... OK
Configuring Test-SharedFork-0.35 ... OK
Building and testing Test-SharedFork-0.35 ... OK
Successfully installed Test-SharedFork-0.35
Building and testing Test-TCP-2.19 ... O
......
.........

這要看個人喜愛,這2個都不錯...


當然  ActivePerl  也是可以在 CLI 下 ppm install 去裝 Perl module 如下 Demo...

C:\Perl\bin>ppm install LINE-Bot-API
Downloading LINE-Bot-API-1.04...done
Downloading Furl-3.08...done
Downloading JSON-XS-3.03...done
Downloading HTTP-Parser-XS-0.17...done
Downloading Class-Accessor-Lite-0.08...done
Downloading common-sense-3.74...done
Downloading Types-Serialiser-1.0...done
Unpacking LINE-Bot-API-1.04...done
Unpacking Furl-3.08...done
Unpacking JSON-XS-3.03...done
Unpacking HTTP-Parser-XS-0.17...done
Unpacking Class-Accessor-Lite-0.08...done
Unpacking common-sense-3.74...done
Unpacking Types-Serialiser-1.0...done
Generating HTML for LINE-Bot-API-1.04...done
Generating HTML for Furl-3.08...done
Generating HTML for JSON-XS-3.03...done
Generating HTML for HTTP-Parser-XS-0.17...done
Generating HTML for Class-Accessor-Lite-0.08...done
Generating HTML for common-sense-3.74...done
Generating HTML for Types-Serialiser-1.0...done
Updating files in site area...done
  54 files installed

C:\Perl\bin>


2017年7月19日 星期三

Zabbix 3.0.x 整合 Line Alert 圖解



Zabbix 3.0.x 整合 Line Alert 圖解,下方有 Perl 的範例程式,可供參考,
使用其的慣用程式也行,至於 Line API 的使用方式可參考下面的其它參考資訊,
或見 https://github.com/line





































































Demo



























其它參考資訊

http://xrcd2.blogspot.tw/2016/12/perl-line-api.html
http://xrcd2.blogspot.tw/2017/07/line-api-emoticons.html

Demo Perl Shell:

[root@CentOS73 shell]# pwd
/usr/local/zabbix/shell
[root@CentOS73 shell]# cat line.pl
#!/usr/local/perl-5.26/bin/perl
# Line
#recipient = sys.argv[1]  $ARGV[0]
#subject = sys.argv[2]    $ARGV[1]
#body = sys.argv[3]       $ARGV[2]

$myid="$ARGV[0]";
$subj="$ARGV[1]";
$body="$ARGV[2]";

use LINE::Bot::API;
use LINE::Bot::API::Builder::SendMessage;
$bot = LINE::Bot::API->new(
 channel_secret => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
 channel_access_token => 'xxxxxxxxxxxxxxxxxxxxxx',
);

$messages = LINE::Bot::API::Builder::SendMessage->new;
$messages->add_text( text => "$subj \r\n \r\n $body " );
$bot->push_message($myid, $messages->build);
[root@CentOS73 shell]#


2017年7月14日 星期五

line API emoticons 試用

為解一個 line API 傳送文字的限制,在官網上看到的資訊,
順便玩玩這個  emoticons 功能.
官網上的 URL 為 https://devdocs.line.me/en/#send-message-object

Send message object

JSON object which contains the contents of the message you send.

Text
Image
Video
Audio
Location
Sticker
Imagemap
Template
Text

Text example
{
    "type": "text",
    "text": "Hello, world"
}
Field Type Required Description
type String Yes text
text String Yes Message text
Max: 2000 characters
Text example with emoticon

{
    "type": "text",
    "text": "Hello, world 􀂲"
}
You can include emoticons in your message text. Because emoticons are
a part of Unicode, they must be converted from their character codes.
Note that the character code, 0x1000B2, is used in the example on the
right. Although the emoticon does not show up correctly in the browser,
it will show up in LINE.
For a list of emoticons that can be sent with the Messaging API,
see emoticon list.

https://devdocs.line.me/files/emoticon.pdf

perl 的用法可參考
http://www.drdobbs.com/web-development/unicode-in-perl/184416148 
及
https://github.com/patch-orphan/text-emoticon-unicode-pm5


加上 https://github.com/line/line-bot-sdk-perl

所以可以加以變化成 如下的程式.....

[root@CentOS73 bin]# cat Send-Line-Message.pl
#!/usr/local/perl-5.26/bin/perl
use LINE::Bot::API;
use LINE::Bot::API::Builder::SendMessage;

$bot = LINE::Bot::API->new(
    channel_secret       => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
    channel_access_token => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
);


use Text::Emoticon;
$emoticon = Text::Emoticon->new('Unicode');
$msg = $emoticon->filter('Howdy :) :( ');


$ToLineUid = 'Uxxxxxxxxxxxxxxxxxxxxxxxxxxxx';


$messages = LINE::Bot::API::Builder::SendMessage->new;
$messages->add_text( text => "Example push text \r\n $msg \x{1F480}" );
$bot->push_message($ToLineUid, $messages->build);
[root@CentOS73 bin]#

Demo



其它參考資訊

http://unicode.org/emoji/charts/full-emoji-list.html

http://xrcd2.blogspot.tw/2016/12/perl-line-api.html

2017年1月8日 星期日

Send Splunk Alerts to Line API 試作

Send Splunk Alerts to Line API 試作,參考 URL 如下



在 Line API 介接的程式放置於  $SPLUNK_HOME/bin/scripts


SPLUNK SNMP DATA WITH CACTI MIRAGE 試作 延伸而來.

其它參考資訊:



[root@bbb scripts]# pwd
/opt/splunk/bin/scripts

[root@bbb scripts]# ll
total 12
-rwxr-xr-x 1 root   root   659 Jan  9 14:30 line.pl
-r--r--r-- 1 splunk splunk  71 Oct 26 00:27 readme.txt

[root@bbb scripts]# cat line.pl 
#!/usr/local/bin/perl
use LINE::Bot::API;
use LINE::Bot::API::Builder::SendMessage;
$bot = LINE::Bot::API->new(
channel_secret => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
channel_access_token => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);

$ToLineUid = "xxxxxxxxxxxxxxxxxxxxxxx";
$messages = LINE::Bot::API::Builder::SendMessage->new;
$messages->add_text( text => " Splunk $ARGV[0] $ARGV[1] $ARGV[2] $ARGV[3] $ARGV[4] $ARGV[5] $ARGV[6] $ARGV[7] $ARGV[8] " );
$bot->push_message($ToLineUid, $messages->build);

[root@bbb scripts]#

























Demo







































































2017年1月4日 星期三

如何做出 cacti plugin manager + Line API Alert

在 Cacti 官網上有說到 這個 manager plugin 的用途為何及如何安裝這個 Plugin.
URL 如下 http://docs.cacti.net/userplugin:manage
這裡不再說明了,本 LAB 是用 plugin manage + TCP Port Template 這二個元件做出.
關於 TCP Port Template 安裝與請參閱.
http://forums.cacti.net/viewtopic.php?f=12&t=16477

本 LAB 的重點在於說明  manager  與 Line API 的介接,並發出 Alert Message.

在 Manage 裝好後,在 Cacti WEB UI \ Configuration \ Settings \ Manage
可以看見它提供了 Netsend Options 的功能,
簡單的說它可以透過 Enable "Net send" alerts 的觸發 Windows Net Send 機制,
所以我們可以將這個機制變更為與 Line API 的介接.

因為它是使用 Perl 程式語言.所以我們可以參考 Line 官網上
Push message (Perl) 的寫法,去改寫它即可.

https://devdocs.line.me/en/?perl#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
    ....
}


沒錯程式就是這樣..

==========


接下來我們再看一下原來的 netsend.pl 的 Source Code .


[root@bbb manage]# pwd
/var/www/html/cacti/plugins/manage
[root@bbb manage]#

[root@bbb manage]# cat netsend.pl

use Net::NetSend qw(:all);
use Net::Nslookup;
  
my $target_netbios_name = $ARGV[0];
my $source_netbios_name = "CACTI";
my $target_ip = nslookup(host => $ARGV[0], type => "A");
my $message = $ARGV[1];
my $debug = 0;

my $success = sendMsg($target_netbios_name, $source_netbios_name, $target_ip, $message, $debug);

print ($success ? "Delivery successfull\n" : "Error in delivery! \n$@\n");


=========

可先使用 # cp netsend.pl netsend.pl.bak  將原程式備份,再去改寫該程式. 

Push Line message API example

#!/usr/local/bin/perl
use LINE::Bot::API;
use LINE::Bot::API::Builder::SendMessage;
$bot = LINE::Bot::API->new(
   channel_secret       => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
   channel_access_token => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);

$ToLineUid = "$ARGV[0]";
$messages = LINE::Bot::API::Builder::SendMessage->new;
$messages->add_text( text => " $ARGV[1] " );
$bot->push_message($ToLineUid, $messages->build);

======

沒錯程式就是這樣..


補充說明,那個 $ARGV[0] 是第一個參數 為 Line UID 的,
(原來的用法是指 Net Send 接收者)
可在 Cacti WEB UI \ Configuration \ Settings \ Manage \
Netsend Options \ Statuis that will  receiv 'net send' alerts
的地方輸入.$ARGV[1] 是第二個參數,是原 Manage Alert Message.

所以程式的介接改寫就是這樣簡單.


WEB UI 設定畫面





















Demo





































其它參考資訊


使用 Perl 建立 Line API 運用環境
http://xrcd2.blogspot.tw/2016/12/perl-line-api.html

Line API 使用小筆記
http://www.vlab.com.tw/index.php/forum/21/15572-line-api


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 API與Reply 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
    ....
}