2024年11月30日 星期六

LINE Messaging API / Perl CGI 試作

延續前筆記 

使用 Ngrok + Perl 串接 LINE BOT Webhook

https://xrcd2.blogspot.com/2024/11/ngrok-perl-line-bot-webhook.html


參考 URL

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

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

舊筆記

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


Perl CGI 程式抄改參考

https://m.runoob.com/perl/perl-cgi-programming.html


[root@node1 cgi]# pwd

/var/www/html/cgi


[root@node1 cgi]# cat index.html 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>CGI Demo</title>

</head>

<body>

<form action="/cgi/perl.cgi" method="post">

Subject: <input type="text" name="subject">  <br />

 

Body: <input type="text" name="body" />

<input type="submit" value="submit" />

</form>

</body>

</html>


[root@node1 cgi]# cat perl.cgi 

#!/usr/bin/perl

 

local ($buffer, @pairs, $pair, $name, $value, %FORM);

# 讀取 POST 資訊

$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

if ($ENV{'REQUEST_METHOD'} eq "POST")

{

   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

}else {

   $buffer = $ENV{'QUERY_STRING'};

}

# 讀取 POST/FORM 資訊

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)

{

   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;

   $value =~ s/%(..)/pack("C", hex($1))/eg;

   $FORM{$name} = $value;

}

$subject = $FORM{subject};

$body  = $FORM{body};

 

print "Content-type:text/html\r\n\r\n";

print "<html>";

print "<head>";

print '<meta charset="utf-8">';

print '<title>CGI DEMO</title>';

print "</head>";

print "<body>";

print "<h2>$subject <P> $body</h2>";

print "</body>";

print "</html>"; 


linebot();


1;



sub linebot {

use LINE::Bot::API;

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

use JSON;

use utf8;

use Encode;


$subject = Encode::decode('UTF-8', $subject);

$body = Encode::decode('UTF-8', $body);


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$now = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);


$bot = LINE::Bot::API->new(

   channel_secret       => '738feXXXXXXXXXXXXXXXXXXXXcf43d7',

   channel_access_token => 'dqlhcXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1cDnyilFU=',

);


$ToLineUid = "U74ea4fXXXXXXXXXXXXXXXXXXX2d866b1";

#$ToLineGid = "C39bdb2XXXXXXXXXXXXXXXXXX6bb32dea";


$messages = LINE::Bot::API::Builder::SendMessage->new;

$messages->add_text( text => "$now \n $subject \n $body " );

$bot->push_message($ToLineUid, $messages->build);


}

[root@node1 cgi]# 


Demo









Synology NAS [  DSM Alert Demo ]







沒有留言:

張貼留言