robotic

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: MIT Imports: 21 Imported by: 1

README

Robotic

安装

go get github.com/glide-im/robotic

使用

这里实现了一个简单的 echo 机器人

botX := robotic.NewBotX("ws://bot_server.address", "Token")
botX.HandleChatMessage(func(m *messages.GlideMessage, cm *messages.ChatMessage) {
    if m.GetAction() == robotic.ActionChatMessage {
        go func() {
            replyMsg := messages.ChatMessage{
                CliMid:  uuid.New().String(),
                Mid:     0,
                From:    botX.Id,
                To:      cm.From,
                Type:    cm.Type,
                Content: cm.Content,
                SendAt:  time.Now().Unix(),
            }
            _ := botX.Send(cm.From, robotic.ActionChatMessage, &replyMsg)
        }()
    }
})
err = botX.Start(nil)
panic(err)

Documentation

Index

Constants

View Source
const (
	ActionHello               messages.Action = "hello"
	ActionHeartbeat           messages.Action = "heartbeat"
	ActionNotifyUnknownAction messages.Action = "notify.unknown.action"

	ActionChatMessage       messages.Action = "message.chat"
	ActionChatMessageResend messages.Action = "message.chat.resend"
	ActionGroupMessage      messages.Action = "message.group"
	ActionMessageFailed     messages.Action = "message.failed.send"

	ActionNotifyNeedAuth      messages.Action = "notify.auth"
	ActionNotifyKickOut       messages.Action = "notify.kickout"
	ActionNotifyNewContact    messages.Action = "notify.contact"
	ActionNotifyGroup         messages.Action = "notify.group"
	ActionNotifyAccountLogin  messages.Action = "notify.login"
	ActionNotifyAccountLogout messages.Action = "notify.logout"
	ActionNotifyError         messages.Action = "notify.error"

	ActionAckRequest  messages.Action = "ack.request"
	ActionAckGroupMsg messages.Action = "ack.group.msg"
	ActionAckMessage  messages.Action = "ack.message"
	ActionAckNotify   messages.Action = "ack.notify"

	ActionApiAuth    messages.Action = "api.auth"
	ActionApiFailed  messages.Action = "api.failed"
	ActionApiSuccess messages.Action = "api.success"

	ActionClientCustom messages.Action = "message.cli"

	NotifyKickOut messages.Action = "notify.kickout"
	AckOffline    messages.Action = "ack.offline"
)
View Source
const (
	CommandPrefix    = "#"
	CommandMaxLength = 10
)
View Source
const (
	MessageTypeText  int32 = 1
	MessageTypeImage int32 = 2
	MessageTypeAudio int32 = 3
	MessageTypeVideo int32 = 4
	MessageTypeFile  int32 = 5

	MessageTypeMarkdown int32 = 11
)

Variables

View Source
var ApiBaseUrl string
View Source
var RoleController = &roleController{
	defaultRole: 1,
	userRole:    map[UserId]Role{},
}

Functions

func RequestSessionTicket added in v1.2.2

func RequestSessionTicket(to string) (string, error)

Types

type AuthResponse added in v1.2.1

type AuthResponse struct {
	Token      string      `json:"token"`
	Servers    []string    `json:"servers"`
	NickName   string      `json:"nick_name"`
	Uid        int64       `json:"uid"`
	Status     int         `json:"status"`
	Credential *Credential `json:"credential"`
}

func Login added in v1.2.1

func Login(account, password string) (*AuthResponse, error)

type BotX

type BotX struct {
	Id string

	Commands []*Command
	// contains filtered or unexported fields
}

func NewBotX

func NewBotX(wsUrl string) *BotX

func (*BotX) AddCommand added in v1.1.0

func (b *BotX) AddCommand(command *Command) error

func (*BotX) HandleChatMessage

func (b *BotX) HandleChatMessage(h func(m *messages.GlideMessage, cm *messages.ChatMessage))

func (*BotX) Reply added in v1.2.0

func (b *BotX) Reply(originMessage *ResolvedChatMessage, messageType int32, content interface{}) error

func (*BotX) RunAndLogin added in v1.2.1

func (b *BotX) RunAndLogin(email, password string, h func(m *messages.GlideMessage)) error

func (*BotX) Send

func (b *BotX) Send(to string, action messages.Action, data interface{}) error

type Command added in v1.1.0

