hybridgroup.gobot/examples/hello_api_auth.go

37 lines
752 B
Go
Raw Normal View History

2014-04-19 06:45:42 +08:00
package main
import (
"fmt"
2014-07-22 13:19:04 +08:00
"html"
"net/http"
2014-07-11 08:21:21 +08:00
2014-04-19 06:45:42 +08:00
"github.com/hybridgroup/gobot"
2014-05-23 11:18:45 +08:00
"github.com/hybridgroup/gobot/api"
2014-04-19 06:45:42 +08:00
)
func main() {
2014-07-09 09:36:14 +08:00
gbot := gobot.NewGobot()
2014-04-19 06:45:42 +08:00
2014-07-22 13:19:04 +08:00
a := api.NewAPI(gbot)
a.SetBasicAuth("gort", "klatuu")
a.SetDebug()
a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
})
a.Start()
gbot.AddCommand("custom_gobot_command",
func(params map[string]interface{}) interface{} {
return "This command is attached to the mcp!"
})
2014-05-23 11:18:45 +08:00
2014-07-09 09:36:14 +08:00
hello := gbot.AddRobot(gobot.NewRobot("hello"))
2014-06-12 07:44:23 +08:00
2014-07-09 09:36:14 +08:00
hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
2014-07-22 13:19:04 +08:00
return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
2014-06-12 07:44:23 +08:00
})
2014-04-19 06:45:42 +08:00
2014-07-09 09:36:14 +08:00
gbot.Start()
2014-04-19 06:45:42 +08:00
}