Last active
February 27, 2022 08:44
-
-
Save imyaman/70013bc5d83432682ed2d73257454c1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use JSON qw(from_json to_json decode_json); | |
use URI::Encode qw(uri_decode); | |
use Data::Dumper; | |
use Dancer2; | |
use HTTP::CookieJar::LWP (); | |
use LWP::UserAgent (); | |
use Data::Dumper; | |
use Encode; | |
use feature 'signatures'; | |
my ( $jar, $ua ); | |
my ($key); | |
$key = '____:____'; | |
$jar = HTTP::CookieJar::LWP->new; | |
$ua = LWP::UserAgent->new( | |
cookie_jar => $jar, | |
protocols_allowed => [ 'http', 'https' ], | |
#timeout => 10, | |
); | |
post '/webhook-test' => sub { | |
my $self = shift; | |
my ( $rbody, $evtype ); | |
my @evtypes = ( "postCreated", "postSubjectChanged" ); | |
my @evprocessors = ( \&postCreated, \&postSubjectChanged ); | |
headers 'content-type' => 'application/json'; | |
# request->body 로 payload 를 확인합니다. request->body 는 string 입니다. | |
# 이를 hash로 바꿉니다. 500 에러를 방지하기 위해 eval을 사용합니다. | |
# $rbody 는 hashref 입니다. | |
# eval ... or do ... 는 try/catch 와 비슷합니다. | |
eval { $rbody = decode_json( request->body ); } or do { | |
debug $@; | |
return to_json( { "result" => "Not OK. It's not json" } ); | |
}; | |
# dooray는 웹훅을 두 가지 포맷으로 보낼 수 있습니다. Slack을 위한 포맷이면 바로 응답합니다. | |
if ( $rbody->{channel} ) { | |
debug "It's for Slack, not for me."; | |
return to_json( { "result" => "Not OK. It's for Slack" } ); | |
} | |
# dooray는 이벤트 타입을 알 수 없으면 바로 응답합니다. | |
$evtype = $rbody->{hookEventType}; | |
if ( $evtype eq "" ) { | |
debug "It's not for an event"; | |
return to_json( { "result" => "Not OK. It's not for an event" } ); | |
} | |
for ( my $i = 0 ; $i < scalar @evtypes ; $i++ ) { | |
if ( $evtype eq $evtypes[$i] ) { | |
# $rbody 는 hashref 입니다, 위에 말했듯이. | |
# 함수 안에서 $body와 $rbody는 같은 hashref 입니다. | |
# 함수 안에서 hash를 return 한 걸 클라이언트에게 응답으로 줍니다. | |
return $evprocessors[$i]($rbody); | |
last; | |
} | |
} | |
return to_json( { "result" => "OK. Nothing to do for the event: $evtype" } ); | |
}; | |
sub postSubjectChanged ($body) { | |
debug "I got postSubjectChanged"; | |
return to_json( { "result" => "ok, postSubjectChanged" } ); | |
} | |
sub postCreated ($body) { | |
my ($title); | |
debug "I got postCreated"; | |
$title = $body->{post}->{subject}; | |
if ( $title eq "" ) { | |
debug "Posts must have a title"; | |
return to_json( { "result" => "Not OK, the post doesn't have a title" } ); | |
} | |
debug "The task title is; $title"; | |
if ( $title =~ m/화상회의/ ) { | |
assignToMe($body); | |
} | |
return to_json( { "result" => "OK. Nothing to do on postCreated" } ); | |
} | |
sub assignToMe ($body) { | |
# 태스크의 담당자를 "나"로 지정합니다 | |
my ( $memberId, $projectId, $taskId, $payload, $uri, $req, $response ); | |
$memberId = '3118892665733691672'; | |
$projectId = $body->{project}->{id}; | |
$taskId = $body->{post}->{id}; | |
$payload = | |
'{"users":{"to":[{"type":"member", "member":{"organizationMemberId":"3118892665733691672"}}]}}'; | |
$uri = "https://api.dooray.com/project/v1/projects/$projectId/posts/$taskId"; | |
$req = HTTP::Request->new( 'PUT', $uri ); | |
$req->header( 'Authorization' => "dooray-api $key" ); | |
$req->header( 'Accept' => 'application/json' ); | |
$req->content_type('application/json'); | |
$req->content($payload); | |
debug $req; | |
$response = $ua->request($req); | |
if ( $response->is_success ) { | |
debug $response->decoded_content; | |
return to_json( { "result" => "OK, postCreated, changed the assignee" } ); | |
} | |
else { | |
debug $response->status_line; | |
return to_json( | |
{ "result" => "OK, postCreated, but could not change the assignee" } ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment