gobot

package module
v0.0.0-...-9a39f02 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: ISC Imports: 1 Imported by: 0

README

gobot

gobot is a Simple chatbot written in golang

Initail work based on: https://github.com/mattn/go-xmpp/blob/master/example/example.go

gobot is under active development, the api may change w/out warning.

By itself, gobot just sits and listens for incoming messages. When a message is received, gobot passes the message on to any number of plugins. The plugins provide all the features of the chatbot.

Installing gobot

First, you'll need to have golang installed: http://golang.org/doc/install

Then, once that is working, all you need to do is:

$ go get github.com/gabeguz/gobot/cmd/gobot

Running a bot

gobot supports both slack and XMPP (jabber).

To connect to slack you'll need an API token, directions for getting one can be found at https://api.slack.com/bot-users aside from that, pick a name for your bot and enter the room you would like your bot to join.

$ gobot -protocol=slack -user=herman -pass=<slack api token> -room=bottest -name=herman

For xmpp you need a few extra bits of information. The hostname with port, the abber user, the users password, and the full address to the MUC you'd like the bot to join:

$ gobot -protocol=xmpp -host="jabber.my.domain:5222" -user="gabe@jabber.my.domain" -pass="hunter2" -room="#gobottest@conference.jabber.my.domain" -name="gobot"

Creating A Plugin

Creating a plugin is pretty easy, you just need to import the gobot library:

import (
	"github.com/gabeguz/gobot"
)

And then implement 2 exported methods so that you conform to the bot.Plugin interface:

type MyPlugin struct {}

func (p MyPlugin) Name() string {
	return "MyPlugin v1.0"
}

func (p MyPlugin) Execute(msg bot.Message, bot bot.Bot) error {
	if msg.From() != bot.FullName() {
		bot.Send("Why, hello!")
	}
	return nil
}

One last step, edit gobot/cmd/main.go and add a call to your new plugin to the []gb.Plugin slice:

plugins := []gb.Plugin{
	myplugin.MyPlugin{},
}

and import the code in the import statement at the top of the file.

Finally, reinstall gobot $ cd $GOPATH/src/github/gabeguz/gobot/cmd && go install ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot interface {
	Name() string
	FullName() string
	Send(msg string)
	Reply(orig Message, msg string)
	Connect() error
	Listen() chan Message
	SetLogger(*log.Logger)
	Log(msg string)
}

Bot is the interface implemented by an object that can connect to a chat service and send and reply to messages.

type Gobot

type Gobot struct {
	Bot
	Plugins []Plugin
}

Gobot is a wrapper around a chatbot and its associated plugins.

func (*Gobot) InternalBot

func (g *Gobot) InternalBot() Bot

InternalBot grants access to the currently used chatbot.

type Message

type Message interface {
	ID() string
	Body() string
	From() string
	Room() string
}

Message is the interface implemented by an object that contains an individual chat message as well as the meta-data associated with that chat message.

type Plugin

type Plugin interface {
	Name() string
	Execute(msg Message, bot Bot) error
}

Plugin is the interface implemented by an object that interacts with an individual chat message.

Directories

Path Synopsis
cmd
plugins
chatlog
Package chatlog logs all chat messages to a text file
Package chatlog logs all chat messages to a text file
dm
Package dm gives the bot the ability to recognize messages sent to himself, and reply accordingly
Package dm gives the bot the ability to recognize messages sent to himself, and reply accordingly
quote
Package quote handles different type of quotes
Package quote handles different type of quotes
stathat
Package stathat gives the bot the ability to send info to stathat.com
Package stathat gives the bot the ability to send info to stathat.com
url

Jump to

Keyboard shortcuts

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