bearychat

package module
v0.0.0-...-14b491e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2017 License: Apache-2.0 Imports: 10 Imported by: 13 modules, 12 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Bearychat API

Outgoing

下载代码

go get github.com/gogap/bearychat
Outgoing HTTP

直接启动现成的HTTP服务示例:

一. 编译

cd $GOPATH/github.com/gogap/bearychat/outgoing/cmd/outgoing
go build

二. 配置

cp outgoing.conf.example outgoing.conf

样例配置文件:outgoing.conf

{
    http.address=":3000"
    http.path="/triggers"

    outgoing {

        hello {
            word = "!hello"
            drivers = [gogap-auth, gogap-greeter]
            gogap-greeter = {
                name = "Robot GoGap"
                image="https://avatars2.githubusercontent.com/u/8731757?v=3&s=200"
            }

            gogap-auth = {
                token = "a5abc87ce0dcd169d4560385c2be9d3a"
            }
        }

        cmd {
            word = "!cmd"
            drivers = [gogap-auth, gogap-commands]

            gogap-auth = {
                token = "8831067e28290392313ca4d81356abe3"
            }

            gogap-commands = {
               timeout = 5s
               commands = {
                    ping = {
                        cmd = ping
                        cwd = /
                    }

                    ls = {
                        cmd = ls
                        cwd = /Users/zhengxujin/Downloads
                    }
                }
            }
        }
    }
}

outgoing这个程序默认会加载本地的 outgoing.conf, 如果您想指定某个配置文件,也可以使用--config 参数.

./outgoing run --config your-config.conf

config 文件采用的是 hocon 格式,同时兼容JSON,具体使用方法请参考:https://github.com/go-akka/configuration

三. 启动

./outgoing run

根据上面的配置信息,我们的服务监听地址为:http://127.0.0.1:3000/triggers

访问测试

curl -X POST -H "Content-Type: application/json" -d '{
  "token" : "8831067e28290392313ca4d81356abe3",
  "ts" : 1355517523,
  "text" : "!cmd ping -c 2 baidu.com",
  "trigger_word" : "!cmd",
  "subdomain" : "your_domain",
  "channel_name" : "your_channel",
  "user_name" : "Zeal"
}' "http://127.0.0.1:3000/triggers"

返回如下结果

{
    "text": "PING baidu.com (220.181.57.217): 56 data bytes\n64 bytes from 220.181.57.217: icmp_seq=0 ttl=51 time=4.935 ms\n64 bytes from 220.181.57.217: icmp_seq=1 ttl=51 time=6.317 ms\n\n--- baidu.com ping statistics ---\n2 packets transmitted, 2 packets received, 0.0% packet loss\nround-trip min/avg/max/stddev = 4.935/5.626/6.317/0.691 ms\n",
    "attachments": null
}
自定义 Trigger

Auth Trigger样例

package auth

import (
    "errors"

    "github.com/go-akka/configuration"
    "github.com/gogap/bearychat/outgoing"
)

type Auth struct {
    word  string
    token string
}

func init() {
    outgoing.RegisterTriggerDriver("gogap-auth", NewAuth)
}

func NewAuth(word string, config *configuration.Config) (outgoing.Trigger, error) {
    return &Auth{
        word:  word,
        token: config.GetString("token"),
    }, nil
}

func (p *Auth) Handle(req *outgoing.Request, resp *outgoing.Response) (err error) {

    if req.TriggerWord != p.word {
        err = errors.New("bad request trigger word in gogap-auth")
        return
    }

    if req.Token != p.token {
        err = errors.New("error auth token")
        return
    }

    return
}

gogap-auth为唯一标识,不得与其他插件冲突

使用这个Trigger

我们在目录$GOPATH/github.com/gogap/bearychat/outgoing/cmd/outgoing下创建一个 imports_mine.go

加入如下内容

package main

import (
    _ "github.com/gogap/bearychat/outgoing/triggers/auth"
    ... //此处可以添加更多的Trigger
)

然后再重新编译

go build

这个Tigger的驱动就添加完成了,我们在配置文件里按照如下配置即可:


...

outgoing {

        hello {
            word = "!hello"
            drivers = [gogap-auth, ...] // 按顺序排列执行,如果某个驱动返回error,则中断

            ...

            gogap-auth = {
                token = "a5abc87ce0dcd169d4560385c2be9d3a"
            }
        }
}
Incoming
  • Request
type Request struct {
    Text         string       `json:"text"`
    Notification string       `json:"notification"`
    Markdown     bool         `json:"markdown"`
    Channel      string       `json:"channel"`
    User         string       `json:"user"`
    Attachments  []Attachment `json:"attachments"`
}
  • Response
