| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"log" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/slack-go/slack" |
| 13 |
"github.com/slack-go/slack/slackevents" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
IDSelectOptionBlock = "select-option-block" |
| 18 |
IDExampleSelectInput = "example-select-input" |
| 19 |
) |
| 20 |
|
| 21 |
func handleMyWorkflowStep(w http.ResponseWriter, r *http.Request) { |
| 22 |
if r.Method != http.MethodPost { |
| 23 |
w.WriteHeader(http.StatusMethodNotAllowed) |
| 24 |
return |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
body, err := ioutil.ReadAll(r.Body) |
| 29 |
if err != nil { |
| 30 |
w.WriteHeader(http.StatusBadRequest) |
| 31 |
return |
| 32 |
} |
| 33 |
|
| 34 |
eventsAPIEvent, err := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionNoVerifyToken()) |
| 35 |
if err != nil { |
| 36 |
log.Printf("[ERROR] Failed on parsing event: %s", err.Error()) |
| 37 |
w.WriteHeader(http.StatusInternalServerError) |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
if eventsAPIEvent.Type == slackevents.URLVerification { |
| 43 |
var r *slackevents.ChallengeResponse |
| 44 |
err := json.Unmarshal([]byte(body), &r) |
| 45 |
if err != nil { |
| 46 |
log.Printf("[ERROR] Failed to decode json message on event url_verification: %s", err.Error()) |
| 47 |
w.WriteHeader(http.StatusInternalServerError) |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
w.Header().Set("Content-Type", "text") |
| 52 |
w.Write([]byte(r.Challenge)) |
| 53 |
return |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
if eventsAPIEvent.Type == slackevents.CallbackEvent { |
| 58 |
innerEvent := eventsAPIEvent.InnerEvent |
| 59 |
|
| 60 |
switch ev := innerEvent.Data.(type) { |
| 61 |
|
| 62 |
|
| 63 |
case *slackevents.WorkflowStepExecuteEvent: |
| 64 |
if ev.CallbackID == MyExampleWorkflowStepCallbackID { |
| 65 |
go doHeavyLoad(ev.WorkflowStep) |
| 66 |
|
| 67 |
w.WriteHeader(http.StatusOK) |
| 68 |
return |
| 69 |
} |
| 70 |
w.WriteHeader(http.StatusBadRequest) |
| 71 |
log.Printf("[WARN] unknown callbackID: %s", ev.CallbackID) |
| 72 |
return |
| 73 |
|
| 74 |
default: |
| 75 |
w.WriteHeader(http.StatusBadRequest) |
| 76 |
log.Printf("[WARN] unknown inner event type: %s", eventsAPIEvent.InnerEvent.Type) |
| 77 |
return |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
w.WriteHeader(http.StatusBadRequest) |
| 82 |
log.Printf("[WARN] unknown event type: %s", eventsAPIEvent.Type) |
| 83 |
} |
| 84 |
|
| 85 |
func handleInteraction(w http.ResponseWriter, r *http.Request) { |
| 86 |
if r.Method != http.MethodPost { |
| 87 |
w.WriteHeader(http.StatusMethodNotAllowed) |
| 88 |
return |
| 89 |
} |
| 90 |
|
| 91 |
body, err := ioutil.ReadAll(r.Body) |
| 92 |
if err != nil { |
| 93 |
w.WriteHeader(http.StatusBadRequest) |
| 94 |
return |
| 95 |
} |
| 96 |
|
| 97 |
jsonStr, err := url.QueryUnescape(string(body)[8:]) |
| 98 |
if err != nil { |
| 99 |
w.WriteHeader(http.StatusInternalServerError) |
| 100 |
return |
| 101 |
} |
| 102 |
|
| 103 |
var message slack.InteractionCallback |
| 104 |
if err := json.Unmarshal([]byte(jsonStr), &message); err != nil { |
| 105 |
log.Printf("[ERROR] Failed to decode json message from slack: %s", jsonStr) |
| 106 |
w.WriteHeader(http.StatusInternalServerError) |
| 107 |
return |
| 108 |
} |
| 109 |
|
| 110 |
switch message.Type { |
| 111 |
case slack.InteractionTypeWorkflowStepEdit: |
| 112 |
|
| 113 |
err := replyWithConfigurationView(message, "", "") |
| 114 |
if err != nil { |
| 115 |
log.Printf("[ERROR] Failed to open configuration modal in slack: %s", err.Error()) |
| 116 |
} |
| 117 |
|
| 118 |
case slack.InteractionTypeViewSubmission: |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
blockAction := message.View.State.Values |
| 124 |
selectedOption := blockAction[IDSelectOptionBlock][IDExampleSelectInput].SelectedOption.Value |
| 125 |
log.Println(fmt.Sprintf("user selected: %s", selectedOption)) |
| 126 |
|
| 127 |
in := &slack.WorkflowStepInputs{ |
| 128 |
IDExampleSelectInput: slack.WorkflowStepInputElement{ |
| 129 |
Value: selectedOption, |
| 130 |
SkipVariableReplacement: false, |
| 131 |
}, |
| 132 |
} |
| 133 |
|
| 134 |
err := saveUserSettingsForWorkflowStep(message.WorkflowStep.WorkflowStepEditID, in, nil) |
| 135 |
if err != nil { |
| 136 |
log.Printf("[ERROR] Failed on doing a POST request to workflows.updateStep: %s", err.Error()) |
| 137 |
w.WriteHeader(http.StatusInternalServerError) |
| 138 |
} |
| 139 |
|
| 140 |
default: |
| 141 |
log.Printf("[WARN] unknown message type: %s", message.Type) |
| 142 |
w.WriteHeader(http.StatusInternalServerError) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func replyWithConfigurationView(message slack.InteractionCallback, privateMetaData string, externalID string) error { |
| 147 |
headerText := slack.NewTextBlockObject("mrkdwn", "Hello World!\nThis is your workflow step app configuration view", false, false) |
| 148 |
headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 149 |
|
| 150 |
options := []*slack.OptionBlockObject{} |
| 151 |
options = append( |
| 152 |
options, |
| 153 |
slack.NewOptionBlockObject("one", slack.NewTextBlockObject("plain_text", "One", false, false), nil), |
| 154 |
) |
| 155 |
|
| 156 |
options = append( |
| 157 |
options, |
| 158 |
slack.NewOptionBlockObject("two", slack.NewTextBlockObject("plain_text", "Two", false, false), nil), |
| 159 |
) |
| 160 |
|
| 161 |
options = append( |
| 162 |
options, |
| 163 |
slack.NewOptionBlockObject("three", slack.NewTextBlockObject("plain_text", "Three", false, false), nil), |
| 164 |
) |
| 165 |
|
| 166 |
selection := slack.NewOptionsSelectBlockElement( |
| 167 |
"static_select", |
| 168 |
slack.NewTextBlockObject("plain_text", "your choice", false, false), |
| 169 |
IDExampleSelectInput, |
| 170 |
options..., |
| 171 |
) |
| 172 |
|
| 173 |
|
| 174 |
initialOption, ok := slack.GetInitialOptionFromWorkflowStepInput(selection, message.WorkflowStep.Inputs, options) |
| 175 |
if ok { |
| 176 |
selection.InitialOption = initialOption |
| 177 |
} |
| 178 |
|
| 179 |
inputBlock := slack.NewInputBlock( |
| 180 |
IDSelectOptionBlock, |
| 181 |
slack.NewTextBlockObject("plain_text", "Select an option", false, false), |
| 182 |
selection, |
| 183 |
) |
| 184 |
|
| 185 |
blocks := slack.Blocks{ |
| 186 |
BlockSet: []slack.Block{ |
| 187 |
headerSection, |
| 188 |
inputBlock, |
| 189 |
}, |
| 190 |
} |
| 191 |
|
| 192 |
cmr := slack.NewConfigurationModalRequest(blocks, privateMetaData, externalID) |
| 193 |
_, err := appCtx.slack.OpenView(message.TriggerID, cmr.ModalViewRequest) |
| 194 |
return err |
| 195 |
} |
| 196 |
|
| 197 |
func saveUserSettingsForWorkflowStep(workflowStepEditID string, inputs *slack.WorkflowStepInputs, outputs *[]slack.WorkflowStepOutput) error { |
| 198 |
return appCtx.slack.SaveWorkflowStepConfiguration(workflowStepEditID, inputs, outputs) |
| 199 |
} |
| 200 |
|
| 201 |
func doHeavyLoad(workflowStep slackevents.EventWorkflowStep) { |
| 202 |
|
| 203 |
log.Printf("Inputs:") |
| 204 |
for name, input := range *workflowStep.Inputs { |
| 205 |
log.Printf(fmt.Sprintf("%s: %s", name, input.Value)) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
time.Sleep(10 * time.Second) |
| 210 |
log.Println("Done") |
| 211 |
} |