bot

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2018 License: MIT Imports: 19 Imported by: 1

README

bot

Is a mini framework that alow you to create a plugin or system that works across multiple chat platform.

Currently it support small portions of telegram api.

TODO:

example project:

  • fam100 a game of family feud

Still experimental, interface is not stable yet

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// OutboxBufferSize is the size of the outbox channel
	OutboxBufferSize = 100
	// OutboxWorker is the number of worker that sends message to telegram api
	OutboxWorker = 5

	// VERSION compile time info
	VERSION = ""
)

Options

View Source
var TChatTypeMap = map[string]ChatType{
	"private":    Private,
	"group":      Group,
	"supergroup": SuperGroup,
	"channel":    Channel,
}

TChatTypeMap maps betwwen string to bot.ChatType

Functions

func SetLogger

func SetLogger(l zap.Logger)

SetLogger replace the logger object

func TelegramEscape added in v0.2.0

func TelegramEscape(s string) string

TelegramEscape escapes html that is acceptable by telegram

Types

type Attachment added in v0.2.0

type Attachment struct {
	Fallback string `json:"fallback"`
	Text     string `json:"text"`
	Pretext  string `json:"pretext"`
	Title    string `json:"title"`
	ID       int64  `json:"id"`
}

type ChannelMigratedMessage

type ChannelMigratedMessage struct {
	FromID     string
	ToID       string
	ReceivedAt time.Time
	Raw        json.RawMessage `json:"-"`
}

ChannelMigratedMessage represents that a chat type has been upgraded. Currently works on telegram

type Chat

type Chat struct {
	ID       string
	Type     ChatType
	Title    string
	Username string
}

Chat represents a chat session

func (Chat) Name added in v0.2.0

func (t Chat) Name() string

Name returns the title of the bot

type ChatInfo added in v0.2.0

type ChatInfo struct {
	ID          string
	Type        ChatType
	Title       string
	Topic       string
	Description string
}

type ChatType

type ChatType string

ChatType is type of the message

const (
	Channel    ChatType = "channel"
	Group      ChatType = "group"
	Private    ChatType = "private"
	SuperGroup ChatType = "supergroup"
	Thread     ChatType = "thread"
)

Available ChatType

type Client added in v0.2.0

type Client interface {
	AddPlugins(...Plugin) error
	Start() error
	Stop()

	UserName() string // bot username
	Mentioned(field string) bool
	Mention(User) string
	FindUser(UserName string) (User, bool)
	ChatInfo(chatID string) (ChatInfo, error)
	SetTopic(chatID, topic string) error

	UploadFile(chatID string, filename string, r io.Reader) error
}

Client represent a chat client. Currently supports telegram

type JoinMessage added in v0.2.0

type JoinMessage struct {
	*Message
}

JoinMessage represents information that a user join a chat

type LeftMessage added in v0.2.0

type LeftMessage struct {
	*Message
}

LeftMessage represents information that a user left a chat

type Message

type Message struct {
	ID           string
	From         User
	Date         time.Time
	Chat         Chat
	Text         string
	Format       MessageFormat
	ReplyTo      *Message
	ReplyToID    string
	ReceivedAt   time.Time
	Attachments  []Attachment
	Raw          json.RawMessage `json:"-"`
	Retry        int             `json:"-"`
	DiscardAfter time.Time       `json:"-"`
	// contains filtered or unexported fields
}

Message represents chat message

func (*Message) Context added in v0.2.0

func (m *Message) Context() context.Context

func (*Message) WithContext added in v0.2.0

func (m *Message) WithContext(ctx context.Context) *Message

type MessageFormat

type MessageFormat string

MessageFormat represents formatting of the message

const (
	Text     MessageFormat = ""
	Markdown MessageFormat = "markdown"
	HTML     MessageFormat = "html"
)

Available MessageFormat

type Plugin

type Plugin interface {
	Name() string
	Init(out chan Message, cl Client) error
	Handle(in interface{}) (handled bool, msg interface{})
}

Plugin is pluggable module to process messages

type Slack added in v0.2.0

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

func NewSlack added in v0.2.0

func NewSlack(ctx context.Context, token string) (*Slack, error)

func (*Slack) AddPlugins added in v0.2.0

func (s *Slack) AddPlugins(plugins ...Plugin) error

func (*Slack) ChatInfo added in v0.2.0

func (s *Slack) ChatInfo(chatID string) (ChatInfo, error)

func (*Slack) EmulateReceiveMessage added in v0.2.0

func (s *Slack) EmulateReceiveMessage(raw []byte) error

func (*Slack) FindUser added in v0.2.0

func (s *Slack) FindUser(username string) (User, bool)

func (*Slack) Mention added in v0.2.0

func (s *Slack) Mention(u User) string

func (*Slack) Mentioned added in v0.2.0

func (s *Slack) Mentioned(field string) bool

func (*Slack) SetTopic added in v0.2.0

func (s *Slack) SetTopic(chatID string, topic string) error

func (*Slack) Start added in v0.2.0

func (s *Slack) Start() error

func (*Slack) Stop added in v0.2.0

func (s *Slack) Stop()

