Slack Slash Command (with Golang)

임태빈·2022년 6월 16일
0

slack

목록 보기
3/3

개요

안녕하세요.

이번글에서는 Slack Slash Command에 대해서 작성해보려고 합니다.

Slack Slash Command는 슬랙에서 많이 활용되고 있는 기술 중 하나입니다.

문자를 입력하는 에디터창에서 슬래시(/)를 한다음 명령어를 입력하여 자신이 원하는 작업을 할 수 있게 해줍니다.

Slack에서는 이런 기술을 Slack bot에서도 사용할 수 있게 해줄 수 있어서 Bot에 다양한 기능을 추가해줄 수 있습니다.

먼저 이를 작성하는 방법에 대해서 알아보겠습니다.

Slack Slash Command 만들기

  • Slack Api 사이트에 접속하여 Yours App을 클릭하여 slash command를 넣어줄 프로젝트를 선택해줍니다.

  • 메인 페이지 왼쪽 메뉴에서 Features → Slash Commands에 들어가 Create New Command를 클릭해줍니다.

  • Create New Command를 클릭하게 하단의 페이지를 볼 수 있습니다. 여기서 input 값들의 정의는 다음과 같습니다.
    Command: 명령어 단축어
    Request URL: Slash Command 와 interactive하는 서버
    Short Description: 해당 Command에 대한 설명
    Usage Hint: 사용 방법에 대한 설명

Slack Slash Command Server 응답하기

  • Slack Slash Command 가 입력되었을 때 서버에는 다음과 같은 Payload가 전달받게 됩니다.
token=gIkuvaNzQIHg97ATvDxqgjtO
&team_id=T0001
&team_domain=example
&enterprise_id=E0001
&enterprise_name=Globular%20Construct%20Inc
&channel_id=C2147483705
&channel_name=test
&user_id=U2147483697
&user_name=Steve
&command=/weather
&text=94070
&response_url=https://hooks.slack.com/commands/1234/5678
&trigger_id=13345224609.738474920.8088930838d88f008e0
&api_app_id=A123456
  • Slack Slash Command에 경우 slack에서 Command와 연동된 url에 보내게 되면 POST 방식에 x-www-form-urlencoded로 들어옵니다. 따라서 서버에서는 Form방식에 데이터를 긁어와서 사용하시면 됩니다.
  • 저는 golang 기반으로 만들어서 사용했기에 다음 라이브러리들을 설치하였습니다.
go get -u github.com/slack-go/slack
go get github.com/joho/godotenv
go get -u github.com/gin-gonic/gin
  • 저는 gin gonic을 사용했기에 form을 받는 방법은 다음과 같이 api코드를 작성했습니다.
func SlackCommandSlash(c *gin.Context) {
    //slack Signing Secret 얻기
	SlackSigningSecret := os.Getenv("SLACK_SIGNING_SECRET")
	//slack verify 진행
	if result := config.VerifyRequest(c, []byte(SlackSigningSecret)); result == false {
		c.String(400, "bad request")
		return
	}
	// slach command 파싱하기
	s, err := slack.SlashCommandParse(c.Request)
	if err != nil {
		c.String(400, "bad request")
		return
	}
	// testing ㅎ가디
    c.String(200, "good")
}
profile
golang과 서버 개발을 하고 있는 개발자입니다.

0개의 댓글