Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] function execution events accept interfaces not strings #1357

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"fmt"
"os"

"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"os"
)

func main() {
Expand Down Expand Up @@ -38,7 +39,7 @@ func main() {
if callbackID == "sample_function" {
userId := ev.Inputs["user_id"]
payload := map[string]string{
"user_id": userId,
"user_id": userId.(string),
}

err := api.FunctionCompleteSuccess(ev.FunctionExecutionID, slack.FunctionCompleteSuccessRequestOptionOutput(payload))
Expand Down
10 changes: 5 additions & 5 deletions slackevents/inner_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,11 @@ type FunctionExecutedEvent struct {
DateUpdated int64 `json:"date_updated"`
DateDeleted int64 `json:"date_deleted"`
} `json:"function"`
Inputs map[string]string `json:"inputs"`
FunctionExecutionID string `json:"function_execution_id"`
WorkflowExecutionID string `json:"workflow_execution_id"`
EventTs string `json:"event_ts"`
BotAccessToken string `json:"bot_access_token"`
Inputs map[string]interface{} `json:"inputs"`
FunctionExecutionID string `json:"function_execution_id"`
WorkflowExecutionID string `json:"workflow_execution_id"`
EventTs string `json:"event_ts"`
BotAccessToken string `json:"bot_access_token"`
}

type InviteRequestedEvent struct {
Expand Down
57 changes: 56 additions & 1 deletion slackevents/inner_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2546,12 +2546,33 @@ func TestFunctionExecutedEvent(t *testing.T) {
"description": "Runs sample function",
"type": "app",
"input_parameters": [
{
"type": "slack#/types/message_context",
"name": "message_context",
"description": "",
"title": "Message Context",
"is_required": true
},
{
"type": "slack#/types/user_id",
"name": "user_id",
"description": "Message recipient",
"title": "User",
"is_required": true
},
{
"type": "integer",
"name": "timestamp",
"description": "Timestamp of the event",
"title": "Timestamp",
"is_required": true
},
{
"type": "boolean",
"name": "enabled",
"description": "Indicates if the feature is enabled",
"title": "Enabled",
"is_required": true
}
],
"output_parameters": [
Expand All @@ -2568,13 +2589,32 @@ func TestFunctionExecutedEvent(t *testing.T) {
"date_updated": 1698947481,
"date_deleted": 0
},
"inputs": { "user_id": "USER12345678" },
"inputs": {
"user_id": "USER12345678",
"timestamp": 1698947481,
"enabled": true,
"message_context": {
"channel_id": "C0123456789",
"message_ts": "1733331835.871019"
}
},
"function_execution_id": "Fx1234567O9L",
"workflow_execution_id": "WxABC123DEF0",
"event_ts": "1698958075.998738",
"bot_access_token": "abcd-1325532282098-1322446258629-6123648410839-527a1cab3979cad288c9e20330d212cf"
}`

type MessageContext struct {
ChannelId string `json:"channel_id"`
MessageTs string `json:"message_ts"`
}
type TestInputs struct {
UserId string `json:"user_id"`
Timestamp int `json:"timestamp"`
Enabled bool `json:"enabled"`
Context MessageContext `json:"message_context"`
}

var event FunctionExecutedEvent
if err := json.Unmarshal([]byte(jsonStr), &event); err != nil {
t.Errorf("Failed to unmarshal FunctionExecutedEvent: %v", err)
Expand All @@ -2591,6 +2631,21 @@ func TestFunctionExecutedEvent(t *testing.T) {
if event.FunctionExecutionID != "Fx1234567O9L" {
t.Fail()
}

inputStr, err := json.Marshal(event.Inputs)
if err != nil {
t.Errorf("Failed to marshal Inputs of FunctionExecutedEvent: %v", err)
}
testInputs := new(TestInputs)
err = json.Unmarshal(inputStr, testInputs)
if err != nil {
t.Errorf("Failed to unmarshal Inputs of FunctionExecutedEvent: %v", err)
}
assert.Equal(t, "USER12345678", testInputs.UserId)
assert.Equal(t, 1698947481, testInputs.Timestamp)
assert.True(t, testInputs.Enabled)
assert.Equal(t, "C0123456789", testInputs.Context.ChannelId)
assert.Equal(t, "1733331835.871019", testInputs.Context.MessageTs)
}

func TestInviteRequestedEvent(t *testing.T) {
Expand Down