Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env perl
use Mojolicious::Lite;
use HTTP::Tiny;
use JSON;
# ピザ会課題
# mojoliciousから、IFTTTのwebhookに経由でLINEを送る
# IFTTT経由でLINEにメッセージを送る処理
# IFTTT側での準備が必要
my $IFTTT_event = 'IFTTTのEvent Name';
my $IFTTT_key = 'IFTTTのwebhookのkey';
my $IFTTT_API_URI
= 'https://maker.ifttt.com/trigger/'
. $IFTTT_event
. '/with/key/'
. $IFTTT_key;
get '/' => sub {
my $c = shift;
$c->render( template => 'index' );
};
get '/done' => sub {
my $c = shift;
$c->render( template => 'done' );
};
post '/' => sub {
my $c = shift;
my $status = $c->param('status');
# IFTTTにアクセスするサブルーチン
send_status_to_ifttt($status);
$c->redirect_to('/done');
};
#
sub send_status_to_ifttt {
# ステータスを引数として受け取る
my $status = shift @_;
# 受け取ったステータスをハッシュの形にする
my %hash = ( value1 => $status );
# ハッシュをハッシュリファレンスにして、JSONに変換する
my $json = encode_json( \%hash );
# URLを叩くのには標準モジュールのHTTP::Tinyを利用
my $http = HTTP::Tiny->new();
# ヘッダーやjsonなどを添えてIFTTTにアクセスする
my $response = $http->request(
'POST',
$IFTTT_API_URI,
{ headers => { 'Content-Type' => 'application/json' },
content => $json,
}
);
return;
}
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>LINEに状態をIFTTT経由で送る</h1>
<form action="/" method="post">
<input type="submit" name="status" value="sleeping" >
<input type="submit" name="status" value="studying" >
<input type="submit" name="status" value="dying" >
</form>
@@ done.html.ep
% layout 'default';
% title 'Welcome';
<h1>おくったよ</h1>
<a href="/">最初の画面に戻る</a>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
#!/usr/bin/env perl
use Mojolicious::Lite;
# ピザ会課題
# mojoliciousから、IFTTTのwebhookに経由でLINEを送る
# まずは普通にtwitter APIを使う
use Net::Twitter::Lite::WithAPIv1_1;
use Scalar::Util 'blessed';
my $consumer_key = '';
my $consumer_secret = '';
my $token = '';
my $token_secret = '';
my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
access_token => $token,
access_token_secret => $token_secret,
ssl => 1,
);
get '/' => sub {
my $c = shift;
$c->render( template => 'index' );
};
get '/done' => sub {
my $c = shift;
$c->render( template => 'done' );
};
post '/' => sub {
my $c = shift;
my $status = $c->param('status');
$nt->update($status);
$c->redirect_to('/done');
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>状態</h1>
<form action="/" method="post">
<input type="submit" name="status" value="sleep" >
<input type="submit" name="status" value="study" >
<input type="submit" name="status" value="dying" >
</form>
@@ done.html.ep
% layout 'default';
% title 'Welcome';
<h1>おくったよ</h1>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.