scmd

package module
v0.0.0-...-735613f Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2017 License: MIT Imports: 8 Imported by: 1

README

slack-cmd-go

Making slack-bot perseing your messages like cli commands.

Usage

install
go get -u github.com/yutaro/slack-cmd-go

Examples

Hello World

package main

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/yutaro/slack-cmd-go"
)

func main() {
	conf := scmd.LoadToml("config.toml")
	bot := scmd.New(conf.TOKEN)

	// just one phrase command.
	// hello => Hello!
	// hello yutaro => Hello yutaro!
	bot.OneCmd("hello", []string{"greeting"},
		func(c *scmd.Context) {
			args := c.GetArgs()
			if len(args) == 0 {
				c.SendMessage("Hello!")
				return
			}
			c.SendMessage(fmt.Sprintf("Hello %s!", strings.Join(args, " ")))
		})

	bot.Start()
}

Calc

package main

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/yutaro/slack-cmd-go"
)

func main() {
	conf := scmd.LoadToml("config.toml")
	bot := scmd.New(conf.TOKEN)

	// two phrase command
	calc := bot.NewCmdGroup("calc")

	// calc sum 2 3 => The result is : 5
	calc.Cmd("sum", []string{"Add two numbers.}",
		func(c *scmd.Context) {
			args := c.GetArgs()
			x, _ := strconv.Atoi(args[0])
			y, _ := strconv.Atoi(args[1])
			c.SendMessage(fmt.Sprintf("The result is : %d", x+y))
		})

	// calc sub 5 10 => The result is : -5
	calc.Cmd("sub", []string{"Sub two numbers."},
		func(c *scmd.Context) {
			args := c.GetArgs()
			x, _ := strconv.Atoi(args[0])
			y, _ := strconv.Atoi(args[1])
			c.SendMessage(fmt.Sprintf("The result is : %d", x-y))
		})

	// calc fib 5 => The result is : 1 1 2 3 5
	calc.Cmd("fib", []string{"Show fibonacci numbers."},
		func(c *scmd.Context) {
			args := c.GetArgs()
			x, _ := strconv.Atoi(args[0])

			nums := make([]string, x)
			f := fibonacci()
			for i := 0; i < x; i++ {
				nums[i] = strconv.Itoa(f())
			}

			c.SendMessage(fmt.Sprintf("The result is : %s", strings.Join(nums, " ")))
		})

	bot.Start()
}

func fibonacci() func() int {
	fib1 := 0
	fib2 := 1
	return func() int {
		fib := fib1 + fib2
		fib1 = fib2
		fib2 = fib
		return fib1
	}
}

Options

package main

import (
	"fmt"

	"github.com/yutaro/slack-cmd-go"
)

func main() {
	conf := scmd.LoadToml("config.toml")
	bot := scmd.New(conf.TOKEN)

	bot.OneCmd("test", []string{"test options and flags"},
		func(c *scmd.Context) {
			args := c.GetArgs()
			flags := c.GetFlags()
			options := c.GetOptions()

			c.SendMessage("--- test ---")
			c.SendMessage(fmt.Sprintf("options : %v , flags : %v , args : %v \n", options, flags, args))
		})

	bot.Start()
}

Library

https://github.com/nlopes/slack
https://github.com/BurntSushi/toml

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	Cmds map[string]map[string]*Cmd
	// contains filtered or unexported fields
}

func New

func New(key string) *Bot

func (*Bot) NewCmdGroup

func (b *Bot) NewCmdGroup(name string) *CmdGroup

func (*Bot) OneCmd

func (b *Bot) OneCmd(name string, explain []string, callback func(*Context))

func (*Bot) Start

func (b *Bot) Start()

type Cmd

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

type CmdGroup

type CmdGroup struct {
	Bot
	// contains filtered or unexported fields
}

func (*CmdGroup) Cmd

func (g *CmdGroup) Cmd(label string, explain []string, callback func(*Context))

type Config

type Config struct {
	TOKEN string
}

func LoadToml

func LoadToml(confname string) *Config

type Context

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

func (*Context) GetArgs

func (c *Context) GetArgs() []string

func (*Context) GetFlags

func (c *Context) GetFlags() map[string]bool

func (*Context) GetHelpMes

func (c *Context) GetHelpMes(name string) string

func (*Context) GetOptions

func (c *Context) GetOptions() map[string]string

func (*Context) SendFile

func (c *Context) SendFile(mes string) *slack.File

func (*Context) SendMessage

func (c *Context) SendMessage(mes string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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