본문 바로가기

DevOps/Slack & API Gateway

Slack Slash Command를 통해 AWS 서버 스케줄링하기 - 3. API Gateway & Slack

REST API로 구성하도록 하겠습니다.

 

 

Method를 POST로 Auth 함수를 연동시켜주세요

 

 

매핑 탬플릿은 아래와 같습니다.

application/x-www-form-urlencoded
## convert HTML POST data to JSON
 
## get the raw post data from the AWS built-in variable and give it a nicer name
#set($rawAPIData = $input.path('$'))

## first we get the number of "&" in the string, this tells us if there is more than one key value pair
#set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length())
 
## if there are no "&" at all then we have only one key value pair.
## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
## the "empty" kv pair to the right of the ampersand will be ignored anyway.
#if ($countAmpersands == 0)
 #set($rawPostData = $rawAPIData + "&")
#end
 
## now we tokenise using the ampersand(s)
#set($tokenisedAmpersand = $rawAPIData.split("&"))
 
## we set up a variable to hold the valid key value pairs
#set($tokenisedEquals = [])
 
## now we set up a loop to find the valid key value pairs, which must contain only one "="
#foreach( $kvPair in $tokenisedAmpersand )
 #set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
 #if ($countEquals == 1)
  #set($kvTokenised = $kvPair.split("="))
  #if ($kvTokenised[0].length() > 0)
   ## we found a valid key value pair. add it to the list.
   #set($devNull = $tokenisedEquals.add($kvPair))
  #end
 #end
#end
 
## next we set up our loop inside the output structure "{" and "}"
{
#foreach( $kvPair in $tokenisedEquals )
  ## finally we output the JSON for this pair and append a comma if this isn't the last pair
  #set($kvTokenised = $kvPair.split("="))
 "$util.urlDecode($kvTokenised[0])" : #if($kvTokenised[1].length() > 0)"$util.urlDecode($kvTokenised[1])"#{else}""#end#if( $foreach.hasNext ),#end
#end
}

 

그리고 배포해주시면 API Gateway설정은 끝나게됩니다.

 

이제, Slack App -> Slash Command에서 Create New Command 를 해주세요

 

/ag도 마찬가지로 구성해주시면 됩니다.

 

그러면 모든 세팅이 끝났습니다.

테스트를 해보면 아래와 같은 결과들을 볼 수 있습니다.

 

 

유용하게 사용하실 수 있으면 좋겠습니다.