Skip to content

This package provides golang support for SendBird's API

License

Notifications You must be signed in to change notification settings

tomMoulard/sendbird-go

Repository files navigation

sendbird-go

Keep a Changelog GitHub Release Go Reference go.mod LICENSE Build Status Go Report Card

Yet another go client for the Sendbird chat API.

Star this repository if you find it valuable and worth maintaining.

👁 Watch this repository to get notified about new releases, issues, etc.

User Guide

Installation

go get github.com/tomMoulard/sendbird-go

Usage

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/tomMoulard/sendbird-go/pkg/client"
    "github.com/tomMoulard/sendbird-go/pkg/user"
)

func main() {
    opts := []client.Option{
        client.WithAPPID(os.Getenv("SENDBIRD_APP_ID")),
        client.WithAPIToken(os.Getenv("SENDBIRD_API_KEY")),
    }
    client := client.NewClient(opts...)

    // To create a user
    userClient := user.NewUser(client)

    u := user.CreateUserRequest{
        UserID: "user-id",
        Nickname: "nickname",
    }
    createdUser, err := userClient.Create(context.Background(), u)
    if err != nil {
        if errors.Is(err, client.ErrTooManyRequests) {
            log.Fatalf("rate limit exceeded: %v", err)
            return
        }
        log.Fatalf("failed to create user: %v", err)
    }
    fmt.Printf("created user: %v\n", createdUser)
}

See available methods in the corresponding package documentation.

Usage in tests

See the source for the full example.

// iencli wraps the message.Message interface.
type iencli struct {
	message message.Message
}

// SendMessage wraps the sends message method.
func (c *iencli) SendMessage(ctx context.Context, channelType message.ChannelType, channelURL string, sendMessageRequest message.SendMessageRequest) (*message.SendMessageResponse, error) {
	got, err := c.message.SendMessage(ctx, channelType, channelURL, sendMessageRequest)
	if err != nil {
		return nil, fmt.Errorf("failed to send message: %w", err)
	}

	return got, nil
}

func TestSendMessage(t *testing.T) {
	t.Parallel()

	req := message.SendMessageRequest{
		Message: "hello",
	}
	messageMock := message.NewMessageMock(t).
		OnSendMessage(message.ChannelTypeGroup, "channelURL", req).TypedReturns(&message.SendMessageResponse{}, nil).Once().
		Parent

	c := &iencli{
		message: messageMock,
	}

	_, err := c.SendMessage(context.Background(), message.ChannelTypeGroup, "channelURL", req)
	if err != nil {
		t.Fatalf("failed to send message: %v", err)
	}
}

Build

Terminal

  • make - execute the build pipeline.
  • make help - print help for the Make targets.

Contributing

Feel free to create an issue or propose a pull request.

Follow the Code of Conduct.