type Command struct {
	Role   Role
	Name   string
	Desc   string
	Handle CommandHandler
	// contains filtered or unexported fields
}

func CommandHelp added in v1.2.0

func CommandHelp(x *BotX) *Command

func CommandPing added in v1.2.0

func CommandPing(x *BotX) *Command

func NewCommand added in v1.1.0

func NewCommand(role Role, name string, handle CommandHandler) (*Command, error)

func NewCommand2 added in v1.2.0

func NewCommand2(role Role, name string, desc string, handle CommandHandler) (*Command, error)

type CommandHandler added in v1.1.0

type CommandHandler func(message *ResolvedChatMessage, value string) error

type Credential added in v1.2.1

type Credential struct {
	Version    int64  `json:"version,omitempty"`
	Credential string `json:"credential,omitempty"`
}

type Data added in v1.2.1

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

func (*Data) UnmarshalJSON added in v1.2.1

func (d *Data) UnmarshalJSON(i []byte) error

type MessageHandler

type MessageHandler func(g gate.Gateway, m *messages.GlideMessage)

MessageHandler used to handle the message that robot received.

type MessageInterceptor

type MessageInterceptor interface {
	// contains filtered or unexported methods
}

type Options

type Options struct {
	Ticket string
}

type Perm added in v1.2.0

type Perm int64

type ProxyRobot

type ProxyRobot struct {
}

func (*ProxyRobot) EnqueueMessage

func (p *ProxyRobot) EnqueueMessage(message *messages.GlideMessage) error

func (*ProxyRobot) Exit

func (p *ProxyRobot) Exit()

func (*ProxyRobot) GetInfo

func (p *ProxyRobot) GetInfo() gate.Info

func (*ProxyRobot) IsRunning

func (p *ProxyRobot) IsRunning() bool

func (*ProxyRobot) Run

func (p *ProxyRobot) Run()

func (*ProxyRobot) SetID

func (p *ProxyRobot) SetID(id gate.ID)

type ResolvedChatMessage added in v1.2.0

type ResolvedChatMessage struct {
	Origin      *messages.GlideMessage
	ChatMessage *messages.ChatMessage
}

type Response

type Response struct {
	Msg  string
	Code int
	Data *Data
}

func RequestApi added in v1.2.1

func RequestApi(method string, url string, body interface{}) (*Response, error)

type Robot

type Robot struct {
	Rec chan *messages.GlideMessage
	// contains filtered or unexported fields
}

func NewRobot

func NewRobot(wsUrl string) (*Robot, error)

func (*Robot) BlockSend

func (r *Robot) BlockSend(m *messages.GlideMessage) (error, int64)

func (*Robot) Close

func (r *Robot) Close() error

func (*Robot) Enqueue

func (r *Robot) Enqueue(m *messages.GlideMessage) error

func (*Robot) Run

func (r *Robot) Run() error

type RobotConnection

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

RobotConnection .

func NewRobotConnection

func NewRobotConnection(g gate.Gateway, handler MessageHandler, opts *Options) (*RobotConnection, error)

func (*RobotConnection) EnqueueMessage

func (r *RobotConnection) EnqueueMessage(m *messages.GlideMessage) error

func (*RobotConnection) Exit

func (r *RobotConnection) Exit()

func (*RobotConnection) GetInfo

func (r *RobotConnection) GetInfo() gate.Info

func (*RobotConnection) IsRunning

func (r *RobotConnection) IsRunning() bool

func (*RobotConnection) Run

func (r *RobotConnection) Run()

func (*RobotConnection) SetID

func (r *RobotConnection) SetID(id gate.ID)

type RobotOptions

type RobotOptions struct {
}

type Role added in v1.2.0

type Role int64

func GetUserRoleFromMessage added in v1.2.0

func GetUserRoleFromMessage(message *messages.ChatMessage) Role

func (Role) IsApply added in v1.2.0

func (r Role) IsApply(other Role) bool

type RoleControllerInterface added in v1.2.0

type RoleControllerInterface interface {
	GetRules(id UserId) Role

	Apply(id UserId, role Role) error

	SetUserRole(id UserId, name Perm, enable bool)
}

type TicketResponse added in v1.2.2

type TicketResponse struct {
	Ticket string `json:"ticket"`
}

type UserId added in v1.2.0

type UserId interface{}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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