botmaid

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2019 License: MIT Imports: 18 Imported by: 1

README

botmaid

This repository holds a package includes more useful public functions for bots.

Download / Install

The easiest way to install is to run go get -u github.com/catsworld/botmaid. You can also manually git clone the repository to $GOPATH/src/catsworld/botmaid.

Documentation

Overview

Package botmaid is a package includes more useful public functions for bots.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetArgument added in v1.3.0

func GetArgument(c string) []string

GetArgument returns a slice with a command and an argument.

func In

func In(a interface{}, s ...interface{}) bool

In checks if the element is in the slice.

func SplitCommand added in v1.0.1

func SplitCommand(c string) []string

SplitCommand splits a string into a slice of string.

Types

type API

type API interface {
	GetUpdates(GetUpdatesConfig) (UpdateChannel, ErrorChannel)
	Push(Update) (Update, error)
}

API is an interface including some common behaviors for APIs.

GetUpdates always gets updates and errors into the channels with a given config.

Push always pushes an update and returns it back if existing.

Delete always deletes a specific update.

type Bot

type Bot struct {
	ID string

	API API

	Self *User

	BotMaid *BotMaid
}

Bot includes some information of a bot.

func (*Bot) At added in v1.0.1

func (b *Bot) At(u *User) []string

At returns a string to mention someone in a message.

func (*Bot) BeAt added in v1.3.0

func (b *Bot) BeAt(u *Update) bool

BeAt checks if a message of an update is mentioning the bot.

func (*Bot) IsCommand added in v1.0.1

func (b *Bot) IsCommand(u *Update, c ...string) bool

IsCommand checks if a message is a specific command.

func (*Bot) IsMaster added in v1.0.1

func (b *Bot) IsMaster(u User) bool

IsMaster checks if a user is master of the bot.

func (*Bot) IsTestChat added in v1.4.0

func (b *Bot) IsTestChat(p Chat) bool

IsTestChat checks if a chat is a test chat of the bot.

func (*Bot) Platform

func (b *Bot) Platform() string

Platform returns a string showing the platform of the bot.

func (*Bot) Reply added in v1.4.0

func (b *Bot) Reply(u *Update, s ...string) (Update, error)

Reply replies a message back.

type BotMaid

type BotMaid struct {
	Bots map[string]*Bot

	Conf *toml.Tree

	DB *sql.DB

	Commands []Command
	Timers   []Timer

	HelpMenus []HelpMenu

	Words map[string][]string

	RespTime time.Time
}

BotMaid includes a slice of Bot and some methods to use them.

func (*BotMaid) AddCommand

func (bm *BotMaid) AddCommand(c Command)

AddCommand adds a command into the []Command.

func (*BotMaid) AddTimer

func (bm *BotMaid) AddTimer(t Timer)

AddTimer adds a timer into the []Timer.

func (*BotMaid) Broadcast

func (bm *BotMaid) Broadcast(tableName string, m *Message)

Broadcast sends an update to all chats in the table.

func (*BotMaid) InitBroadcastTable added in v1.3.0

func (bm *BotMaid) InitBroadcastTable(tableName string) error

InitBroadcastTable creates a table with the standard structure of a broadcast.

func (*BotMaid) Start

func (bm *BotMaid) Start() error

Start starts the BotMaid.

func (*BotMaid) SwitchBroadcast

func (bm *BotMaid) SwitchBroadcast(tableName string, chat *Chat, b *Bot)

SwitchBroadcast switches the broadcast on/off of a chat.

type Chat

type Chat struct {
	ID int64

	Type string

	Title string
}

Chat is a struct for a chat.

type Command

type Command struct {
	Do       func(*Update, *Bot) bool
	Check    func(*Update, *Bot) bool
	Priority int

	Menu, Help   string
	Names        []string
	Master, Test bool
}

Command is a func with priority value so that we can sort some Commands to make them in a specific order.

type CommandSlice

type CommandSlice []Command

CommandSlice is a slice of Command that could be sort.

