-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
function_execute.go
93 lines (76 loc) · 2.69 KB
/
function_execute.go
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
package slack
import (
"context"
"encoding/json"
)
type (
FunctionCompleteSuccessRequest struct {
FunctionExecutionID string `json:"function_execution_id"`
Outputs map[string]string `json:"outputs"`
}
FunctionCompleteErrorRequest struct {
FunctionExecutionID string `json:"function_execution_id"`
Error string `json:"error"`
}
)
type FunctionCompleteSuccessRequestOption func(opt *FunctionCompleteSuccessRequest) error
func FunctionCompleteSuccessRequestOptionOutput(outputs map[string]string) FunctionCompleteSuccessRequestOption {
return func(opt *FunctionCompleteSuccessRequest) error {
if len(outputs) > 0 {
opt.Outputs = outputs
}
return nil
}
}
// FunctionCompleteSuccess indicates function is completed
func (api *Client) FunctionCompleteSuccess(functionExecutionId string, options ...FunctionCompleteSuccessRequestOption) error {
return api.FunctionCompleteSuccessContext(context.Background(), functionExecutionId, options...)
}
// FunctionCompleteSuccess indicates function is completed
func (api *Client) FunctionCompleteSuccessContext(ctx context.Context, functionExecutionId string, options ...FunctionCompleteSuccessRequestOption) error {
// More information: https://api.slack.com/methods/functions.completeSuccess
r := &FunctionCompleteSuccessRequest{
FunctionExecutionID: functionExecutionId,
}
for _, option := range options {
option(r)
}
endpoint := api.endpoint + "functions.completeSuccess"
jsonData, err := json.Marshal(r)
if err != nil {
return err
}
response := &SlackResponse{}
if err := postJSON(ctx, api.httpclient, endpoint, api.token, jsonData, response, api); err != nil {
return err
}
if !response.Ok {
return response.Err()
}
return nil
}
// FunctionCompleteError indicates function is completed with error
func (api *Client) FunctionCompleteError(functionExecutionID string, errorMessage string) error {
return api.FunctionCompleteErrorContext(context.Background(), functionExecutionID, errorMessage)
}
// FunctionCompleteErrorContext indicates function is completed with error
func (api *Client) FunctionCompleteErrorContext(ctx context.Context, functionExecutionID string, errorMessage string) error {
// More information: https://api.slack.com/methods/functions.completeError
r := FunctionCompleteErrorRequest{
FunctionExecutionID: functionExecutionID,
}
r.Error = errorMessage
endpoint := api.endpoint + "functions.completeError"
jsonData, err := json.Marshal(r)
if err != nil {
return err
}
response := &SlackResponse{}
if err := postJSON(ctx, api.httpclient, endpoint, api.token, jsonData, response, api); err != nil {
return err
}
if !response.Ok {
return response.Err()
}
return nil
}