github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/framework/bot.md (about) 1 --- 2 title: Bot 3 keywords: bot 4 tags: [bot] 5 sidebar: home_sidebar 6 permalink: /bot 7 summary: 8 --- 9 10 # micro bot 11 12 The **micro bot** is a bot that sits inside your microservices environment which you can interact with via Slack, HipChat, XMPP, etc. 13 It mimics the functions of the CLI via messaging. 14 15 <p align="center"> 16 <img src="images/bot.png" /> 17 </p> 18 19 ## Supported Inputs 20 21 - Discord 22 - Slack 23 - Telegram 24 25 ## Getting Started 26 27 ### Use Slack 28 29 Run with the slack input 30 31 ```shell 32 micro bot --inputs=slack --slack_token=SLACK_TOKEN 33 ``` 34 35 <img src="images/slack.png"> 36 37 ### Multiple Inputs 38 39 Use multiple inputs by specifying a comma separated list 40 41 ```shell 42 micro bot --inputs=discord,slack --slack_token=SLACK_TOKEN --discord_token=DISCORD_TOKEN 43 ``` 44 45 ### Help 46 47 In slack 48 49 ```shell 50 micro help 51 52 deregister service [definition] - Deregisters a service 53 echo [text] - Returns the [text] 54 get service [name] - Returns a registered service 55 health [service] - Returns health of a service 56 hello - Returns a greeting 57 list services - Returns a list of registered services 58 ping - Returns pong 59 query [service] [method] [request] - Returns the response for a service query 60 register service [definition] - Registers a service 61 the three laws - Returns the three laws of robotics 62 time - Returns the server time 63 ``` 64 65 ## Adding new Commands 66 67 Commands are functions executed by the bot based on text based pattern matching. 68 69 ### Write a Command 70 71 ```go 72 import "github.com/micro/go-micro/v2/agent/command" 73 74 func Ping() command.Command { 75 usage := "ping" 76 description := "Returns pong" 77 78 return command.NewCommand("ping", usage, description, func(args ...string) ([]byte, error) { 79 return []byte("pong"), nil 80 }) 81 } 82 ``` 83 84 ### Register the command 85 86 Add the command to the Commands map with a pattern key that can be matched by golang/regexp.Match 87 88 ```go 89 import "github.com/micro/go-micro/v2/agent/command" 90 91 func init() { 92 command.Commands["^ping$"] = Ping() 93 } 94 ``` 95 96 ### Rebuild Micro 97 98 Build binary 99 100 ```shell 101 cd github.com/tickoalcantara12/micro 102 103 // For local use 104 go build -i -o micro ./main.go 105 106 // For docker image 107 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go 108 ``` 109 110 ## Adding new Inputs 111 112 Inputs are plugins for communication e.g Slack, HipChat, XMPP, IRC, SMTP, etc, etc. 113 114 New inputs can be added in the following way. 115 116 ### Write an Input 117 118 Write an input that satisfies the Input interface. 119 120 ```go 121 type Input interface { 122 // Provide cli flags 123 Flags() []cli.Flag 124 // Initialise input using cli context 125 Init(*cli.Context) error 126 // Stream events from the input 127 Stream() (Conn, error) 128 // Start the input 129 Start() error 130 // Stop the input 131 Stop() error 132 // name of the input 133 String() string 134 } 135 ``` 136 137 ### Register the input 138 139 Add the input to the Inputs map. 140 141 ```go 142 import "github.com/micro/go-micro/v2/agent/input" 143 144 func init() { 145 input.Inputs["name"] = MyInput 146 } 147 ``` 148 149 ### Rebuild Micro 150 151 Build binary 152 153 ```shell 154 cd github.com/tickoalcantara12/micro 155 156 // For local use 157 go build -i -o micro ./main.go 158 159 // For docker image 160 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go 161 ``` 162 163 ## Commands as Services 164 165 The micro bot supports the ability to create commands as microservices. 166 167 ### How does it work? 168 169 The bot watches the service registry for services with it's namespace. The default namespace is `go.micro.bot`. 170 Any service within this namespace will automatically be added to the list of available commands. When a command 171 is executed, the bot will call the service with method `Command.Exec`. It also expects the method `Command.Help` 172 to exist for usage info. 173 174 175 The service interface is as follows and can be found at [go-bot/proto](https://github.com/micro/go-micro/agent/blob/master/proto/bot.proto) 176 177 ```proto 178 syntax = "proto3"; 179 180 package go.micro.bot; 181 182 service Command { 183 rpc Help(HelpRequest) returns (HelpResponse) {}; 184 rpc Exec(ExecRequest) returns (ExecResponse) {}; 185 } 186 187 message HelpRequest { 188 } 189 190 message HelpResponse { 191 string usage = 1; 192 string description = 2; 193 } 194 195 message ExecRequest { 196 repeated string args = 1; 197 } 198 199 message ExecResponse { 200 bytes result = 1; 201 string error = 2; 202 } 203 ``` 204 205 ### Example 206 207 Here's an example echo command as a microservice 208 209 ```go 210 package main 211 212 import ( 213 "fmt" 214 "strings" 215 216 "github.com/micro/go-micro/v2" 217 "golang.org/x/net/context" 218 219 proto "github.com/micro/go-micro/v2/agent/proto" 220 ) 221 222 type Command struct{} 223 224 // Help returns the command usage 225 func (c *Command) Help(ctx context.Context, req *proto.HelpRequest, rsp *proto.HelpResponse) error { 226 // Usage should include the name of the command 227 rsp.Usage = "echo" 228 rsp.Description = "This is an example bot command as a micro service which echos the message" 229 return nil 230 } 231 232 // Exec executes the command 233 func (c *Command) Exec(ctx context.Context, req *proto.ExecRequest, rsp *proto.ExecResponse) error { 234 rsp.Result = []byte(strings.Join(req.Args, " ")) 235 // rsp.Error could be set to return an error instead 236 // the function error would only be used for service level issues 237 return nil 238 } 239 240 func main() { 241 service := micro.NewService( 242 micro.Name("go.micro.bot.echo"), 243 ) 244 245 service.Init() 246 247 proto.RegisterCommandHandler(service.Server(), new(Command)) 248 249 if err := service.Run(); err != nil { 250 fmt.Println(err) 251 } 252 } 253 ``` 254