File size: 4,532 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
package mockslack
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type actionItem struct {
ActionID string `json:"action_id"`
BlockID string `json:"block_id"`
Text struct {
Type string
Text string
}
Value string
Type string
}
type actionBody struct {
Type string
AppID string `json:"api_app_id"`
Channel struct{ ID string }
User struct {
ID string
Username string
Name string
TeamID string `json:"team_id"`
}
Team struct {
ID string
Domain string
}
ResponseURL string `json:"response_url"`
Actions []actionItem
}
func (s *Server) ServeActionResponse(w http.ResponseWriter, r *http.Request) {
var req struct {
Text string
Type string `json:"response_type"`
Blocks []struct {
Type string
Text struct{ Text string }
Elements []struct {
Type string
Text struct{ Text string }
Value string
ActionID string `json:"action_id"`
URL string
}
}
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if req.Type != "ephemeral" {
http.Error(w, "unexpected response type", http.StatusBadRequest)
return
}
actData := r.URL.Query().Get("action")
var a Action
err := json.Unmarshal([]byte(actData), &a)
if respondErr(w, err) {
return
}
opts := ChatPostMessageOptions{
ChannelID: a.ChannelID,
User: r.URL.Query().Get("user"),
}
if len(req.Blocks) > 0 {
// new API
for _, block := range req.Blocks {
switch block.Type {
case "section":
opts.Text = block.Text.Text
case "actions":
for _, action := range block.Elements {
if action.Type != "button" {
continue
}
opts.Actions = append(opts.Actions, Action{
ChannelID: a.ChannelID,
TeamID: a.TeamID,
AppID: a.AppID,
ActionID: action.ActionID,
Text: action.Text.Text,
Value: action.Value,
URL: action.URL,
})
}
}
}
} else {
opts.Text = req.Text
}
msg, err := s.API().ChatPostMessage(r.Context(), opts)
if respondErr(w, err) {
return
}
var respData struct {
response
TS string
Channel string `json:"channel"`
Message *Message `json:"message"`
}
respData.TS = msg.TS
respData.OK = true
respData.Channel = msg.ChannelID
respData.Message = msg
respondWith(w, respData)
}
// PerformActionAs will perform the action as the given user.
func (s *Server) PerformActionAs(userID string, a Action) error {
usr := s.user(userID)
if usr == nil {
return errors.New("invalid Slack user ID")
}
app := s.app(a.AppID)
if app == nil {
return errors.New("invalid Slack app ID")
}
actionData, err := json.Marshal(a)
if err != nil {
return err
}
var p actionBody
p.Type = "block_actions"
p.User.ID = usr.ID
p.User.Username = usr.Name
p.User.Name = usr.Name
p.User.TeamID = a.TeamID
p.Team.ID = a.TeamID
p.Team.Domain = "example.com"
p.Channel.ID = a.ChannelID
p.AppID = a.AppID
tok := s.newToken(AuthToken{
User: userID,
Scopes: []string{"bot"},
})
p.ResponseURL = fmt.Sprintf("%s/actions/response?token=%s&user=%s&action=%s", strings.TrimSuffix(s.urlPrefix, "/"), url.QueryEscape(tok.ID), url.QueryEscape(usr.ID), url.QueryEscape(string(actionData)))
var action actionItem
action.ActionID = a.ActionID
action.BlockID = a.BlockID
action.Text.Type = "plain_text"
action.Text.Text = a.Text
action.Value = a.Value
action.Type = "button"
p.Actions = append(p.Actions, action)
data, err := json.Marshal(p)
if err != nil {
return fmt.Errorf("serialize action payload: %w", err)
}
v := make(url.Values)
v.Set("payload", string(data))
data = []byte(v.Encode())
req, err := http.NewRequest("POST", app.ActionURL, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("create action request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
t := time.Now()
req.Header.Set("X-Slack-Request-Timestamp", strconv.FormatInt(t.Unix(), 10))
h := hmac.New(sha256.New, []byte(app.SigningSecret))
fmt.Fprintf(h, "v0:%d:%s", t.Unix(), string(data))
req.Header.Set("X-Slack-Signature", "v0="+fmt.Sprintf("%x", h.Sum(nil)))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("perform action: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("perform action: %s", resp.Status)
}
return nil
}
|