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:

gbot := gobot.NewGobot()
api.NewAPI(gbot).Start()

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

gbot := gobot.NewGobot()
server := api.NewAPI(gbot)
server.Port = "3000"
server.Username = "Gort"
server.Password = "klaatu"
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"

  "github.com/hybridgroup/gobot"
  "github.com/hybridgroup/gobot/api"
)

func main() {
  gbot := gobot.NewGobot()

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

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

  hello := gbot.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)
  })

  gbot.Start()
}