bearychat

package module
v0.0.0-...-62b6810 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2018 License: MIT Imports: 13 Imported by: 5

README

bearychat-go

BearyChat API with go.

Build Status GoDoc GoReport Development Status

Usage

go 1.7+

go get github.com/bearyinnovative/bearychat-go

Examples

LICENSE

MIT

Documentation

Index

Examples

Constants

View Source
const (
	UserRoleOwner   = "owner"
	UserRoleAdmin   = "admin"
	UserRoleNormal  = "normal"
	UserRoleVisitor = "visitor"
)
View Source
const (
	UserTypeNormal    = "normal"
	UserTypeAssistant = "assistant"
	UserTypeHubot     = "hubot"
)
View Source
const (
	RTMMessageTypeUnknown              RTMMessageType = "unknown"
	RTMMessageTypePing                                = "ping"
	RTMMessageTypePong                                = "pong"
	RTMMessageTypeReply                               = "reply"
	RTMMessageTypeOk                                  = "ok"
	RTMMessageTypeP2PMessage                          = "message"
	RTMMessageTypeP2PTyping                           = "typing"
	RTMMessageTypeChannelMessage                      = "channel_message"
	RTMMessageTypeChannelTyping                       = "channel_typing"
	RTMMessageTypeUpdateUserConnection                = "update_user_connection"
)
View Source
const (
	DEFAULT_RTM_API_BASE = "https://rtm.bearychat.com"
)

Variables

View Source
var (
	ErrRTMLoopClosed = errors.New("rtm loop is closed")
)

Functions

func NewIncomingWebhookClient

func NewIncomingWebhookClient(webhook string) *webhookClient

Creates a new incoming webhook client.

For full documentation, visit https://bearychat.com/integrations/incoming .

Example
m := Incoming{Text: "Hello, BearyChat"}
payload, _ := m.Build()
resp, _ := NewIncomingWebhookClient("YOUR WEBHOOK URL").Send(payload)
if resp.IsOk() {
	// parse resp result
} else {
	// parse resp error
}
Output:

func NewRTMLoop

func NewRTMLoop(wsHost string, setters ...rtmLoopSetter) (*rtmLoop, error)

func WithRTMAPIBase

func WithRTMAPIBase(apiBase string) rtmOptSetter

WithRTMAPIBase can be used to set rtm client's base api.

func WithRTMHTTPClient

func WithRTMHTTPClient(httpClient *http.Client) rtmOptSetter

WithRTMHTTPClient sets http client.

func WithRTMLoopBacklog

func WithRTMLoopBacklog(backlog int) rtmLoopSetter

Set RTM message chan backlog.

Types

type Channel

type Channel struct {
	Id         string `json:"id"`
	TeamId     string `json:"team_id"`
	UserId     string `json:"uid"`
	VChannelId string `json:"vchannel_id"`
	Name       string `json:"name"`
	IsPrivate  bool   `json:"private"`
	IsGeneral  bool   `json:"general"`
	Topic      string `json:"topic"`
	CreatedAt  string `json:"created"` // TODO parse date
	UpdatedAt  string `json:"updated"` // TODO parse date
}

Channel information.

type Incoming

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

Incoming message builder.

m := Incoming{
        Text: "Hello, **BearyChat**",
        Markdown: true,
        Notification: "Hello, BearyChat in Notification",
}
output, _ := m.Build()
http.Post("YOUR INCOMING HOOK URI HERE", "application/json", output)

For full documentation, visit https://bearychat.com/integrations/incoming .

Example
m := Incoming{
	Text:         "Hello, **BearyChat",
	Notification: "Hello, BearyChat in notification",
	Markdown:     true,
	Channel:      "#所有人",
	User:         "@bearybot",
	Attachments: []IncomingAttachment{
		{Text: "attachment 1", Color: "#cb3f20"},
		{Title: "attachment 2", Color: "#ffa500"},
		{
			Text: "愿原力与你同在",
			Images: []IncomingAttachmentImage{
				{URL: "http://img3.douban.com/icon/ul15067564-30.jpg"},
			},
		},
	},
}

m.Build()
Output:

func (Incoming) Build

func (m Incoming) Build() (io.Reader, error)

Build an incoming message.

func (Incoming) Validate

