Categorygithub.com/go-joe/joe
modulepackage
0.12.0
Repository: https://github.com/go-joe/joe.git
Documentation: pkg.go.dev

# README

Joe Bot :robot:

A general-purpose bot library inspired by Hubot but written in Go.


Joe is a library used to write chat bots in the Go programming language. It is very much inspired by the awesome Hubot framework developed by the folks at Github and brings its power to people who want to implement chat bots using Go.

Getting Started

Joe is a software library that is packaged as Go module. You can get it via:

go get github.com/go-joe/joe

Example usage

You can find all code examples, more explanation and complete recipes at https://joe-bot.net

Each bot consists of a chat Adapter (e.g. to integrate with Slack), a Memory implementation to remember key-value data (e.g. using Redis) and a Brain which routes new messages or custom events (e.g. receiving an HTTP call) to the corresponding registered handler functions.

By default joe.New(…) uses the CLI adapter which makes the bot read messages from stdin and respond on stdout. Additionally the bot will store key value data in-memory which means it will forget anything you told it when it is restarted. This default setup is useful for local development without any dependencies but you will quickly want to add other Modules to extend the bots capabilities.

The following example connects the Bot with a Slack workspace and stores key-value data in Redis. To allow the message handlers to access the memory we define them as functions on a custom ExampleBottype which embeds the joe.Bot.

package main

import (
	"fmt"

	"github.com/go-joe/joe"
	"github.com/go-joe/redis-memory"
	"github.com/go-joe/slack-adapter/v2"
)

type ExampleBot struct {
	*joe.Bot
}

func main() {
	b := &ExampleBot{
		Bot: joe.New("example",
			redis.Memory("localhost:6379"),
			slack.Adapter("xoxb-1452345…"),
		),
	}

	b.Respond("remember (.+) is (.+)", b.Remember)
	b.Respond("what is (.+)", b.WhatIs)

	err := b.Run()
	if err != nil {
		b.Logger.Fatal(err.Error())
	}
}

func (b *ExampleBot) Remember(msg joe.Message) error {
	key, value := msg.Matches[0], msg.Matches[1]
	msg.Respond("OK, I'll remember %s is %s", key, value)
	return b.Store.Set(key, value)
}

func (b *ExampleBot) WhatIs(msg joe.Message) error {
	key := msg.Matches[0]
	var value string
	ok, err := b.Store.Get(key, &value)
	if err != nil {
		return fmt.Errorf("failed to retrieve key %q from brain: %w", key, err)
	}

	if ok {
		msg.Respond("%s is %s", key, value)
	} else {
		msg.Respond("I do not remember %q", key)
	}

	return nil
}

Available modules

Joe ships with no third-party modules such as Redis integration to avoid pulling in more dependencies than you actually require. There are however already some modules that you can use directly to extend the functionality of your bot without writing too much code yourself.

If you have written a module and want to share it, please add it to this list and open a pull request.

Chat Adapters

Memory Modules

Other Modules

Built With

  • zap - Blazing fast, structured, leveled logging in Go
  • multierr - Package multierr allows combining one or more errors together
  • testify - A simple unit test library

Contributing

Please read CONTRIBUTING.md for details on our code of conduct and on the process for submitting pull requests to this repository.

Versioning

THIS SOFTWARE IS STILL IN ALPHA AND THERE ARE NO GUARANTEES REGARDING API STABILITY YET.

All significant (e.g. breaking) changes are documented in the CHANGELOG.md.

After the v1.0 release we plan to use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Friedrich Große - Initial work - fgrosse

See also the list of contributors who participated in this project.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

Acknowledgments

  • Hubot and its great community for the inspiration
  • embedmd for a cool tool to embed source code in markdown files

# Packages

Package joetest implements helpers to implement unit tests for bots.
Package reactions contains a list of generated reactions that are widely used in different chat applications on the internet.

# Functions

FinishEventContent can be called from within your event handler functions to indicate that the Brain should not execute any other handlers after the calling handler has returned.
New creates a new Bot and initializes it with the given Modules and Options.
NewAuth creates a new Auth instance.
NewBrain creates a new robot Brain.
NewCLIAdapter creates a new CLIAdapter.
NewConfig creates a new Config that is used to setup the underlying components of a Bot.
NewStorage creates a new Storage instance that encodes values as JSON and stores them in-memory.
WithContext is an option to replace the default context of a bot.
WithHandlerTimeout is an option to set a timeout on event handlers functions.
WithLogger is an option to replace the default logger of a bot.
WithLogLevel is an option to change the default log level of a bot.

# Constants

ErrNotAllowed is returned if the user is not allowed access to a specific scope.
ErrNotImplemented is returned if the user tries to use a feature that is not implemented on the corresponding components (e.g.

# Structs

Auth implements logic to add user authorization checks to your bot.
A Bot represents an event based chat bot.
The Brain contains the core logic of a Bot by implementing an event handling system that dispatches events to all registered event handlers.
The CLIAdapter is the default Adapter implementation that the bot uses if no other adapter was configured.
Config is the configuration of a Bot that can be used or changed during setup in a Module.
An Event represents a concrete event type and optional callbacks that are triggered when the event was processed by all registered handlers.
The InitEvent is the first event that is handled by the Brain after the Bot is started via Bot.Run().
A Message is automatically created from a ReceiveMessageEvent and then passed to the RespondFunc that was registered via Bot.Respond(…) or Bot.RespondRegex(…) when the message matches the regular expression of the handler.
The ReceiveMessageEvent is typically emitted by an Adapter when the Bot sees a new message from the chat.
The ShutdownEvent is the last event that is handled by the Brain before it stops handling any events after the bot context is done.
A Storage provides a convenient interface to a Memory implementation.
User contains all the information about a user.
The UserTypingEvent is emitted by the Adapter and indicates that the Bot sees that a user is typing.

# Interfaces

An Adapter connects the bot with the chat by enabling it to receive and send messages.
The EventEmitter can be used by a Module by calling Config.EventEmitter().
The Memory interface allows the bot to persist data as key-value pairs.
A MemoryEncoder is used to encode and decode any values that are stored in the Memory.
A Module is an optional Bot extension that can add new capabilities such as a different Memory implementation or Adapter.
ReactionAwareAdapter is an optional interface that Adapters can implement if they support reacting to messages with emojis.

# Type aliases

Error is the error type used by Joe.
ModuleFunc is a function implementation of a Module.