Bot

micro bot

The micro bot is a bot that sits inside your microservices environment which you can interact with via Slack, HipChat, XMPP, etc. It mimics the functions of the CLI via messaging.

Slack Bot - 图1

Supported Inputs

  • Discord
  • Slack
  • Telegram

Getting Started

Use Slack

Run with the slack input

  1. micro bot --inputs=slack --slack_token=SLACK_TOKEN

Slack Bot - 图2

Multiple Inputs

Use multiple inputs by specifying a comma separated list

  1. micro bot --inputs=discord,slack --slack_token=SLACK_TOKEN --discord_token=DISCORD_TOKEN

Help

In slack

  1. micro help
  2. deregister service [definition] - Deregisters a service
  3. echo [text] - Returns the [text]
  4. get service [name] - Returns a registered service
  5. health [service] - Returns health of a service
  6. hello - Returns a greeting
  7. list services - Returns a list of registered services
  8. ping - Returns pong
  9. query [service] [method] [request] - Returns the response for a service query
  10. register service [definition] - Registers a service
  11. the three laws - Returns the three laws of robotics
  12. time - Returns the server time

Adding new Commands

Commands are functions executed by the bot based on text based pattern matching.

Write a Command

  1. import "github.com/micro/go-micro/v2/agent/command"
  2. func Ping() command.Command {
  3. usage := "ping"
  4. description := "Returns pong"
  5. return command.NewCommand("ping", usage, desc, func(args ...string) ([]byte, error) {
  6. return []byte("pong"), nil
  7. })
  8. }

Register the command

Add the command to the Commands map with a pattern key that can be matched by golang/regexp.Match

  1. import "github.com/micro/go-micro/v2/agent/command"
  2. func init() {
  3. command.Commands["^ping$"] = Ping()
  4. }

Rebuild Micro

Build binary

  1. cd github.com/micro/micro
  2. // For local use
  3. go build -i -o micro ./main.go
  4. // For docker image
  5. CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go

Adding new Inputs

Inputs are plugins for communication e.g Slack, HipChat, XMPP, IRC, SMTP, etc, etc.

New inputs can be added in the following way.

Write an Input

Write an input that satisfies the Input interface.

  1. type Input interface {
  2. // Provide cli flags
  3. Flags() []cli.Flag
  4. // Initialise input using cli context
  5. Init(*cli.Context) error
  6. // Stream events from the input
  7. Stream() (Conn, error)
  8. // Start the input
  9. Start() error
  10. // Stop the input
  11. Stop() error
  12. // name of the input
  13. String() string
  14. }

Register the input

Add the input to the Inputs map.

  1. import "github.com/micro/go-micro/v2/agent/input"
  2. func init() {
  3. input.Inputs["name"] = MyInput
  4. }

Rebuild Micro

Build binary

  1. cd github.com/micro/micro
  2. // For local use
  3. go build -i -o micro ./main.go
  4. // For docker image
  5. CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go

Commands as Services

The micro bot supports the ability to create commands as microservices.

How does it work?

The bot watches the service registry for services with it’s namespace. The default namespace is go.micro.bot. Any service within this namespace will automatically be added to the list of available commands. When a command is executed, the bot will call the service with method Command.Exec. It also expects the method Command.Help to exist for usage info.

The service interface is as follows and can be found at go-bot/proto

  1. syntax = "proto3";
  2. package go.micro.bot;
  3. service Command {
  4. rpc Help(HelpRequest) returns (HelpResponse) {};
  5. rpc Exec(ExecRequest) returns (ExecResponse) {};
  6. }
  7. message HelpRequest {
  8. }
  9. message HelpResponse {
  10. string usage = 1;
  11. string description = 2;
  12. }
  13. message ExecRequest {
  14. repeated string args = 1;
  15. }
  16. message ExecResponse {
  17. bytes result = 1;
  18. string error = 2;
  19. }

Example

Here’s an example echo command as a microservice

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/micro/go-micro/v2"
  6. "golang.org/x/net/context"
  7. proto "github.com/micro/go-micro/v2/agent/proto"
  8. )
  9. type Command struct{}
  10. // Help returns the command usage
  11. func (c *Command) Help(ctx context.Context, req *proto.HelpRequest, rsp *proto.HelpResponse) error {
  12. // Usage should include the name of the command
  13. rsp.Usage = "echo"
  14. rsp.Description = "This is an example bot command as a micro service which echos the message"
  15. return nil
  16. }
  17. // Exec executes the command
  18. func (c *Command) Exec(ctx context.Context, req *proto.ExecRequest, rsp *proto.ExecResponse) error {
  19. rsp.Result = []byte(strings.Join(req.Args, " "))
  20. // rsp.Error could be set to return an error instead
  21. // the function error would only be used for service level issues
  22. return nil
  23. }
  24. func main() {
  25. service := micro.NewService(
  26. micro.Name("go.micro.bot.echo"),
  27. )
  28. service.Init()
  29. proto.RegisterCommandHandler(service.Server(), new(Command))
  30. if err := service.Run(); err != nil {
  31. fmt.Println(err)
  32. }
  33. }