knock-go

module
v0.1.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2024 License: MIT

README

Knock Go Library

Knock API access for applications written in Go.

Documentation

See the documentation for Go usage examples.

Installation

go get github.com/knocklabs/knock-go/knock

Configuration

To use the library you must provide a secret API key, provided in the Knock dashboard. You must pass it into the client:

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/knocklabs/knock-go/knock"
)

func main() {
    token := os.Getenv("KNOCK_TOKEN")

    ctx := context.Background()

    // create a new Knock API client with the given access token
    client, _ := knock.NewClient(
        knock.WithAccessToken(token),
    )

    // any example code from the rest of the readme can be run here

Identifying Users
user, _ := client.Users.Identify(ctx, &knock.IdentifyUserRequest{
    ID:   "fun-user2",
    Name: "John Hammond",
    CustomProperties: map[string]interface{}{
        "welcome":     "to jurassic park",
        "middle-name": "alfred",
    },
})
fmt.Printf("user-name: %+v\n", user)
Sending notifies (triggering workflows)

Simple trigger for one recipient by id:

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByID("tim")
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger with inline-identified recipient:

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByEntity(map[string]interface{}{
    "id":    "dnedry",
    "name":  "Dennis",
    "email": "nedry@ingen.io",
})
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger for multiple recipients and an object

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}

for _, r := range []string{"tim", "hammond"} {
    req.AddRecipientByID(r)
}

req.AddRecipientByEntity(map[string]interface{}{
    "id": "group-a",
    "collection": "groups",
})
workflow, _ := client.Workflows.Trigger(ctx, req, nil)
fmt.Printf("workflow: %+v\n", workflow)

Trigger with idempotency key

req := &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
}
req.AddRecipientByID("tim")
workflow, _ := client.Workflows.Trigger(ctx, req, &knock.MethodOptions{
    IdempotencyKey: "an-idempotency-key",
})
fmt.Printf("workflow: %+v\n", workflow)
Retrieving users
user, _ = client.Users.Get(ctx, &knock.GetUserRequest{
    ID: "fun-user",
})

fmt.Printf("retrieved-user: %+v\n", user)
Deleting users
client.Users.Delete(ctx, &knock.DeleteUserRequest{
    ID: "fun-user",
})
Getting and setting channel data
setUserChannelDataResponse, _ := client.Users.SetChannelData(ctx, &knock.SetUserChannelDataRequest{
    UserID:    "test-123",
    ChannelID: "5d2377a0-92fb-4616-8315-eee843556566",
    Data: map[string]interface{}{
        "tokens": []string{"a", "b"},
    },
})
fmt.Printf("channel-data-set: %+v\n", setUserChannelDataResponse)

getUserChannelDataResponse, _ := client.Users.GetChannelData(ctx, &knock.GetUserChannelDataRequest{
    UserID:    "test-123",
    ChannelID: "5d2377a0-92fb-4616-8315-eee843556566",
})
fmt.Printf("channel-data-found: %+v\n", getUserChannelDataResponse)
Canceling workflows
client.Workflows.Cancel(ctx, &knock.CancelWorkflowRequest{
    Workflow:        "test",
    CancellationKey: "user-123",
})
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL