AWS Lambda를 활용한 Slack Slash 명령어 생성

AWS Lambda와 API GateWay를 활용한 Slack의 Slash 명령어를 만들어 보려고 합니다.

진행 순서

  1. Slack의 App과 Slash 명령어 생성
  2. AWS Lambda + API Gateway 생성
  3. 연동

Slack의 App과 Slash 명령어 생성

Slack에 가입 후 Cloud Study라는 채널에 입장한 상태로 시작합니다~

슬랙은 메신저 뿐만 아니라 다양한 통합 기능을 제공하고 있습니다. (Web Hooking, Slash Command등)

post_1_1

post_1_2

post_1_3

post_1_4

post_1_5

post_1_6

AWS Lambda 생성

아마존 클라우드 프리티어 사용 중입니다~

아마존 클라우드 로그인 후 대쉬보드 화면으로 GO GO

post_1_7

post_1_8

post_1_9

// The entry point of this lambda function.
exports.handler = function(event, context) {
  console.log(event);

  context.succeed({
    "response_type": "in_channel",
    "text": "I found some about \"" + event.text + "\"",
    "attachments": [
        {
            "text": "For more info please visit to \nhttps://ko.wikipedia.org/wiki/" + event.text,
            "color": "#7CD197"
        }
    ]
  });

};

post_1_10

post_1_11

API Gateway 생성

AWS Lambda는 혼자서는 어떤 기능을 하기 힘들기 때문에 이제 API Gateway와 연결해서 Slack에서 가져다 쓸 수 있도록 작업해 보겠습니다. post_1_12

post_1_13 post_1_14

post_1_14

post_1_16

Slack에서 명령어를 호출하면 "application/x-www-form-urlencoded" Content-Type을 사용하여 폼 post 방식 전송을 하기 때문에 이를 JSON 형태로 변환하는 작업이 필요합니다.

마지막으로 Actions 드롭다운박스를 클릭해서 Deploy API 해 주셔야 수정한 것들이 적용됩니다.

연결

Lambda와 API Gateway가 잘 생성됐으면 맨 왼쪽 Tree에서 API » Stages 에 가면 Invoke URL 정보를 보실 수 있습니다.

이 URL을 복사해서 아까 맨 처음에 Slack 명렁어 생성시 비워뒀던 Request URL에 채워넣고, Slack 채팅창으로 가서 생성한 명령어가 사용가능한지 확인 해 보시면 됩니다~

블로그도 처음인데 마크다운 문법을 사용해서 하다보니 너무 힘들었네요..ㅠㅠ


참고한 블로그
AWS Lambda와 API Gateway로 Slack Bot 만들기
AWS API GatewayでContent-Type:application/x-www-form-urlencoded のPOSTデータを受け取り JSONに変換する
AWS Lambda를 이용해서 GitHub과 Slack 연동하기
[AWS]서버없이 Lambda와 API Gateway로 서버API 만들기