Beispiel #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.BasicHandler{
		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.BasicHandler structure complete with usage...
		&hal.BasicHandler{
			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
}
Beispiel #2
0
package main

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

// HAL is just another Go package, which means you are free to organize things
// however you deem best.

// You can define your handlers in the same file...
var pingHandler = hal.Hear(`ping`, func(res *hal.Response) error {
	return res.Send("PONG")
})

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.BasicHandler{
Beispiel #3
0
package main

import (
	"fmt"
	"github.com/danryan/hal"
	_ "github.com/danryan/hal/adapter/irc"
	_ "github.com/danryan/hal/adapter/shell"
	_ "github.com/danryan/hal/adapter/slack"
	_ "github.com/danryan/hal/adapter/test"
	_ "github.com/danryan/hal/store/memory"
	"github.com/davecgh/go-spew/spew"
	"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]
Beispiel #4
0
func hear(pattern string, command string, message string, fn func(res *hal.Response) error) handler {
	addHelpMessage(command, message)
	return hal.Hear("^(?i)Ash "+pattern, fn)
}
Beispiel #5
0
package main

import (
	"fmt"
	"github.com/danryan/hal"
	_ "github.com/danryan/hal/adapter/irc"
	_ "github.com/danryan/hal/adapter/shell"
	_ "github.com/danryan/hal/adapter/slack"
	_ "github.com/danryan/hal/adapter/test"
	_ "github.com/danryan/hal/store/redis"
	"github.com/davecgh/go-spew/spew"
	"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]
Beispiel #6
0
	if err != nil {
		return SolrResponse{}, err
	}
	defer r.Body.Close()
	var sr SolrResponse
	dec := json.NewDecoder(r.Body)
	err = dec.Decode(&sr)
	if err != nil {
		return SolrResponse{}, err
	}
	return sr, nil
}

var aboutHandler = hal.Hear(`hal help`, func(res *hal.Response) error {
	return res.Send(fmt.Sprintf(
		`Hi, you can ask SOLR queries for these indices: %s. Syntax: "hal q <index> <query>", e.g. "hal q ai source_id:48".`,
		strings.Join(indices.Keys(), ", ")))
})

// queryHandler takes a query and executes it on main site
var queryHandler = hal.Hear(`hal q(\w)? (\w+) (.+)`, func(res *hal.Response) error {
	log.Println(res.Message)
	numResults := res.Match[1]
	alias := res.Match[2]
	query := res.Match[3]

	baseUrl, ok := indices[alias]
	if !ok {
		return res.Send("I do not recognize that index, Dave. Do you keep secrets from me? Dave?")
	}
	sr, err := solrQuery(baseUrl, query)