forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.go
More file actions
74 lines (64 loc) · 1.79 KB
/
function.go
File metadata and controls
74 lines (64 loc) · 1.79 KB
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
package main
import (
"fmt"
"os"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
)
func main() {
// Get tokens from environment variables
botToken := os.Getenv("SLACK_BOT_TOKEN")
if botToken == "" {
fmt.Println("SLACK_BOT_TOKEN environment variable is required")
os.Exit(1)
}
appToken := os.Getenv("SLACK_APP_TOKEN")
if appToken == "" {
fmt.Println("SLACK_APP_TOKEN environment variable is required")
os.Exit(1)
}
api := slack.New(
botToken,
slack.OptionDebug(true),
slack.OptionAppLevelToken(appToken),
)
client := socketmode.New(api, socketmode.OptionDebug(true))
go func() {
for evt := range client.Events {
switch evt.Type {
case socketmode.EventTypeEventsAPI:
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
fmt.Printf("Ignored %+v\n", evt)
continue
}
fmt.Printf("Event received: %+v\n", eventsAPIEvent)
client.Ack(*evt.Request)
switch eventsAPIEvent.Type {
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.FunctionExecutedEvent:
callbackID := ev.Function.CallbackID
if callbackID == "sample_function" {
userId := ev.Inputs["user_id"]
payload := map[string]string{
"user_id": userId.(string),
}
err := api.FunctionCompleteSuccess(ev.FunctionExecutionID, slack.FunctionCompleteSuccessRequestOptionOutput(payload))
if err != nil {
fmt.Printf("failed posting message: %v \n", err)
}
}
}
default:
client.Debugf("unsupported Events API event received\n")
}
default:
fmt.Fprintf(os.Stderr, "Unexpected event type received: %s\n", evt.Type)
}
}
}()
client.Run()
}