Index

API

Gobot includes a RESTful API to query the status of any robot running within a group, including the connection and device status, and execute device commands.

To activate the API, import the gobot.io/x/gobot/api package and instantiate the API like this:

mbot := gobot.NewMaster()
api.NewAPI(mbot).Start()

You can also specify the api host and port, and turn on authentication:

mbot := gobot.NewMaster()
server := api.NewAPI(mbot)
server.AddHandler(api.BasicAuth("gort", "klaatu"))
server.Port = "3000"
server.Start()

You may access the robeaux React.js interface with Gobot by navigating to http://localhost:3000/.

Routes

The Gobot API conforms to the cppp.io spec.

Example

The following code example will spin up an API server with two custom commands and a middleware handler:

package main

import (
  "fmt"
  "html"
  "net/http"

  "gobot.io/x/gobot"
  "gobot.io/x/gobot/api"
)

func main() {
  mbot := gobot.NewMaster()

  a := api.NewAPI(mbot)
  a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
  })
  a.Debug()
  a.Start()

  mbot.AddCommand("custom_gobot_command",
    func(params map[string]interface{}) interface{} {
      return "This command is attached to the mcp!"
    })

  hello := mbot.AddRobot(gobot.NewRobot("hello"))

  hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
    return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
  })

  mbot.Start()
}