# 微型機器人
微型機器人是一個位于微服務環境中的機器人,您可以通過Slack,HipChat,XMPP等進行交互。它通過消息傳遞模擬CLI的功能。

## 支持的輸入
- Slack
- HipChat
## 入門
### 安裝Micro
```
go get github.com/micro/micro
```
### 用Slack運行
```
micro bot --inputs=slack --slack_token=SLACK_TOKEN
```

### 用HipChat運行
```
micro bot --inputs=hipchat --hipchat_username=XMPP_USER --hipchat_password=XMPP_PASSWORD
```

通過使用逗號分隔列表來指定多個輸入
```
micro bot --inputs=hipchat,slack --slack_token=SLACK_TOKEN --hipchat_username=XMPP_USER --hipchat_password=XMPP_PASSWORD
```
### 幫助
在slack中
```
micro help
deregister service [definition] - Deregisters a service
echo [text] - Returns the [text]
get service [name] - Returns a registered service
health [service] - Returns health of a service
hello - Returns a greeting
list services - Returns a list of registered services
ping - Returns pong
query [service] [method] [request] - Returns the response for a service query
register service [definition] - Registers a service
the three laws - Returns the three laws of robotics
time - Returns the server time
```
## 添加新的命令
由機器人執行的命令和功能是基于文本的匹配模式。
### 寫一個命令
```
import "github.com/micro/go-bot/command"
func Ping() command.Command {
usage := "ping"
description := "Returns pong"
return command.NewCommand("ping", usage, desc, func(args ...string) ([]byte, error) {
return []byte("pong"), nil
})
}
Registe
```
### 注冊該命令
使用可以通過`golang/regexp.Match`匹配的模式鍵將命令添加到Commands映射表。
```
import "github.com/micro/go-bot/command"
func init() {
command.Commands["^ping$"] = Ping()
}
```
### 重新構建Micro
構建二進制
```
cd github.com/micro/micro
// For local use
go build -i -o micro ./main.go
// For docker image
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go
```
## 添加新的輸入
輸入是用于通信的插件,例如Slack,HipChat,XMPP,IRC,SMTP等等。
可以通過以下方式添加新的輸入。
### 編寫一個輸入
編寫滿足輸入接口的輸入。
```
type Input interface {
// Provide cli flags
Flags() []cli.Flag
// Initialise input using cli context
Init(*cli.Context) error
// Stream events from the input
Stream() (Conn, error)
// Start the input
Start() error
// Stop the input
Stop() error
// name of the input
String() string
}
```
### 注冊輸入
將輸入添加到輸入映射。
```
import "github.com/micro/go-bot/input"
func init() {
input.Inputs["name"] = MyInput
}
```
### 重新構建Micro
構建二進制
```
cd github.com/micro/micro
// For local use
go build -i -o micro ./main.go
// For docker image
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go
```
## 作為服務的命令
微型機器人支持作為微服務創建命令的能力。
### 它是如何工作的?
機器人使用它的命名空間監視服務注冊中心的服務。默認名稱空間是`go.micro.bot`。該名稱空間內的任何服務都將自動添加到可用命令列表中。執行命令時,機器人將使用`Command.Exec`方法調用該服務。它也希望方法`Command.Help`存在于使用信息中。
服務接口如下,可以在[go-bot/proto](https://github.com/micro/go-bot/blob/master/proto/bot.proto)中找到。
```
syntax = "proto3";
package go.micro.bot;
service Command {
rpc Help(HelpRequest) returns (HelpResponse) {};
rpc Exec(ExecRequest) returns (ExecResponse) {};
}
message HelpRequest {
}
message HelpResponse {
string usage = 1;
string description = 2;
}
message ExecRequest {
repeated string args = 1;
}
message ExecResponse {
bytes result = 1;
string error = 2;
}
```
### 示例
這里有一個echo命令作為微服務的示例。
```
package main
import (
"fmt"
"strings"
"github.com/micro/go-micro"
"golang.org/x/net/context"
proto "github.com/micro/go-bot/proto"
)
type Command struct{}
// Help returns the command usage
func (c *Command) Help(ctx context.Context, req *proto.HelpRequest, rsp *proto.HelpResponse) error {
// Usage should include the name of the command
rsp.Usage = "echo"
rsp.Description = "This is an example bot command as a micro service which echos the message"
return nil
}
// Exec executes the command
func (c *Command) Exec(ctx context.Context, req *proto.ExecRequest, rsp *proto.ExecResponse) error {
rsp.Result = []byte(strings.Join(req.Args, " "))
// rsp.Error could be set to return an error instead
// the function error would only be used for service level issues
return nil
}
func main() {
service := micro.NewService(
micro.Name("go.micro.bot.echo"),
)
service.Init()
proto.RegisterCommandHandler(service.Server(), new(Command))
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
```