func (*Slack) UploadFile added in v0.2.0

func (s *Slack) UploadFile(chatID string, filename string, r io.Reader) error

func (*Slack) UserName added in v0.2.0

func (s *Slack) UserName() string

type TChat

type TChat struct {
	Type  string `json:"type"`
	Title string `json:"title"`
	TUser
}

TChat represents Telegram chat session

type TChatMember added in v0.2.0

type TChatMember struct {
	User   TUser `json:"user"`
	Status string
}

TChatMember represent user membership of a group

type TError added in v0.2.0

type TError struct {
	ErrorCode   int64  `json:"error_code,omitempty"`
	Description string `json:"description"`
}

TError error response structure

func (TError) Error added in v0.2.0

func (t TError) Error() string

type TMessage

type TMessage struct {
	MessageID       int64           `json:"message_id"`
	From            TUser           `json:"from"`
	Date            int64           `json:"date"`
	Chat            TChat           `json:"chat"`
	Text            string          `json:"text"`
	ParseMode       string          `json:"parse_mode,omitempty"`
	MigrateToChatID *int64          `json:"migrate_to_chat_id,omitempty"`
	ReplyTo         *TMessage       `json:"reply_to_message,omitempty"`
	NewChatMember   *TUser          `json:"new_chat_member,omitempty"`
	LeftChatMember  *TUser          `json:"left_chat_member,omitempty"`
	ReceivedAt      time.Time       `json:"-"`
	Raw             json.RawMessage `json:"-"`
}

TMessage is Telegram incomming message

func (*TMessage) ToMessage added in v0.2.0

func (m *TMessage) ToMessage() *Message

ToMessage converts TMessage to *bot.Message

func (*TMessage) ToMigratedMessage added in v0.2.0

func (m *TMessage) ToMigratedMessage() ChannelMigratedMessage

ToMigratedMessage converts Telegram Message to bot.ChannelMigratedMessage

type TOutMessage

type TOutMessage struct {
	ChatID           string `json:"chat_id"`
	Text             string `json:"text"`
	ParseMode        string `json:"parse_mode,omitempty"`
	ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"`
}

TOutMessage is Telegram outgoing message

type TResponse

type TResponse struct {
	Ok     bool            `json:"ok"`
	Result json.RawMessage `json:"result,omitempty"`
	TError
}

TResponse represents response from telegram

type TUpdate

type TUpdate struct {
	UpdateID int64           `json:"update_id"`
	Message  json.RawMessage `json:"message"`
}

TUpdate represents an update event from telegram

type TUser

type TUser struct {
	ID        int64  `json:"id"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Username  string `json:"username"`
}

TUser is Telegram User

func (TUser) ToUser added in v0.2.0

func (u TUser) ToUser() User

ToUser converts to bot.User

type Telegram

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

Telegram API

func NewTelegram

func NewTelegram(ctx context.Context, key string) (*Telegram, error)

NewTelegram creates telegram API Client

func (*Telegram) AddPlugins added in v0.2.0

func (t *Telegram) AddPlugins(plugins ...Plugin) error

AddPlugin add processing module to telegram

func (*Telegram) Chat added in v0.2.0

func (t *Telegram) Chat(id string) (*TChat, error)

Chat gets chat information based on chatID

func (*Telegram) ChatInfo added in v0.2.0

func (t *Telegram) ChatInfo(chatID string) (ChatInfo, error)

func (*Telegram) FindUser added in v0.2.0

func (t *Telegram) FindUser(username string) (User, bool)

func (*Telegram) Kick added in v0.2.0

func (t *Telegram) Kick(chatID, userID string) error

Kick userID from chatID

func (*Telegram) Leave

func (t *Telegram) Leave(chatID string) error

Leave a chat

func (*Telegram) Member added in v0.2.0

func (t *Telegram) Member(chatID, userID string) (*TChatMember, error)

Member check if userID is member of chatID

func (*Telegram) MembersCount added in v0.2.0

func (t *Telegram) MembersCount(chatID string) (int, error)

MembersCount gets the counts of member for a chat id

func (*Telegram) Mention added in v0.2.0

func (t *Telegram) Mention(u User) string

func (*Telegram) Mentioned added in v0.2.0

func (t *Telegram) Mentioned(field string) bool

func (*Telegram) SetTopic added in v0.2.0

func (t *Telegram) SetTopic(chatID string, topic string) error

func (*Telegram) Start

func (t *Telegram) Start() error

Start consuming from telegram

func (*Telegram) Stop added in v0.2.0

func (t *Telegram) Stop()

func (*Telegram) Unban added in v0.2.0

func (t *Telegram) Unban(chatID, userID string) error

Unban userID from chatID

func (*Telegram) UploadFile added in v0.2.0

func (t *Telegram) UploadFile(chatID string, filename string, r io.Reader) error

func (*Telegram) UserName added in v0.2.0

func (t *Telegram) UserName() string

Username returns bot's username

type User

type User struct {
	ID        string
	FirstName string
	LastName  string
	Username  string
}

User represents user information

func (User) FullName

func (u User) FullName() string

FullName returns first name + last name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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