| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | ) |
| 6 | |
| 7 | // SlashCommand contains information about a request of the slash command |
| 8 | type SlashCommand struct { |
| 9 | Token string `json:"token"` |
| 10 | TeamID string `json:"team_id"` |
| 11 | TeamDomain string `json:"team_domain"` |
| 12 | EnterpriseID string `json:"enterprise_id,omitempty"` |
| 13 | EnterpriseName string `json:"enterprise_name,omitempty"` |
| 14 | ChannelID string `json:"channel_id"` |
| 15 | ChannelName string `json:"channel_name"` |
| 16 | UserID string `json:"user_id"` |
| 17 | UserName string `json:"user_name"` |
| 18 | Command string `json:"command"` |
| 19 | Text string `json:"text"` |
| 20 | ResponseURL string `json:"response_url"` |
| 21 | TriggerID string `json:"trigger_id"` |
| 22 | APIAppID string `json:"api_app_id"` |
| 23 | } |
| 24 | |
| 25 | // SlashCommandParse will parse the request of the slash command |
| 26 | func SlashCommandParse(r *http.Request) (s SlashCommand, err error) { |
| 27 | if err = r.ParseForm(); err != nil { |
| 28 | return s, err |
| 29 | } |
| 30 | s.Token = r.PostForm.Get("token") |
| 31 | s.TeamID = r.PostForm.Get("team_id") |
| 32 | s.TeamDomain = r.PostForm.Get("team_domain") |
| 33 | s.EnterpriseID = r.PostForm.Get("enterprise_id") |
| 34 | s.EnterpriseName = r.PostForm.Get("enterprise_name") |
| 35 | s.ChannelID = r.PostForm.Get("channel_id") |
| 36 | s.ChannelName = r.PostForm.Get("channel_name") |
| 37 | s.UserID = r.PostForm.Get("user_id") |
| 38 | s.UserName = r.PostForm.Get("user_name") |
| 39 | s.Command = r.PostForm.Get("command") |
| 40 | s.Text = r.PostForm.Get("text") |
| 41 | s.ResponseURL = r.PostForm.Get("response_url") |
| 42 | s.TriggerID = r.PostForm.Get("trigger_id") |
| 43 | s.APIAppID = r.PostForm.Get("api_app_id") |
| 44 | return s, nil |
| 45 | } |
| 46 | |
| 47 | // ValidateToken validates verificationTokens |
| 48 | func (s SlashCommand) ValidateToken(verificationTokens ...string) bool { |
| 49 | for _, token := range verificationTokens { |
| 50 | if s.Token == token { |
| 51 | return true |
| 52 | } |
| 53 | } |
| 54 | return false |
| 55 | } |