Slack 机器人

micro bot

micro bot 是一个机器人, 位于您的微服务环境中, 您可以通过 Slack, HipChat, XMPP 等进行交互. 它通过消息模拟 CLI 的功能.

输入支持

  • Discord
  • Slack
  • Telegram

入门

使用 Slack

运行并使用 slack 输入参数

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

多个输入

通过指定逗号分隔列表使用多个输入

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

帮助

在 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-micro/v2/agent/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
    })
}

注册命令

可使用由 golang/regexp.Match 匹配的模式键将命令添加到 Commands 集合 .

import "github.com/micro/go-micro/v2/agent/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
}

注册输入

将输入添加到 Inputs 集合.

import "github.com/micro/go-micro/v2/agent/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

命令即服务

micro bot 支持将命令创建为微服务的能力.

它是如何工作的?

机器人监视服务注册表的服务与它的命名空间. 默认命名空间为 go.micro.bot. 此命名空间中的任何服务将自动添加到可用命令列表中. 当执行命令时, 自动程序将使用 Command.Exec 方法调用服务. 它还希望该 Command.Help 方法存在使用情况信息.

服务接口如下所示, 可在这里找到 go-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;
}

示例

这是一个命令即服务的示例

package main

import (
    "fmt"
    "strings"

    "github.com/micro/go-micro/v2"
    "golang.org/x/net/context"

    proto "github.com/micro/go-micro/v2/agent/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)
    }
}
文档更新时间: 2020-06-02 15:56   作者:kuteng