1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00
hybridgroup.gobot/examples/hello_api_auth.go

45 lines
871 B
Go
Raw Permalink Normal View History

//go:build example
// +build example
//
// Do not build by default.
2014-04-18 15:45:42 -07:00
package main
import (
"fmt"
2014-07-21 22:19:04 -07:00
"html"
"net/http"
2014-07-10 17:21:21 -07:00
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/api"
2014-04-18 15:45:42 -07:00
)
func main() {
manager := gobot.NewManager()
2014-04-18 15:45:42 -07:00
a := api.NewAPI(manager)
a.AddHandler(api.BasicAuth("gort", "klatuu"))
a.Debug()
2014-07-21 22:19:04 -07:00
a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
})
a.Start()
manager.AddCommand("custom_gobot_command",
2014-07-21 22:19:04 -07:00
func(params map[string]interface{}) interface{} {
return "This command is attached to the mcp!"
})
2014-05-22 20:18:45 -07:00
hello := manager.AddRobot(gobot.NewRobot("hello"))
2014-06-11 16:44:23 -07:00
2014-07-08 18:36:14 -07:00
hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
2014-07-21 22:19:04 -07:00
return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
2014-06-11 16:44:23 -07:00
})
2014-04-18 15:45:42 -07:00
if err := manager.Start(); err != nil {
panic(err)
}
2014-04-18 15:45:42 -07:00
}