PLT-1649: add response_url support for custom slash commands (#6739)
* add response_url support for custom slash commands * pr suggestions * pr update / suggestion * test fix
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
5cd45c9394
Коммит
b122381e87
@@ -45,10 +45,9 @@ func CreateCommandPost(post *model.Post, teamId string, response *model.CommandR
|
||||
parseSlackAttachment(post, response.Attachments)
|
||||
}
|
||||
|
||||
switch response.ResponseType {
|
||||
case model.COMMAND_RESPONSE_TYPE_IN_CHANNEL:
|
||||
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
|
||||
return CreatePost(post, teamId, true)
|
||||
case model.COMMAND_RESPONSE_TYPE_EPHEMERAL:
|
||||
} else if response.ResponseType == "" || response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL {
|
||||
if response.Text == "" {
|
||||
return post, nil
|
||||
}
|
||||
@@ -196,7 +195,12 @@ func ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.App
|
||||
|
||||
p.Set("command", "/"+trigger)
|
||||
p.Set("text", message)
|
||||
p.Set("response_url", "not supported yet")
|
||||
|
||||
if hook, err := CreateCommandWebhook(cmd.Id, args); err != nil {
|
||||
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
p.Set("response_url", args.SiteURL+"/hooks/commands/"+hook.Id)
|
||||
}
|
||||
|
||||
method := "POST"
|
||||
if cmd.Method == model.COMMAND_METHOD_GET {
|
||||
@@ -213,7 +217,7 @@ func ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.App
|
||||
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
response := model.CommandResponseFromJson(resp.Body)
|
||||
response := model.CommandResponseFromHTTPBody(resp.Header.Get("Content-Type"), resp.Body)
|
||||
if response == nil {
|
||||
return nil, model.NewAppError("command", "api.command.execute_command.failed_empty.app_error", map[string]interface{}{"Trigger": trigger}, "", http.StatusInternalServerError)
|
||||
} else {
|
||||
|
||||
@@ -533,3 +533,54 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest) *mo
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateCommandWebhook(commandId string, args *model.CommandArgs) (*model.CommandWebhook, *model.AppError) {
|
||||
hook := &model.CommandWebhook{
|
||||
CommandId: commandId,
|
||||
UserId: args.UserId,
|
||||
ChannelId: args.ChannelId,
|
||||
RootId: args.RootId,
|
||||
ParentId: args.ParentId,
|
||||
}
|
||||
|
||||
if result := <-Srv.Store.CommandWebhook().Save(hook); result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return result.Data.(*model.CommandWebhook), nil
|
||||
}
|
||||
}
|
||||
|
||||
func HandleCommandWebhook(hookId string, response *model.CommandResponse) *model.AppError {
|
||||
if response == nil {
|
||||
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var hook *model.CommandWebhook
|
||||
if result := <-Srv.Store.CommandWebhook().Get(hookId); result.Err != nil {
|
||||
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
|
||||
} else {
|
||||
hook = result.Data.(*model.CommandWebhook)
|
||||
}
|
||||
|
||||
var cmd *model.Command
|
||||
if result := <-Srv.Store.Command().Get(hook.CommandId); result.Err != nil {
|
||||
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.command.app_error", nil, "err="+result.Err.Message, http.StatusBadRequest)
|
||||
} else {
|
||||
cmd = result.Data.(*model.Command)
|
||||
}
|
||||
|
||||
args := &model.CommandArgs{
|
||||
UserId: hook.UserId,
|
||||
ChannelId: hook.ChannelId,
|
||||
TeamId: cmd.TeamId,
|
||||
RootId: hook.RootId,
|
||||
ParentId: hook.ParentId,
|
||||
}
|
||||
|
||||
if result := <-Srv.Store.CommandWebhook().TryUse(hook.Id, 5); result.Err != nil {
|
||||
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
|
||||
}
|
||||
|
||||
_, err := HandleCommandResponse(cmd, args, response, false)
|
||||
return err
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user