2014-10-16 01:57:07 +08:00
|
|
|
/*
|
2014-10-29 05:52:59 +08:00
|
|
|
Package api provides a webserver to interact with your Gobot program over the network.
|
2014-10-16 01:57:07 +08:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
package main
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/api"
|
|
|
|
)
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
func main() {
|
2024-02-13 23:30:25 +08:00
|
|
|
gbot := gobot.NewManager()
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// Starts the API server on default port 3000
|
|
|
|
api.NewAPI(gbot).Start()
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// Accessible via http://localhost:3000/api/commands/say_hello
|
|
|
|
gbot.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
|
2024-02-13 23:30:25 +08:00
|
|
|
return "Manager says hello!"
|
2023-10-21 02:50:42 +08:00
|
|
|
})
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
hello := gbot.AddRobot(gobot.NewRobot("Eve"))
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// Accessible via http://localhost:3000/api/robots/Eve/commands/say_hello
|
|
|
|
hello.AddCommand("say_hello", func(params map[string]interface{}) interface{} {
|
|
|
|
return fmt.Sprintf("%v says hello!", hello.Name)
|
|
|
|
})
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
gbot.Start()
|
|
|
|
}
|
2014-10-16 01:57:07 +08:00
|
|
|
|
|
|
|
It follows Common Protocol for Programming Physical Input and Output (CPPP-IO) spec:
|
2016-12-08 20:24:03 +08:00
|
|
|
https://gobot.io/x/cppp-io
|
2014-10-16 01:57:07 +08:00
|
|
|
*/
|
|
|
|
package api
|