rocketbot

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Rocket.chat-client

Go-библиотека для создания ботов в Rocket.Chat через WebSocket (DDP)`.

  • подключение к Rocket.Chat по WebSocket (/websocket);
  • авторизация бота через resume token (AuthToken);
  • подписка на сообщения комнаты (stream-room-messages);
  • отправка сообщений в комнату (sendMessage);
  • обработка входящих сообщений через callback.

Для работы бота нужны:

  • ServerURL — базовый адрес Rocket.Chat, например https://chat.example.com;
  • UserID — ID пользователя бота;
  • AuthToken — токен авторизации пользователя бота (можно использовать Personal Access Token);
  • roomID — ID комнаты/канала, где бот слушает и/или отправляет сообщения.

Пример создания клиента:

import 	rocketbot "github.com/motylkov/Rocket.chat-client"

bot := rocketbot.NewClient(rocketbot.Config{
	ServerURL: "https://chat.example.com",
	UserID:    "your-bot-user-id",
	AuthToken: "your-bot-auth-token",
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotConnected is returned when an operation requires an active connection.
	ErrNotConnected = errors.New("rocketbot: not connected")
)

Functions

This section is empty.

Types

type AutoSubscribeOptions added in v0.2.0

type AutoSubscribeOptions struct {
	// Optional static room IDs to subscribe once on startup.
	StaticRoomIDs []string
	// Poll interval for discovering new room subscriptions.
	Interval time.Duration
	// Optional callback called for each successfully subscribed room id.
	OnSubscribed func(roomID string)
	// Optional callback for auto-subscribe/discovery errors.
	OnError func(error)
	// Optional callback after each discovery pass.
	OnDiscovered func(roomCount int)
}

AutoSubscribeOptions controls background room auto-subscription.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a DDP client for building Rocket.Chat bots.

func NewClient

func NewClient(cfg Config) *Client

NewClient creates a new bot client.

func (*Client) Close

func (c *Client) Close(code websocket.StatusCode, reason string) error

Close closes websocket connection.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect opens websocket, performs DDP connect and bot login (resume token auth).

func (*Client) ListSubscribedRoomIDs added in v0.2.0

func (c *Client) ListSubscribedRoomIDs(ctx context.Context) ([]string, error)

ListSubscribedRoomIDs returns current room ids from /api/v1/subscriptions.get.

func (*Client) OnMessage

func (c *Client) OnMessage(handler func(context.Context, Message))

OnMessage sets callback invoked on each room message event.

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, roomID, text string) error

SendMessage sends a plain text message to room/channel.

func (*Client) StartAutoSubscribeRooms added in v0.2.0

func (c *Client) StartAutoSubscribeRooms(ctx context.Context, opts AutoSubscribeOptions)

StartAutoSubscribeRooms starts background room subscription management.

func (*Client) SubscribeRoomMessages

func (c *Client) SubscribeRoomMessages(ctx context.Context, roomID string) error

SubscribeRoomMessages subscribes to message stream for a specific room/channel.

func (*Client) ValidateAuth added in v0.2.0

func (c *Client) ValidateAuth(ctx context.Context) error

ValidateAuth verifies X-User-Id and X-Auth-Token against /api/v1/me.

type Config

type Config struct {
	// ServerURL is your Rocket.Chat base URL, for example: https://chat.example.com
	ServerURL string

	// UserID is the user id of bot account (X-User-Id / userId).
	UserID string

	// AuthToken is auth token for bot account (X-Auth-Token / resume token).
	AuthToken string

	// Optional custom HTTP client used by websocket dialer.
	HTTPClient *http.Client
}

Config contains parameters needed to connect to Rocket.Chat over WebSocket (DDP).

type Message

type Message struct {
	ID       string
	RoomID   string
	Text     string
	SenderID string
	Raw      json.RawMessage
}

Message represents a chat message event from Rocket.Chat stream-room-messages.

Directories

Path Synopsis
examples
1 command
2 command
3 command

Jump to

Keyboard shortcuts

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