gobot.io/x/gobot/v2@v2.1.0/examples/hello_api.go (about)

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