command

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2020 License: MIT Imports: 9 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GoodMorningCommand = BasicCommandTemplate{
	Help:   "reply Good morning in thread",
	Regexp: regexp.MustCompile("(?i)Good Morning"),
	GenerateFn: func(d CommandData) Command {
		c := Command{}
		text := fmt.Sprintf("Good Morning <@%s>", d.SenderID)
		task := NewReplyThreadEngineTask(d.Engine, d.Channel, text, d.ThreadTimestamp)
		c.Add(task)
		return c
	},
}

GoodMorningCommand is an example command. This command reply greeting to a thread.

View Source
var HelpCommand = BasicCommandTemplate{
	Help:           "show command list and usages",
	MentionCommand: "help",
	GenerateFn: func(d CommandData) Command {
		c := Command{}

		mentionMap := d.GetMentionMap()
		mentions := make([]string, 0, len(mentionMap))
		for key, c := range mentionMap {
			if !c.HasHelp() {
				continue
			}
			str := fmt.Sprintf("- %s\t\t%s", key, c.GetHelp())
			mentions = append(mentions, str)
		}

		regexpList := d.GetRegexpList()
		freewords := make([]string, len(regexpList))
		for i, c := range regexpList {
			if !c.HasHelp() {
				continue
			}
			freewords[i] = fmt.Sprintf("- %s\t\t%s", c.GetRegexp().String(), c.GetHelp())
		}

		text := fmt.Sprintf("```Command:\n%s\n\nFreeword:\n%s```", strings.Join(mentions, "\n"), strings.Join(freewords, "\n"))
		task := NewReplyEngineTask(d.Engine, d.Channel, text)
		c.Add(task)
		return c
	},
}

HelpCommand is an example command. This command shows all of the commands help text.

View Source
var ParrotCommand = BasicCommandTemplate{
	Help:           "reply text between :parrot: Emoji",
	MentionCommand: "parrot",
	GenerateFn: func(d CommandData) Command {
		c := Command{}
		text := fmt.Sprintf(":parrot: %s :parrot:", d.TextOther)
		task := NewReplyEngineTask(d.Engine, d.Channel, text)
		c.Add(task)
		return c
	},
}

ParrotCommand is an example command. This command puts :parrot: emoji both side.

View Source
var PingCommand = BasicCommandTemplate{
	Help:           "reply PONG",
	MentionCommand: "ping",
	GenerateFn: func(d CommandData) Command {
		c := Command{}
		task := NewReplyEngineTask(d.Engine, d.Channel, "PONG")
		c.Add(task)
		return c
	},
}

PingCommand is an example command. This command sais "PONG".

View Source
var ReactEmojiCommand = BasicCommandTemplate{
	Help:   "add reaction to message",
	Regexp: regexp.MustCompile("^"),
	GenerateFn: func(d CommandData) Command {
		c := Command{}
		if !isRandValid(20) {
			return c
		}

		emoji, err := d.Engine.GetEmojiByRandom()
		if err != nil {
			task := NewReplyEngineTask(d.Engine, d.Channel, fmt.Sprintf("Error on Slack!\nErr: `%s`", err.Error()))
			c.Add(task)
			return c
		}

		task := NewReactionEmojiEngineTask(d.Engine, d.Channel, emoji, d.ThreadTimestamp)
		c.Add(task)
		return c
	},
}

ReactEmojiCommand is an example command. This command add emoji reaction 20% chance.

View Source
var ReloadCommand = BasicCommandTemplate{
	Help:           "Reload Bot Engine",
	MentionCommand: "reload",
	GenerateFn: func(d CommandData) Command {
		c := Command{}
		c.Add(NewReplyEngineTask(d.Engine, d.Channel, "Reloading..."))
		c.Add(NewReloadEngineTask(d.Engine))
		return c
	},
}

ReloadCommand is an example command. This command reloads bot engine.

Functions

func NewReactionEmojiEngineTask

func NewReactionEmojiEngineTask(e engine.Engine, channel, emoji, timestamp string) *reactionEmojiEngineTask

