knock-go

module
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: May 2, 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)
workflow, _ := client.Workflows.Trigger(ctx, &knock.TriggerWorkflowRequest{
    Workflow:   "test",
    Recipients: []string{"tim", "lex"},
    Data: map[string]interface{}{
        "life":      "found a way",
        "dinosaurs": "loose",
    },
})
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