2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2017-03-13 23:01:39 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
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
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
2014-04-19 06:45:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-13 23:30:25 +08:00
|
|
|
manager := gobot.NewManager()
|
2014-04-19 06:45:42 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
a := api.NewAPI(manager)
|
2014-09-19 11:12:49 +08:00
|
|
|
a.AddHandler(api.BasicAuth("gort", "klatuu"))
|
|
|
|
a.Debug()
|
2014-07-22 13:19:04 +08:00
|
|
|
|
|
|
|
a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
|
|
|
|
})
|
|
|
|
a.Start()
|
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
manager.AddCommand("custom_gobot_command",
|
2014-07-22 13:19:04 +08:00
|
|
|
func(params map[string]interface{}) interface{} {
|
|
|
|
return "This command is attached to the mcp!"
|
|
|
|
})
|
2014-05-23 11:18:45 +08:00
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
hello := manager.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
|
|
|
|
2024-02-13 23:30:25 +08:00
|
|
|
if err := manager.Start(); err != nil {
|
2024-02-11 22:34:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-19 06:45:42 +08:00
|
|
|
}
|