NewReactionEmojiEngineTask is a task to add reaction to a message.

func NewReloadEngineTask added in v0.0.2

func NewReloadEngineTask(e engine.Engine) reloadEngineTask

NewReloadEngineTask is a task to reload engine.

func NewReplyEngineTask

func NewReplyEngineTask(e engine.Engine, channel, text string) *replyEngineTask

NewReplyEngineTask is a task to reply text.

func NewReplyThreadEngineTask

func NewReplyThreadEngineTask(e engine.Engine, channel, text, timestamp string) *replyThreadEngineTask

NewReplyThreadEngineTask is a task to reply text on a thread.

func NewUploadEngineTask

func NewUploadEngineTask(e engine.Engine, channel string, file io.Reader, filename string) *uploadEngineTask

NewUploadEngineTask is a task to upload a file.

Types

type BasicCommandTemplate

type BasicCommandTemplate struct {
	MentionCommand string
	Regexp         *regexp.Regexp
	GenerateFn     func(CommandData) Command

	Help   string // usage text
	NoHelp bool   // don't show usage text
}

BasicCommandTemplate is a basic command template.

func (BasicCommandTemplate) Exec

func (BasicCommandTemplate) GetHelp

func (c BasicCommandTemplate) GetHelp() string

func (BasicCommandTemplate) GetMentionCommand

func (c BasicCommandTemplate) GetMentionCommand() string

func (BasicCommandTemplate) GetRegexp

func (c BasicCommandTemplate) GetRegexp() *regexp.Regexp

func (BasicCommandTemplate) HasHelp

func (c BasicCommandTemplate) HasHelp() bool

type Command

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

Command contains task list to execute.

func (*Command) Add

func (c *Command) Add(t Task)

Add adds a task.

func (Command) Exec

func (c Command) Exec()

Exec executes all of tasks.

type CommandData

type CommandData struct {
	Engine engine.Engine

	RawText     string // "@bot <command> <other>" or "<command> <other>"
	Text        string // "@bot <command> <other>"
	TextMention string // @bot
	TextCommand string // <command>
	TextOther   string // <other>
	IsDM        bool   // it's on DM or not

	BotID      string
	SenderID   string
	SenderName string

	IsFile bool // it's file upload event or not
	File   File

	// Engine's data
	Channel         string
	ThreadTimestamp string

	// for help usecase
	CommandSet *CommandSet
}

CommandData is used to create and execute command.

func (CommandData) GetMentionMap

func (d CommandData) GetMentionMap() map[string]CommandTemplate

func (CommandData) GetRegexpList

func (d CommandData) GetRegexpList() []CommandTemplate

func (CommandData) HasMyMention

func (d CommandData) HasMyMention() bool

type CommandSet

type CommandSet struct {
	List       []CommandTemplate
	FileUpload CommandTemplate
	// contains filtered or unexported fields
}

CommandSet is a collection of bot commands.

func NewCommandSet

func NewCommandSet(templateList ...CommandTemplate) *CommandSet

func NewCommandSetWithFileUpload added in v0.1.0

func NewCommandSetWithFileUpload(fileUpload CommandTemplate, templateList ...CommandTemplate) *CommandSet

func (*CommandSet) Exec

func (s *CommandSet) Exec(d CommandData)

func (*CommandSet) Init

func (s *CommandSet) Init()

type CommandTemplate

type CommandTemplate interface {
	Exec(CommandData)

	GetMentionCommand() string
	GetRegexp() *regexp.Regexp
	GetHelp() string
	HasHelp() bool
}

type File added in v0.1.0

type File struct {
	ID                string
	Name              string
	Title             string
	Mimetype          string
	ImageExifRotation int
	Filetype          string
	PrettyType        string
	Size              int
	URL               string
	IsPublic          bool
	Permalink         string
}

File contains metadeta of a file uploaded by user.

type Task

type Task interface {
	// GetName gets a task name.
	GetName() string
	// Run runs a task.
	Run() error
}

Single task interface.

Jump to

Keyboard shortcuts

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