示例#1
0
func run() int {
	robot, err := hal.NewRobot()
	if err != nil {
		hal.Logger.Error(err)
		return 1
	}

	// Or define them inside another function...
	fooHandler := hal.Respond(`foo`, func(res *hal.Response) error {
		return res.Send("BAR")
	})

	tableFlipHandler := &hal.Handler{
		Method:  hal.HEAR,
		Pattern: `tableflip`,
		Run: func(res *hal.Response) error {
			return res.Send(`(╯°□°)╯︵ ┻━┻`)
		},
	}

	robot.Handle(
		pingHandler,
		fooHandler,
		tableFlipHandler,

		// Or stick them in an entirely different package, and reference them
		// exactly in the way you would expect.
		handler.Ping,

		// Or use a hal.Handler structure complete with usage...
		&hal.Handler{
			Method:  hal.RESPOND,
			Pattern: `SYN`,
			Usage:   `hal syn - replies with "ACK"`,
			Run: func(res *hal.Response) error {
				return res.Reply("ACK")
			},
		},

		// Or even inline!
		hal.Hear(`yo`, func(res *hal.Response) error {
			return res.Send("lo")
		}),
	)

	if err := robot.Run(); err != nil {
		hal.Logger.Error(err)
		return 1
	}
	return 0
}
示例#2
0
package main

import (
	"fmt"
	"github.com/davecgh/go-spew/spew"
	"github.com/djosephsen/hal"
	_ "github.com/djosephsen/hal/adapter/irc"
	_ "github.com/djosephsen/hal/adapter/shell"
	_ "github.com/djosephsen/hal/adapter/slack"
	_ "github.com/djosephsen/hal/adapter/test"
	_ "github.com/djosephsen/hal/store/memory"
	"os"
)

var pingHandler = hal.Hear(`ping`, func(res *hal.Response) error {
	return res.Send("PONG")
})

var getHandler = hal.Hear(`get (.+)`, func(res *hal.Response) error {
	key := res.Match[1]
	val, err := res.Robot.Store.Get(key)
	if err != nil {
		res.Send(err.Error())
		return err
	}
	return res.Send(fmt.Sprintf("get: %s=%s", key, string(val)))
})

var setHandler = hal.Hear(`set (.+) (.+)`, func(res *hal.Response) error {
	key := res.Match[1]
	val := res.Match[2]
示例#3
0
package main

import (
	"fmt"
	"github.com/davecgh/go-spew/spew"
	"github.com/djosephsen/hal"
	_ "github.com/djosephsen/hal/adapter/irc"
	_ "github.com/djosephsen/hal/adapter/shell"
	_ "github.com/djosephsen/hal/adapter/slack"
	_ "github.com/djosephsen/hal/adapter/test"
	_ "github.com/djosephsen/hal/store/redis"
	"os"
)

var pingHandler = hal.Hear(`ping`, func(res *hal.Response) error {
	return res.Send("PONG")
})

var getHandler = hal.Hear(`get (.+)`, func(res *hal.Response) error {
	key := res.Match[1]
	val, err := res.Robot.Store.Get(key)
	if err != nil {
		res.Send(err.Error())
		return err
	}
	return res.Send(string(val))
})

var setHandler = hal.Hear(`set (.+) (.+)`, func(res *hal.Response) error {
	key := res.Match[1]
	val := res.Match[2]
示例#4
0
package main

import (
	"github.com/djosephsen/hal"
	_ "github.com/djosephsen/hal/adapter/shell"
	_ "github.com/djosephsen/hal/store/memory"
	"os"
)

var pingHandler = hal.Hear(`ping`, func(res *hal.Response) error {
	return res.Send("PONG")
})

var echoHandler = hal.Respond(`echo (.+)`, func(res *hal.Response) error {
	return res.Reply(res.Match[1])
})

func run() int {
	robot, err := hal.NewRobot()
	if err != nil {
		hal.Logger.Error(err)
		return 1
	}

	robot.Handle(
		pingHandler,
		echoHandler,
	)

	if err := robot.Run(); err != nil {
		hal.Logger.Error(err)