func (m Incoming) Validate() error

Validate fields.

type IncomingAttachment

type IncomingAttachment struct {
	Title  string                    `json:"title,omitempty"`
	Text   string                    `json:"text,omitempty"`
	Color  string                    `json:"color,omitempty"`
	Images []IncomingAttachmentImage `json:"images,omitempty"`
}

IncomingAttachment contains incoming attachment fields.

func (IncomingAttachment) Validate

func (a IncomingAttachment) Validate() error

Validate fields.

type IncomingAttachmentImage

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

IncomingAttachmentImage contains attachment image fields.

func (IncomingAttachmentImage) Validate

func (i IncomingAttachmentImage) Validate() error

Validate fields.

type RTMAPIResponse

type RTMAPIResponse struct {
	Code        int             `json:"code"`
	Result      json.RawMessage `json:"result,omitempty"`
	ErrorReason string          `json:"error,omitempty"`
}

RTM api request response

func (*RTMAPIResponse) Error

func (r *RTMAPIResponse) Error() string

type RTMChannelService

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

func (*RTMChannelService) Info

func (s *RTMChannelService) Info(channelId string) (*Channel, error)

type RTMClient

type RTMClient struct {
	// rtm token
	Token string

	// rtm api base, defaults to `https://rtm.bearychat.com`
	APIBase string

	// services
	CurrentTeam *RTMCurrentTeamService
	User        *RTMUserService
	Channel     *RTMChannelService
	// contains filtered or unexported fields
}

RTMClient is used to interactive with BearyChat's RTM api and websocket message protocol.

func NewRTMClient

func NewRTMClient(token string, setters ...rtmOptSetter) (*RTMClient, error)

NewRTMClient creates a rtm client.

client, _ := NewRTMClient(
        "rtm-token",
        WithRTMAPIBase("https://rtm.bearychat.com"),
)

func (RTMClient) Do

func (c RTMClient) Do(resource, method string, in, result interface{}) (*http.Response, error)

Do performs an api request.

func (RTMClient) Get

func (c RTMClient) Get(resource string, result interface{}) (*http.Response, error)

func (RTMClient) Incoming

func (c RTMClient) Incoming(m RTMIncoming) error

Incoming performs rtm.message

func (RTMClient) Post

func (c RTMClient) Post(resource string, in, result interface{}) (*http.Response, error)

func (RTMClient) Start

func (c RTMClient) Start() (*User, string, error)

Start performs rtm.start

type RTMContext

type RTMContext struct {
	Loop RTMLoop
	// contains filtered or unexported fields
}

func NewRTMContext

func NewRTMContext(token string) (*RTMContext, error)

func (*RTMContext) Run

func (c *RTMContext) Run() (error, chan RTMMessage, chan error)

func (*RTMContext) UID

func (c *RTMContext) UID() string

type RTMCurrentTeamService

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

func (*RTMCurrentTeamService) Channels

func (s *RTMCurrentTeamService) Channels() ([]*Channel, error)

Retrieves current team's channels.

func (*RTMCurrentTeamService) Info

func (s *RTMCurrentTeamService) Info() (*Team, error)

Retrieves current team's information.

func (*RTMCurrentTeamService) Members

func (s *RTMCurrentTeamService) Members() ([]*User, error)

Retrieves current team's members.

type RTMIncoming

type RTMIncoming struct {
	Text        string               `json:"text"`
	VChannelId  string               `json:"vchannel"`
	Markdown    bool                 `json:"markdown,omitempty"`
	Attachments []IncomingAttachment `json:"attachments,omitempty"`
}

RTMIncoming represents message sent vai `rtm.message` api

type RTMLoop

type RTMLoop interface {
	// Connect to RTM, returns after connected
	Start() error
	// Stop the connection
	Stop() error
	// Get current state
	State() RTMLoopState
	// Send a ping message
	Ping() error
	// Keep connection alive. Closes ticker before return
	Keepalive(interval *time.Ticker) error
	// Send a message
	Send(m RTMMessage) error
	// Get message receiving channel
	ReadC() (chan RTMMessage, error)
	// Get error channel
	ErrC() chan error
}

RTMLoop is used to interactive with BearyChat's RTM websocket message protocol.

type RTMLoopState

