2018-08-09 15:38:19 -07:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2018-10-07 14:17:46 -07:00
|
|
|
// 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 textfield 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 15:38:19 -07:00
|
|
|
type DialogTrigger struct {
|
2018-10-07 14:17:46 -07:00
|
|
|
TriggerID string `json:"trigger_id"` //Required. Must respond within 3 seconds.
|
2018-08-09 15:38:19 -07:00
|
|
|
Dialog Dialog `json:"dialog"` //Required.
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:17:46 -07:00
|
|
|
// Dialog as in Slack dialogs
|
|
|
|
// https://api.slack.com/dialogs#option_element_attributes#top-level_dialog_attributes
|
2018-08-09 15:38:19 -07:00
|
|
|
type Dialog struct {
|
2018-10-07 14:17:46 -07:00
|
|
|
TriggerID string `json:"trigger_id"` //Required
|
|
|
|
CallbackID string `json:"callback_id"` //Required
|
|
|
|
Title string `json:"title"`
|
|
|
|
SubmitLabel string `json:"submit_label,omitempty"`
|
|
|
|
NotifyOnCancel bool `json:"notify_on_cancel"`
|
|
|
|
Elements []DialogElement `json:"elements"`
|
2018-08-09 15:38:19 -07:00
|
|
|
}
|
|
|
|
|
2018-10-07 14:17:46 -07:00
|
|
|
// DialogElement abstract type for dialogs.
|
2018-08-09 15:38:19 -07:00
|
|
|
type DialogElement interface{}
|
|
|
|
|
|
|
|
// DialogCallback is sent from Slack when a user submits a form from within a dialog
|
|
|
|
type DialogCallback struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
CallbackID string `json:"callback_id"`
|
|
|
|
Team Team `json:"team"`
|
|
|
|
Channel Channel `json:"channel"`
|
|
|
|
User User `json:"user"`
|
|
|
|
ActionTs string `json:"action_ts"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
ResponseURL string `json:"response_url"`
|
|
|
|
Submission map[string]string `json:"submission"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialogSuggestionCallback is sent from Slack when a user types in a select field with an external data source
|
|
|
|
type DialogSuggestionCallback struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
ActionTs string `json:"action_ts"`
|
|
|
|
Team Team `json:"team"`
|
|
|
|
User User `json:"user"`
|
|
|
|
Channel Channel `json:"channel"`
|
|
|
|
ElementName string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
CallbackID string `json:"callback_id"`
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:17:46 -07: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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 15:38:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenDialogContext opens a dialog window where the triggerId originated from with a custom context
|
2018-10-07 14:17:46 -07:00
|
|
|
// 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 15:38:19 -07:00
|
|
|
return errors.New("received empty parameters")
|
|
|
|
}
|
|
|
|
|
2018-10-07 14:17:46 -07:00
|
|
|
req := DialogTrigger{
|
|
|
|
TriggerID: triggerID,
|
2018-08-09 15:38:19 -07:00
|
|
|
Dialog: dialog,
|
|
|
|
}
|
2018-10-07 14:17:46 -07:00
|
|
|
|
|
|
|
encoded, err := json.Marshal(req)
|
2018-08-09 15:38:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-07 14:17:46 -07:00
|
|
|
|
|
|
|
response := &DialogOpenResponse{}
|
2018-08-09 15:38:19 -07:00
|
|
|
endpoint := SLACK_API + "dialog.open"
|
2018-10-07 14:17:46 -07:00
|
|
|
if err := postJSON(ctx, api.httpclient, endpoint, api.token, encoded, response, api.debug); err != nil {
|
2018-08-09 15:38:19 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Err()
|
|
|
|
}
|