func (CommandSlice) Len

func (cs CommandSlice) Len() int

Len is the length of a CommandSlice.

func (CommandSlice) Less

func (cs CommandSlice) Less(i, j int) bool

Less returns true if CommandSlice[i] is less then CommandSlice[j].

func (CommandSlice) Swap

func (cs CommandSlice) Swap(i, j int)

Swap swaps CommandSlice[i] and CommandSlice[j].

type CoolqHTTPAPI added in v1.4.0

type CoolqHTTPAPI struct {
	AccessToken string
	Secret      string
	APIEndpoint string
}

CoolqHTTPAPI is a struct stores some basic information of the Coolq HTTP API. Please search in Coolq HTTP API document for details.

func (*CoolqHTTPAPI) API added in v1.4.0

func (a *CoolqHTTPAPI) API(end string, m map[string]interface{}) (interface{}, error)

API returns the body of an HTTP response to the Coolq HTTP API.

func (*CoolqHTTPAPI) GetUpdates added in v1.4.0

GetUpdates gets updates and errors into the channels with a given config.

func (*CoolqHTTPAPI) Push added in v1.7.0

func (a *CoolqHTTPAPI) Push(update Update) (Update, error)

Push pushes an update and returns it back if existing.

type DBBroadcastChat added in v1.4.0

type DBBroadcastChat struct {
	ID       int64
	BotID    string
	ChatType string
	ChatID   int64
}

DBBroadcastChat is a struct saved some information of the chat to broadcast.

type ErrorChannel

type ErrorChannel chan error

ErrorChannel is a channel for saving errors.

type GetUpdatesConfig added in v1.4.0

type GetUpdatesConfig struct {
	Limit            int
	Timeout          int
	RetryWaitingTime time.Duration
}

GetUpdatesConfig is a struct for getting updates.

Limit decides the number of updates pulled once. Timeout decides the timeout of long polling. RetryWaitingTime decides decides the time waiting after pulling an error.

type HelpMenu added in v1.7.0

type HelpMenu struct {
	Menu, Help string
	Names      []string
}

HelpMenu describes the menu item of the help.

type Message

type Message struct {
	ID int64

	Text  string
	Image string
	Audio string

	Args []string
}

Message is a struct for a message of an update.

type TelegramBotAPI added in v1.4.0

type TelegramBotAPI struct {
	Token  string
	Offset int64
}

TelegramBotAPI is a struct stores some basic information of the Telegram Bot API. Please search in official API document for details.

func (*TelegramBotAPI) API added in v1.4.0

func (a *TelegramBotAPI) API(end string, m map[string]interface{}) (interface{}, error)

API returns the body of an HTTP response to the Telegram Bot API.

func (*TelegramBotAPI) GetUpdates added in v1.4.0

GetUpdates gets updates and errors into the channels with a given config.

func (*TelegramBotAPI) Push added in v1.7.0

func (a *TelegramBotAPI) Push(update Update) (Update, error)

Push pushes an update and returns it back if existing.

type Timer

type Timer struct {
	Do         func()
	Start, End time.Time
	Frequency  time.Duration
}

Timer is a func with time and frequency so that we can call it at some specific time.

type Update

type Update struct {
	ID int64

	Type string

	Time time.Time

	Chat *Chat

	User *User

	Message *Message
}

Update is a struct for an update of APIs.

type UpdateChannel

type UpdateChannel chan Update

UpdateChannel is a channel for saving updates.

type User

type User struct {
	ID       int64
	UserName string

	NickName string
}

User is a struct for a user.

type Word added in v1.0.1

type Word struct {
	Word   string
	Weight int
}

Word is a string with weight so that we can choose them randomly.

type WordSlice added in v1.3.0

type WordSlice []Word

WordSlice is a slice of Word.

func (WordSlice) Random added in v1.3.0

func (ws WordSlice) Random() string

Random returns a random string from the WordSlice.

Jump to

Keyboard shortcuts

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