-
Notifications
You must be signed in to change notification settings - Fork 145
/
main.go
119 lines (92 loc) · 2.16 KB
/
main.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
_ "embed"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/client/types"
"github.com/hatchet-dev/hatchet/pkg/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/integrations/slack"
"github.com/hatchet-dev/hatchet/pkg/worker"
)
type teamCreateEvent struct {
Name string `json:"name"`
}
//go:embed .hatchet/slack-channel.yaml
var SlackChannelWorkflow []byte
func init() {
err := godotenv.Load()
if err != nil {
panic(err)
}
// initialize the slack channel workflow with SLACK_USER_ID
slackUserId := os.Getenv("SLACK_USER_ID")
if slackUserId == "" {
panic("SLACK_USER_ID environment variable must be set")
}
slackFileWithReplacedEnv := strings.Replace(string(SlackChannelWorkflow), "$SLACK_USER_ID", slackUserId, 1)
SlackChannelWorkflow = []byte(slackFileWithReplacedEnv)
}
func main() {
// read the slack workflow
slackWorkflowFile, err := types.ParseYAML(context.Background(), SlackChannelWorkflow)
if err != nil {
panic(err)
}
// render the slack workflow using the environment variable SLACK_USER_ID
slackToken := os.Getenv("SLACK_TOKEN")
slackTeamId := os.Getenv("SLACK_TEAM_ID")
if slackToken == "" {
panic("SLACK_TOKEN environment variable must be set")
}
if slackTeamId == "" {
panic("SLACK_TEAM_ID environment variable must be set")
}
slackInt := slack.NewSlackIntegration(slackToken, slackTeamId, true)
client, err := client.New(
client.InitWorkflows(),
client.WithWorkflows([]*types.Workflow{
&slackWorkflowFile,
}),
)
if err != nil {
panic(err)
}
worker, err := worker.NewWorker(
worker.WithClient(
client,
),
worker.WithIntegration(
slackInt,
),
)
if err != nil {
panic(err)
}
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
defer cancel()
go worker.Start()
testEvent := teamCreateEvent{
Name: "test-team-2",
}
// push an event
err = client.Event().Push(
context.Background(),
"team:create",
testEvent,
)
if err != nil {
panic(err)
}
for {
select {
case <-interruptCtx.Done():
return
default:
time.Sleep(time.Second)
}
}
}