hanu

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 8 Imported by: 0

README

hanu <forked> - Go for Slack Bots!

MIT License

The Go framework hanu is your best friend to create Slack bots! hanu uses allot for easy command and request parsing (e.g. whisper <word>) and runs fine as a Heroku worker. All you need is a Slack API token and you can create your first bot within seconds! Just have a look at the hanu-example bot or read my tutorial

Features
  • Respond to mentions
  • Respond to direct messages
  • Auto-Generated command list for help
  • Works fine as a worker on Heroku

V2 Usage

To use the package import:

import "github.com/ChrisMcKee/hanu"

It is very similar to the above, but there are a few extra things. You can set the command prefix, if you like using those:

slack.SetCommandPrefix("!")
slack.SetReplyOnly(false)

This will make it so you have to type:

!whisper I love turtles

For the command to be recognised. Setting the bot to not reply only means it will listen to all messages in an attempt to find a command (except help will only be printed when bot is mentioned).

Also, the ConversationInterface was changed to just Convo to save your wrists:

	slack.Command("whisper <word>", func(conv hanu.Convo) {
		str, _ := conv.String("word")
		conv.Reply(strings.ToLower(str))
	})

The bot can also now talk arbitrarily:

slack.Say("UGHXISDF324", "I like %s", "turtles")

devops := slack.Channel("UGHXISDF324")
devops.Say("Host called %s is not responding to pings", "bobsburgers01")

You can print the help message whenever you want:

slack.Say("UGHXISDF324", bot.BuildHelpText())

And there is an unknown command handler, but it only works when in reply only mode:

slack.SetReplyOnly(true).UnknownCommand(func(c hanu.Convo) {
	c.Reply(slack.BuildHelpText())
})

Dependencies

Credits

Forked (along with allot) from

  • Read Tutorial
  • Code Example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	RTM       *slack.RTM
	ID        string
	Commands  []CommandInterface
	ReplyOnly bool
	CmdPrefix string
	// contains filtered or unexported fields
}

Bot is the main object

func New

func New(token string) (*Bot, error)

New creates a new bot

func NewDebug added in v0.1.1

func NewDebug(token string) (*Bot, error)

New creates a new bot with Debug

func (*Bot) BuildHelpText added in v0.1.1

func (b *Bot) BuildHelpText() string

BuildHelpText will build the help text

func (*Bot) Channel added in v0.1.1

func (b *Bot) Channel(id string) Channel

Channel will return a channel that the bot can talk in

func (*Bot) Command

func (b *Bot) Command(cmd string, handler Handler)

Command adds a new command with custom handler

func (*Bot) Listen

func (b *Bot) Listen(ctx context.Context)

Listen for message on socket

func (*Bot) Register

func (b *Bot) Register(cmd CommandInterface)

Register registers a Command

func (*Bot) Say added in v0.1.1

func (b *Bot) Say(channel, msg string, a ...interface{})

Say will cause the bot to say something in the specified channel

func (*Bot) SetCommandPrefix added in v0.1.1

func (b *Bot) SetCommandPrefix(pfx string) *Bot

SetCommandPrefix will set thing that must be prefixed to the command, there is no prefix by default but one could set it to "!" for instance

func (*Bot) SetReplyOnly added in v0.1.1

func (b *Bot) SetReplyOnly(ro bool) *Bot

SetReplyOnly will make the bot only respond to messages it is mentioned in

func (*Bot) UnknownCommand added in v0.1.1

func (b *Bot) UnknownCommand(h Handler)

UnknownCommand will be called when the user calls a command that is unknown, but it will only work when the bot is in reply only mode

type Channel added in v0.1.1

type Channel struct {
	ID string
	// contains filtered or unexported fields
}

Channel is an object that allows a bot to say things without specifying the channel in every function call

func (*Channel) Say added in v0.1.1

func (ch *Channel) Say(msg string, a ...interface{})

Say will cause the bot to say something in the channel

type Command

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

Command a command

func NewCommand

func NewCommand(text string, description string, handler Handler) Command

NewCommand creates a new command

func (Command) Description

func (c Command) Description() string

Description returns the description

