Categorygithub.com/tensved/bobrix
modulepackage
0.0.9
Repository: https://github.com/tensved/bobrix.git
Documentation: pkg.go.dev

# README

Go Reference Go Report Card

BOBRIX (Bot Bridge Matrix) - Service Bridge for Matrix Bots

Overview

BOBRIX (Bot Bridge Matrix) is a Go library designed to facilitate interaction with various services through a Matrix client in the form of a bot. BoBRIX abstracts the complexity of integrating multiple services by providing a unified set of interaction contracts and a convenient layer on top of the Matrix client, enabling seamless service integration.

Features

  • Interaction Contracts: Define and manage interaction protocols with various services.
  • Matrix Client Bot: A high-level abstraction over the Matrix client to simplify bot development.
  • Service Integration: Combine interaction contracts and the bot framework to interact with multiple services directly from the Matrix client.

Getting started

Prerequisites

BoBRIX requires Go version 1.21 or above.

BoBRIX Installation

To install BoBRIX, use the following command:

go get -u github.com/tensved/bobrix

Usage

A basic example of creating bobrix Engine:

package main

import (
	"context"
	"github.com/tensved/bobrix"
	"github.com/tensved/bobrix/examples/ada"
	"github.com/tensved/bobrix/mxbot"
	"log/slog"
	"os"
	"os/signal"
	"syscall"
)

func main() {

	engine := bobrix.NewEngine()

	botCredentials := &mxbot.BotCredentials{
		Username:      os.Getenv("MX_BOT_USERNAME"),
		Password:      os.Getenv("MX_BOT_PASSWORD"),
		HomeServerURL: os.Getenv("MX_BOT_HOMESERVER_URL"),
	}
	adaBot, err := ada.NewAdaBot(botCredentials)
	if err != nil {
		panic(err)
	}

	engine.ConnectBot(adaBot)

	ctx := context.Background()

	go func() {
		if err := engine.Run(ctx); err != nil {
			panic(err)
		}
	}()

	go func() {
		ada := engine.Bots()[0]

		slog.Info("starting sync")

		sub := ada.Healthchecker.Subscribe()

		for data := range sub.Sync() {

			slog.Debug("healthcheck", "data", data)
		}

	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)

	<-quit

	if err := engine.Stop(ctx); err != nil {
		slog.Error("failed to stop engine", "error", err)
	}

	slog.Info("service shutdown")
}

A basic creating matrix bot:

package ada

import (
	"github.com/tensved/bobrix"
	"github.com/tensved/bobrix/contracts"
	"github.com/tensved/bobrix/mxbot"
	"log/slog"
)

func NewAdaBot(credentials *mxbot.BotCredentials) (*bobrix.Bobrix, error) {
	bot, err := mxbot.NewDefaultBot("ada", credentials)
	if err != nil {
		return nil, err
	}
	//
	bot.AddCommand(mxbot.NewCommand(
		"ping",
		func(c mxbot.CommandCtx) error {

			return c.TextAnswer("pong")
		}),
	)

	bot.AddEventHandler(
		mxbot.AutoJoinRoomHandler(bot),
	)

	bot.AddEventHandler(
		mxbot.NewLoggerHandler("ada"),
	)

	bobr := bobrix.NewBobrix(bot, bobrix.WithHealthcheck(bobrix.WithAutoSwitch()))

	bobr.SetContractParser(bobrix.DefaultContractParser())

	bobr.SetContractParser(bobrix.AutoRequestParser(&bobrix.AutoParserOpts{
		ServiceName: "ada",
		MethodName:  "generate",
		InputName:   "prompt",
	}))

	bobr.ConnectService(NewADAService("hilltwinssl.singularitynet.io"), func(ctx mxbot.Ctx, r *contracts.MethodResponse) {

		if r.Err != nil {
			slog.Error("failed to process request", "error", r.Err)

			if err := ctx.TextAnswer("error: " + r.Err.Error()); err != nil {
				slog.Error("failed to send message", "error", err)
			}

			return
		}

		answer, ok := r.GetString("answer")
		if !ok {
			answer = "I don't know"
		}

		err := ctx.TextAnswer(answer)

		if err != nil {
			slog.Error("failed to send message", "error", err)
		}
	})

	return bobr, nil
}

Basic example of Service Description:

package ada

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/websocket"
	"github.com/tensved/bobrix/contracts"
	"log/slog"
	"net/url"
)

func NewADAService(adaHost string) *contracts.Service {

	return &contracts.Service{
		Name:        "ada",
		Description: "Ada is an AI language model trained by OpenAI.",
		Methods: map[string]*contracts.Method{
			"generate": {
				Name:        "generate",
				Description: "Generate text using Ada's language model.",
				Inputs: []contracts.Input{
					{
						Name:        "prompt",
						Description: "The prompt to generate text from.",
						Type:        "text",
					},
					{
						Name:         "response_type",
						Type:         "text",
						DefaultValue: "text",
					},
					{
						Name: "audio",
						Type: "audio",
					},
				},
				Outputs: []contracts.Output{
					{
						Name:        "text",
						Description: "The generated text.",
					},
				},
				Handler: NewADAHandler(adaHost),
			},
		},
		Pinger: contracts.NewWSPinger(
			contracts.WSOptions{Host: adaHost, Path: "/", Schema: "wss"},
		),
	}
}

For a more detailed description see Service Example

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AudioMessageContractParser - audio message contract parser It is required to specify strictly the name of the service and method, as well as the name for Input, because when sending a voice message there is no possibility to specify parameters by text.
AutoRequestParser - auto request parser It is convenient to use it in cases when you need to handle situations when a user sends a message that should be sent immediately by a request.
BobrixContractParser - master contract parser It is used for parsing a request from the bot Request should be in the form of a JSON object with the following structure (ServiceRequest) And it should have a tag "bobrix.prompt" in event.Content.
No description provided by the author
DefaultContractParser - default contract parser it parses message text and extracts bot name, service name and method name it used specific regular expression for parsing it returns nil if message doesn't match the pattern message pattern example: @{botname} -service:{servicename} -method:{methodname} -{input1}:"{inputvalue1}" -{input2}:"{inputvalue2}".
ImageMessageContractParser - image message contract parser It is required to specify strictly the name of the service and method, as well as the name for Input because when sending an image there is no possibility to specify parameters by text.
NewBobrix - Bobrix constructor.
No description provided by the author
NewHealthcheck - healthcheck constructor bobrix - reference to the bot opts - options for healthcheck.
NewSubscriber - creates new subscriber.
WithAutoSwitch - turn on option to switch offline/online status for services automatically based on health.
No description provided by the author
WithInterval - set healthcheck interval.

# Constants

No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
Bobrix - bot structure It is a connection structure between two components: it manages the bot and service contracts.
BobrixService - service for bot Binds service and adds handler for processing.
BobrixStatus - status of the bot.
DefaultHealthcheck - default healthcheck implementation.
Engine is the main component of the library Bots can be attached to it (see Bobrix).
Health - status of bot and service.
No description provided by the author
No description provided by the author
Subscriber - subscriber for healthcheck.

# Interfaces

No description provided by the author
Healthcheck - healthcheck interface for bobrix.

# Type aliases

No description provided by the author
No description provided by the author
HealthcheckOption - options for healthcheck.
No description provided by the author
No description provided by the author