help

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: MIT Imports: 4 Imported by: 0

README

botopolis/help

GoDoc Build Status Test Coverage

Output about your botopolis commands.

Usage

See example_test.go for usage details.

In order to show what commands your plugin implements, simply implement the help.Provider interface (Help() []help.Text). help.Plugin lists the help text for all installed plugins.

Example output
user: @bot help
bot:
  help - Displays all the help commands that this bot knows about.
  help <query> - Displays all help commands that match <query>.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/botopolis/bot"
	"github.com/botopolis/bot/help"
	"github.com/botopolis/bot/mock"
)

type HelperPlugin struct{ *mock.Plugin }

func (HelperPlugin) Help() []help.Text {
	return []help.Text{
		{Respond: true, Command: "foo", Description: "bar"},
		{Respond: false, Command: "baz", Description: "bar"},
	}
}

type mockChat struct {
	blocking chan struct{}
	*mock.Chat
}

func newMockChat() *mockChat {
	m := &mockChat{Chat: mock.NewChat(), blocking: make(chan struct{})}
	m.Name = "bot"
	m.MessageChan = make(chan bot.Message, 1)
	m.SendFunc = func(msg bot.Message) error {
		fmt.Println(msg.Text)
		close(m.blocking)
		return nil
	}
	return m
}

func (m *mockChat) SendMessage(text string) {
	m.MessageChan <- bot.Message{Text: text}
	close(m.MessageChan)
}

func (m *mockChat) Wait() { <-m.blocking }

func main() {
	chat := newMockChat()
	go chat.SendMessage("@bot help")

	bot.New(
		chat,
		help.New(),
		&HelperPlugin{mock.NewPlugin()},
	).Run()

	chat.Wait()
}
Output:
bot help - Displays all the help commands that this bot knows about.
bot help <query> - Displays all help commands that match <query>.
bot foo - bar
baz - bar
Example (WithQuery)
package main

import (
	"fmt"

	"github.com/botopolis/bot"
	"github.com/botopolis/bot/help"
	"github.com/botopolis/bot/mock"
)

type HelperPlugin struct{ *mock.Plugin }

func (HelperPlugin) Help() []help.Text {
	return []help.Text{
		{Respond: true, Command: "foo", Description: "bar"},
		{Respond: false, Command: "baz", Description: "bar"},
	}
}

type mockChat struct {
	blocking chan struct{}
	*mock.Chat
}

func newMockChat() *mockChat {
	m := &mockChat{Chat: mock.NewChat(), blocking: make(chan struct{})}
	m.Name = "bot"
	m.MessageChan = make(chan bot.Message, 1)
	m.SendFunc = func(msg bot.Message) error {
		fmt.Println(msg.Text)
		close(m.blocking)
		return nil
	}
	return m
}

func (m *mockChat) SendMessage(text string) {
	m.MessageChan <- bot.Message{Text: text}
	close(m.MessageChan)
}

func (m *mockChat) Wait() { <-m.blocking }

func main() {
	chat := newMockChat()
	go chat.SendMessage("@bot help bar")

	bot.New(
		chat,
		help.New(),
		&HelperPlugin{mock.NewPlugin()},
	).Run()

	chat.Wait()
}
Output:
bot foo - bar
baz - bar

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() bot.Plugin

New creates a new instance of the help plugin.

Types

type Plugin

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

Plugin is the help Plugin for botopolis/bot. It implements bot.Plugin and help.Helper.

func (*Plugin) Help

func (p *Plugin) Help() []Text

Help provides helper text and implements the help.Helper interface.

func (*Plugin) Load

func (p *Plugin) Load(r *bot.Robot)

Load starts the help.Plugin, starts listening for help commands.

type Provider

type Provider interface {
	Help() []Text
}

Provider is the interface a Plugin is expected to implement in order to provide help text.

type Text

type Text struct {
	// Respond is true if the command needs to be sent at a bot.
	// Example: @bot <command>
	Respond bool
	// The command for which we are writing help text.
	// Example: create link <name> <link>
	Command string
	// The description of the command.
	// Example: Creates a link with the <name> short URL to the <link>.
	Description string
}

Text is a representation of what is returned to the user. Example: Text{Respond: true, Command: "foo", Description: "bar"} turns into "<robot.Username> foo - bar"

Jump to

Keyboard shortcuts

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