-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
70 lines (59 loc) · 1.72 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/coze-dev/coze-go"
)
func main() {
// Get an access_token through personal access token or oauth.
token := os.Getenv("COZE_API_TOKEN")
authCli := coze.NewTokenAuth(token)
// Init the Coze client through the access_token.
cozeCli := coze.NewCozeAPI(authCli, coze.WithBaseURL(os.Getenv("COZE_API_BASE")))
ctx := context.Background()
workflowID := os.Getenv("WORKFLOW_ID")
// if your workflow need input params, you can send them by map
data := map[string]interface{}{
"date": "param values",
}
req := &coze.RunWorkflowsReq{
WorkflowID: workflowID,
Parameters: data,
IsAsync: true, // if you want the workflow runs asynchronously, you must set isAsync to true.
}
resp, err := cozeCli.Workflows.Runs.Create(ctx, req)
if err != nil {
fmt.Println("Error running workflow:", err)
return
}
fmt.Println("Start async workflow runs:", resp.ExecuteID)
fmt.Println(resp.LogID())
executeID := resp.ExecuteID
isFinished := false
for !isFinished {
historyResp, err := cozeCli.Workflows.Runs.Histories.Retrieve(ctx, &coze.RetrieveWorkflowsRunsHistoriesReq{
WorkflowID: workflowID,
ExecuteID: executeID,
})
if err != nil {
fmt.Println("Error retrieving history:", err)
return
}
fmt.Println(historyResp)
fmt.Println(historyResp.LogID())
history := historyResp.Histories[0]
switch history.ExecuteStatus {
case coze.WorkflowExecuteStatusFail:
fmt.Println("Workflow runs failed, reason:", history.ErrorMessage)
isFinished = true
case coze.WorkflowExecuteStatusRunning:
fmt.Println("Workflow runs is running")
time.Sleep(time.Second)
default:
fmt.Println("Workflow runs success:", history.Output)
isFinished = true
}
}
}