tgolf

package
v0.0.0-...-cbea934 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

a Telegram Bot Service framework based on yanzay/tbot

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argument

type Argument struct {
	Field
	Value string
}

type CallbackHandler

type CallbackHandler = func(*tbot.CallbackQuery) error

type Command

type Command struct {
	// 继承 BotCommand
	tbot.BotCommand
	// Init 函数用于触发 starter 后的初始化和鉴权
	Init func(from *tbot.User, chat tbot.Chat) bool
	// 命令参数
	Param    []Parameter
	Callback func(argv []Argument, from *tbot.User, chatid string)
	Handler  func(*tbot.Message)
}

描述了一个命令

type Field

type Field struct {
	Key       string
	Validator func(value string) bool
}

type KVDatabase

type KVDatabase interface {
	// get value based on key
	Get(key string) interface{}
	Set(key string, value interface{}) error
	// return a subdatabase with prefix. e. g. subdb.get(key) is equal to
	// db.get(prefix + key), ant note that subdb & db share the same storage
	Sub(prefix string) KVDatabase
	// traverse all data
	ForEach(handler func(key string, value interface{}))
}

simple abstraction for key-value database

type MemoryDB

type MemoryDB struct {
	// 多线程读写锁
	*sync.RWMutex
	// contains filtered or unexported fields
}

MemoryDB implememts KVDatabase, which is a simple storage based on runtime memory

func NewMemoryDB

func NewMemoryDB() MemoryDB

func (*MemoryDB) ForEach

func (r *MemoryDB) ForEach(handler func(key string, value interface{}))

func (*MemoryDB) Get

func (r *MemoryDB) Get(key string) interface{}

func (*MemoryDB) Set

func (r *MemoryDB) Set(key string, value interface{}) error

func (*MemoryDB) Sub

func (r *MemoryDB) Sub(prefix string) KVDatabase

type Parameter

type Parameter struct {
	Field
	Description string
}

func NewParam

func NewParam(key, desc string, validator func(value string) bool) Parameter

type Server

type Server struct {
	Bot    *tbot.Server
	Client *tbot.Client
	// contains filtered or unexported fields
}

tg bot server

func NewServer

func NewServer(botToken string, webhookUrl string, listen string) Server

func NewServerFromTbot

func NewServerFromTbot(bot *tbot.Server) Server

func (*Server) EditCallbackBtn

func (r *Server) EditCallbackBtn(cq *tbot.CallbackQuery, btnMatrix [][]tbot.InlineKeyboardButton) (*tbot.Message, error)

func (*Server) EditCallbackMsg

func (r *Server) EditCallbackMsg(cq *tbot.CallbackQuery, format string, v ...interface{}) (*tbot.Message, error)

func (*Server) EditCallbackMsgWithBtn

func (r *Server) EditCallbackMsgWithBtn(cq *tbot.CallbackQuery, btnMatrix [][]tbot.InlineKeyboardButton,
	format string, v ...interface{}) (*tbot.Message, error)

func (*Server) HandleCallback

func (r *Server) HandleCallback(cq *tbot.CallbackQuery)

func (*Server) HandleMessage

func (r *Server) HandleMessage(m *tbot.Message)

func (*Server) Register

func (r *Server) Register(starter string, description string, init func(from *tbot.User, chat tbot.Chat) bool,
	params []Parameter, f func(argv []Argument, from *tbot.User, chatid string))

starter 为命令的触发字符串。若开头为 / 则会作为显示命令,否则为隐式命令。 description 可选,用于描述命令。开头为 / 的命令会以 start - description 的形式打印到日志中,方便在 Bot Father 那 setcommand。init 指在触发 start 后,获取参数前的检查(例如权限),返回值为 true 则继续获取参数,否则终止。params 描述了参数列表,包含每个参数的描述,校验器,f 即回调函数

func (*Server) RegisterInlineButton

func (r *Server) RegisterInlineButton(data string, handler CallbackHandler)

data: 按扭的 CallbackData

func (*Server) Sendf

func (r *Server) Sendf(chatid string, format string, v ...interface{}) (*tbot.Message, error)

Send formatted message to a chat with html parsing

func (*Server) SendfWithBtn

func (r *Server) SendfWithBtn(chatid string, btnMatrix [][]tbot.InlineKeyboardButton, format string, v ...interface{}) (*tbot.Message, error)

Send message with inline button

func (*Server) Start

func (r *Server) Start() error

func (*Server) StartCommand

func (r *Server) StartCommand(starter string, from *tbot.User, chat tbot.Chat) error

Jump to

Keyboard shortcuts

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