Skip to content

Instantly share code, notes, and snippets.

@imyaman
Created March 4, 2022 14:17
Show Gist options
  • Save imyaman/96173adb5af96cd51dd6cbcc4b31a8d6 to your computer and use it in GitHub Desktop.
Save imyaman/96173adb5af96cd51dd6cbcc4b31a8d6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use common::sense;
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;
my ( $jar, $ua );
my ($key);
$key = '____:____';
$jar = HTTP::CookieJar::LWP->new;
$ua = LWP::UserAgent->new(
cookie_jar => $jar,
protocols_allowed => [ 'https' ],
);
post '/webhook-test' => sub {
my $self = shift;
my ( $rbody, $evtype );
my %ev = ("postCreated" => \&postCreated, "postSubjectChanged" => \&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" } );
}
return $ev{$evtype}($rbody) if (defined $ev{$evtype});
return to_json( { "result" => "OK. Nothing to do for the event: $evtype" } );
};
sub postSubjectChanged {
my ($body) = @_;
debug "I got postSubjectChanged";
return to_json( { "result" => "OK, postSubjectChanged" } );
};
sub postCreated {
my ($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 {
my ($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