5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-12-27 07:45:40 +00:00
matterbridge/vendor/github.com/nlopes/slack/dialog.go

113 lines
3.5 KiB
Go
Raw Normal View History

2018-08-09 22:38:19 +00:00
package slack
import (
"context"
"encoding/json"
"errors"
)
// InputType is the type of the dialog input type
type InputType string
const (
// InputTypeText textfield input
InputTypeText InputType = "text"
// InputTypeTextArea textarea input
InputTypeTextArea InputType = "textarea"
// InputTypeSelect select menus input
InputTypeSelect InputType = "select"
)
// DialogInput for dialogs input type text or menu
type DialogInput struct {
Type InputType `json:"type"`
Label string `json:"label"`
Name string `json:"name"`
Placeholder string `json:"placeholder"`
Optional bool `json:"optional"`
}
// DialogTrigger ...
2018-08-09 22:38:19 +00:00
type DialogTrigger struct {
TriggerID string `json:"trigger_id"` //Required. Must respond within 3 seconds.
2018-08-09 22:38:19 +00:00
Dialog Dialog `json:"dialog"` //Required.
}
// Dialog as in Slack dialogs
// https://api.slack.com/dialogs#option_element_attributes#top-level_dialog_attributes
2018-08-09 22:38:19 +00:00
type Dialog struct {
2018-12-01 18:55:35 +00:00
TriggerID string `json:"trigger_id"` // Required
CallbackID string `json:"callback_id"` // Required
State string `json:"state,omitempty"` // Optional
Title string `json:"title"`
SubmitLabel string `json:"submit_label,omitempty"`
NotifyOnCancel bool `json:"notify_on_cancel"`
Elements []DialogElement `json:"elements"`
2018-08-09 22:38:19 +00:00
}
// DialogElement abstract type for dialogs.
2018-08-09 22:38:19 +00:00
type DialogElement interface{}
2018-12-01 18:55:35 +00:00
// DialogCallback DEPRECATED use InteractionCallback
type DialogCallback InteractionCallback
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
// DialogSubmissionCallback is sent from Slack when a user submits a form from within a dialog
type DialogSubmissionCallback struct {
State string `json:"state,omitempty"`
Submission map[string]string `json:"submission"`
2018-08-09 22:38:19 +00:00
}
// DialogOpenResponse response from `dialog.open`
type DialogOpenResponse struct {
SlackResponse
DialogResponseMetadata DialogResponseMetadata `json:"response_metadata"`
}
// DialogResponseMetadata lists the error messages
type DialogResponseMetadata struct {
Messages []string `json:"messages"`
}
// DialogInputValidationError is an error when user inputs incorrect value to form from within a dialog
type DialogInputValidationError struct {
Name string `json:"name"`
Error string `json:"error"`
}
// DialogInputValidationErrors lists the name of field and that error messages
type DialogInputValidationErrors struct {
Errors []DialogInputValidationError `json:"errors"`
}
// OpenDialog opens a dialog window where the triggerID originated from.
// EXPERIMENTAL: dialog functionality is currently experimental, api is not considered stable.
func (api *Client) OpenDialog(triggerID string, dialog Dialog) (err error) {
return api.OpenDialogContext(context.Background(), triggerID, dialog)
2018-08-09 22:38:19 +00:00
}
// OpenDialogContext opens a dialog window where the triggerId originated from with a custom context
// EXPERIMENTAL: dialog functionality is currently experimental, api is not considered stable.
func (api *Client) OpenDialogContext(ctx context.Context, triggerID string, dialog Dialog) (err error) {
if triggerID == "" {
2018-08-09 22:38:19 +00:00
return errors.New("received empty parameters")
}
req := DialogTrigger{
TriggerID: triggerID,
2018-08-09 22:38:19 +00:00
Dialog: dialog,
}
encoded, err := json.Marshal(req)
2018-08-09 22:38:19 +00:00
if err != nil {
return err
}
response := &DialogOpenResponse{}
2018-12-01 18:55:35 +00:00
endpoint := APIURL + "dialog.open"
if err := postJSON(ctx, api.httpclient, endpoint, api.token, encoded, response, api); err != nil {
2018-08-09 22:38:19 +00:00
return err
}
return response.Err()
}