gobot.io/x/gobot@v1.16.0/examples/hello_api.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"html"
    10  	"net/http"
    11  
    12  	"gobot.io/x/gobot"
    13  	"gobot.io/x/gobot/api"
    14  )
    15  
    16  func main() {
    17  	master := gobot.NewMaster()
    18  
    19  	a := api.NewAPI(master)
    20  	a.AddHandler(func(w http.ResponseWriter, r *http.Request) {
    21  		fmt.Fprintf(w, "Hello, %q \n", html.EscapeString(r.URL.Path))
    22  	})
    23  	a.Debug()
    24  	a.Start()
    25  
    26  	master.AddCommand("custom_gobot_command",
    27  		func(params map[string]interface{}) interface{} {
    28  			return "This command is attached to the mcp!"
    29  		})
    30  
    31  	hello := master.AddRobot(gobot.NewRobot("hello"))
    32  
    33  	hello.AddCommand("hi_there", func(params map[string]interface{}) interface{} {
    34  		return fmt.Sprintf("This command is attached to the robot %v", hello.Name)
    35  	})
    36  
    37  	master.Start()
    38  }