func (Command) Get

func (c Command) Get() allot.CommandInterface

Get returns the command

func (Command) Handle

func (c Command) Handle(conv ConversationInterface)

Handle calls the command's handler

func (*Command) Set

func (c *Command) Set(cmd allot.CommandInterface)

Set defines the command

func (*Command) SetDescription

func (c *Command) SetDescription(text string)

SetDescription sets the description

func (*Command) SetHandler

func (c *Command) SetHandler(handler Handler)

SetHandler sets the handler

type CommandInterface

type CommandInterface interface {
	Get() allot.CommandInterface
	Description() string
	Handle(conv ConversationInterface)
}

CommandInterface defines a command interface

type Conversation

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

Conversation stores message, command and socket information and is passed to the handler function

func (Conversation) Integer

func (c Conversation) Integer(name string) (int, error)

Integer returns integer parameter

func (Conversation) Match

func (c Conversation) Match(position int) (string, error)

Match returns the parameter at the position

func (*Conversation) Message

func (c *Conversation) Message() MessageInterface

Message returns the convos message

func (*Conversation) Reply

func (c *Conversation) Reply(text string, a ...interface{})

Reply sends message using the socket to Slack

func (Conversation) String

func (c Conversation) String(name string) (string, error)

String return string parameter

type ConversationInterface

type ConversationInterface interface {
	Integer(name string) (int, error)
	String(name string) (string, error)
	Reply(text string, a ...interface{})
	Match(position int) (string, error)
	Message() MessageInterface
}

ConversationInterface is the interface for a conversation

func NewConversation

func NewConversation(match allot.MatchInterface, msg Message, bot Sayer) ConversationInterface

NewConversation returns a Conversation struct

type Convo added in v0.1.1

Convo is a shorthand for ConversationInterface

type Handler

type Handler func(Convo)

Handler is the interface for the handler function

type Message

type Message struct {
	ID              uint64
	Type            string
	ChannelID       string
	UserID          string
	Message         string
	OriginalMessage string
}

Message is the Message structure for received and sent messages using Slack

func NewMessage added in v0.1.1

func NewMessage(ev *slack.MessageEvent) Message

func (Message) Channel

func (m Message) Channel() string

Channel returns the channel ID

func (Message) IsDirectMessage

func (m Message) IsDirectMessage() bool

IsDirectMessage checks if the message is received using a direct messaging channel

func (Message) IsFrom

func (m Message) IsFrom(user string) bool

IsFrom checks the sender of the message

func (Message) IsHelpRequest

func (m Message) IsHelpRequest() bool

IsHelpRequest checks if the user requests the help command

func (Message) IsMentionFor

func (m Message) IsMentionFor(user string) bool

IsMentionFor checks if the given user was mentioned with the message

func (Message) IsMessage

func (m Message) IsMessage() bool

IsMessage checks if it is a Message or some other kind of processing information

func (Message) IsRelevantFor

func (m Message) IsRelevantFor(user string) bool

IsRelevantFor checks if the message is relevant for a user

func (*Message) SetText

func (m *Message) SetText(text string)

SetText updates the text of a message

func (*Message) StripLinkMarkup

func (m *Message) StripLinkMarkup() string

StripLinkMarkup converts <http://google.com|google.com> into google.com etc. https://api.slack.com/docs/message-formatting#how_to_display_formatted_messages

func (*Message) StripMention

func (m *Message) StripMention(user string) string

StripMention removes the mention from the message beginning

func (Message) Text

func (m Message) Text() string

Text returns the message text

func (Message) User

func (m Message) User() string

User returns the name of the user who sent the message

type MessageInterface

type MessageInterface interface {
	IsMessage() bool
	IsFrom(user string) bool
	IsHelpRequest() bool
	IsDirectMessage() bool
	IsMentionFor(user string) bool
	IsRelevantFor(user string) bool

	Text() string
	User() string
	Channel() string
}

MessageInterface defines the message interface

type Sayer added in v0.1.1

type Sayer interface {
	Say(string, string, ...interface{})
}

Sayer is an object that can talk in the channel

Jump to

Keyboard shortcuts

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