type Response struct {
    Code   int         `json:"code"`
    Error  string      `json:"error"`
    Result interface{} `json:"result"`
}
使用方法
package main

import (
    "fmt"

    "github.com/gogap/bearychat/incoming"
)

func main() {

    client := incoming.NewClient()

    req := incoming.Request{
        Text: "愿原力与你同在",
    }

    resp, err := client.Send("=ba7Ld", "ae28af7a66e16effe45f71365d6b21dc", &req)

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(resp)
}

Documentation

Index

Constants

View Source
const (
	OUTGOING = "gogap-outgoing"
)

Variables

View Source
var (
	ErrTriggerDriverAlreadyRegistered = errors.New("trigger driver already registered")
	ErrNewTriggerFuncIsNil            = errors.New("trigger func is nil")
	ErrBreakOnly                      = errors.New("break only")
	ErrNoContent                      = errors.New("no content")
)

Functions

func RegisterTriggerDriver

func RegisterTriggerDriver(name string, fn NewTriggerFunc)

func TriggerDrivers

func TriggerDrivers() []string

Types

type Attachment

type Attachment struct {
	Title  string  `json:"title"`
	Text   string  `json:"text"`
	Color  string  `json:"color"`
	Images []Image `json:"images"`
}

type ClientOption

type ClientOption func(*IncomingClient)

func TimeoutOption

func TimeoutOption(timeout time.Duration) ClientOption

func TransportOption

func TransportOption(transport *http.Transport) ClientOption

type ErrorHandlerFunc

type ErrorHandlerFunc func(cause error) Message

type Image

type Image struct {
	URL string `json:"url"`
}

type IncomingClient

type IncomingClient struct {
	// contains filtered or unexported fields
}

func NewIncomingClient

func NewIncomingClient(opts ...ClientOption) *IncomingClient

func (*IncomingClient) Options

func (p *IncomingClient) Options(opts ...ClientOption)

func (*IncomingClient) Send

func (p *IncomingClient) Send(url string, msg *Message) (resp *IncomingResponse, err error)

type IncomingResponse

type IncomingResponse struct {
	Code   int         `json:"code"`
	Error  string      `json:"error"`
	Result interface{} `json:"result"`
}

func (*IncomingResponse) Err

func (p *IncomingResponse) Err() error

type Message

type Message struct {
	Text         string       `json:"text"`
	Notification string       `json:"notification"`
	Markdown     bool         `json:"markdown"`
	Channel      string       `json:"channel"`
	User         string       `json:"user"`
	Attachments  []Attachment `json:"attachments"`
}

type NewTriggerFunc

type NewTriggerFunc func(word string, config *configuration.Config) (Trigger, error)

type Outgoing

type Outgoing struct {
	// contains filtered or unexported fields
}

func NewOutgoing

func NewOutgoing(config *configuration.Config) (*Outgoing, error)

func (*Outgoing) BindTrigger

func (p *Outgoing) BindTrigger(config *configuration.Config) *Outgoing

func (*Outgoing) Handle

func (p *Outgoing) Handle(req *OutgoingRequest, msg *Message) error

func (*Outgoing) HandleHttpRequest

func (p *Outgoing) HandleHttpRequest(rw http.ResponseWriter, req *http.Request)

func (*Outgoing) SetErrorHandler

func (p *Outgoing) SetErrorHandler(handler ErrorHandlerFunc)

type OutgoingOption

type OutgoingOption func(*OutgoingSettings)

type OutgoingRequest

type OutgoingRequest struct {
	Token       string   `json:"token"`
	Timestamp   int      `json:"ts"`
	Text        string   `json:"text"`
	TriggerWord string   `json:"trigger_word"`
	Subdomain   string   `json:"subdomain"`
	ChannelName string   `json:"channel_name"`
	UserName    string   `json:"user_name"`
	Commands    []string `json:"-"`
}

func (*OutgoingRequest) Args

func (p *OutgoingRequest) Args() []string

type OutgoingSettings

type OutgoingSettings struct {
}

func NewOutgoingSettings

func NewOutgoingSettings(config *configuration.Config) *OutgoingSettings

type Trigger

type Trigger interface {
	Handle(*OutgoingRequest, *Message) error
}

func NewOutgoingTrigger

func NewOutgoingTrigger(word string, config *configuration.Config) (Trigger, error)

type TriggerHandleFunc

type TriggerHandleFunc func(req *OutgoingRequest, msg *Message) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址