type RTMLoopState string
const (
	RTMLoopStateClosed RTMLoopState = "closed"
	RTMLoopStateOpen                = "open"
)

type RTMMessage

type RTMMessage map[string]interface{}

RTMMessage represents a message entity send over RTM protocol.

func (RTMMessage) IsChatMessage

func (m RTMMessage) IsChatMessage() bool

func (RTMMessage) IsFromUID

func (m RTMMessage) IsFromUID(uid string) bool

func (RTMMessage) IsFromUser

func (m RTMMessage) IsFromUser(u User) bool

func (RTMMessage) IsP2P

func (m RTMMessage) IsP2P() bool

func (RTMMessage) ParseMentionUID

func (m RTMMessage) ParseMentionUID(uid string) (bool, string)

func (RTMMessage) ParseMentionUser

func (m RTMMessage) ParseMentionUser(u User) (bool, string)

func (RTMMessage) Refer

func (m RTMMessage) Refer(text string) RTMMessage

Refer a message

func (RTMMessage) Reply

func (m RTMMessage) Reply(text string) RTMMessage

Reply a message (with copying type, vchannel_id)

func (RTMMessage) Text

func (m RTMMessage) Text() string

func (RTMMessage) Type

func (m RTMMessage) Type() RTMMessageType

type RTMMessageType

type RTMMessageType string

type RTMUserService

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

func (*RTMUserService) Info

func (s *RTMUserService) Info(userId string) (*User, error)

type Team

type Team struct {
	Id          string `json:"id"`
	Subdomain   string `json:"subdomain"`
	Name        string `json:"name"`
	UserId      string `json:"uid"`
	Description string `json:"description"`
	EmailDomain string `json:"email_domain"`
	Inactive    bool   `json:"inactive"`
	CreatedAt   string `json:"created"` // TODO parse date
	UpdatedAt   string `json:"updated"` // TODO parse date
}

Team information

type User

type User struct {
	Id         string `json:"id"`
	TeamId     string `json:"team_id"`
	VChannelId string `json:"vchannel_id"`
	Name       string `json:"name"`
	FullName   string `json:"full_name"`
	Email      string `json:"email"`
	AvatarUrl  string `json:"avatar_url"`
	Role       string `json:"role"`
	Type       string `json:"type"`
	Conn       string `json:"conn"`
	CreatedAt  string `json:"created"` // TODO parse date
	UpdatedAt  string `json:"updated"` // TODO parse date
}

User information

func (User) IsNormal

func (u User) IsNormal() bool

IsNormal tells if this user a normal user (owner, admin or normal)

func (User) IsOnline

func (u User) IsOnline() bool

IsOnline tells user connection status.

type WebhookClient

type WebhookClient interface {
	// Set webhook webhook.
	SetWebhook(webhook string) WebhookClient

	// Set http client.
	SetHTTPClient(client *http.Client) WebhookClient

	// Send webhook payload.
	Send(payload io.Reader) (*WebhookResponse, error)
}

WebhookClient represents any webhook client can send message to BearyChat.

type WebhookResponse

type WebhookResponse struct {
	StatusCode int              `json:"-"`
	Code       int              `json:"code"`
	Error      string           `json:"error,omitempty"`
	Result     *json.RawMessage `json:"result"`
}

WebhookResponse represents a response.

func (WebhookResponse) IsOk

func (w WebhookResponse) IsOk() bool

Directories

Path Synopsis
example
chaosmonkey
Chaos Monkey will talk to you randomly 🙊 Envvars: - `CM_RTM_TOKEN`: BearyChat RTM token - `CM_VICTIMS`: user ids who will be talk with, separates with comma: `=bw52O,=bw52P`
Chaos Monkey will talk to you randomly 🙊 Envvars: - `CM_RTM_TOKEN`: BearyChat RTM token - `CM_VICTIMS`: user ids who will be talk with, separates with comma: `=bw52O,=bw52P`
rtm
Demo bot built with BearyChat RTM ./rtm -rtmToken <your-BearyChat-RTM-token-here>
Demo bot built with BearyChat RTM ./rtm -rtmToken <your-BearyChat-RTM-token-here>
Package openapi implements BearyChat's OpenAPI methods.
Package openapi implements BearyChat's OpenAPI methods.

Jump to

Keyboard shortcuts

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