Documentation
¶
Overview ¶
Package telegram 提供 Telegram Bot 消息操作的统一封装 所有 Bot 消息发送/编辑/删除操作必须通过此包调用,禁止直接调用原始 API
Index ¶
- func AnswerCallback(ctx context.Context, b *bot.Bot, callbackID string) (bool, error)
- func AnswerCallbackWithAlert(ctx context.Context, b *bot.Bot, callbackID string, text string) (bool, error)
- func AnswerCallbackWithText(ctx context.Context, b *bot.Bot, callbackID string, text string) (bool, error)
- func BackButton(callbackData string) models.InlineKeyboardButton
- func BuildGrid(items []string, columns int, prefix string) *models.InlineKeyboardMarkup
- func Button(text, callbackData string) models.InlineKeyboardButton
- func CancelButton() models.InlineKeyboardButton
- func DeleteMessage(ctx context.Context, b *bot.Bot, chatID int64, messageID int) (bool, error)
- func EditError(ctx context.Context, b *bot.Bot, chatID int64, messageID int, errCode string)
- func EditInlineMessage(ctx context.Context, b *bot.Bot, inlineMessageID string, text string)
- func EditInlineMessageWithKeyboard(ctx context.Context, b *bot.Bot, inlineMessageID string, text string, ...)
- func EditMessage(ctx context.Context, b *bot.Bot, chatID int64, messageID int, text string) (*models.Message, error)
- func EditMessageWithKeyboard(ctx context.Context, b *bot.Bot, chatID int64, messageID int, text string, ...) (*models.Message, error)
- func MainMenuButton() models.InlineKeyboardButton
- func SendError(ctx context.Context, b *bot.Bot, chatID int64, errCode string)
- func SendMessage(ctx context.Context, b *bot.Bot, chatID int64, text string) (*models.Message, error)
- func SendMessageWithKeyboard(ctx context.Context, b *bot.Bot, chatID int64, text string, ...) (*models.Message, error)
- func SendPhoto(ctx context.Context, b *bot.Bot, chatID int64, photo *models.InputFileString, ...) (*models.Message, error)
- func SendPhotoBytes(ctx context.Context, b *bot.Bot, chatID int64, photoData []byte, ...) (*models.Message, error)
- func SwitchInlineButton(text, query string) models.InlineKeyboardButton
- func SwitchInlineCurrentChatButton(text, query string) models.InlineKeyboardButton
- func URLButton(text, url string) models.InlineKeyboardButton
- type InlineKeyboardBuilder
- type Session
- type SessionService
- func (s *SessionService) Clear(ctx context.Context, userID int64) error
- func (s *SessionService) Get(ctx context.Context, userID int64) *Session
- func (s *SessionService) GetData(ctx context.Context, userID int64, key string) (interface{}, bool)
- func (s *SessionService) GetDataFloat(ctx context.Context, userID int64, key string) float64
- func (s *SessionService) GetDataString(ctx context.Context, userID int64, key string) string
- func (s *SessionService) GetStep(ctx context.Context, userID int64) (module, step string)
- func (s *SessionService) IsIdle(ctx context.Context, userID int64) bool
- func (s *SessionService) SetData(ctx context.Context, userID int64, key string, value interface{}) error
- func (s *SessionService) SetStep(ctx context.Context, userID int64, module, step string) error
- func (s *SessionService) SetStepWithData(ctx context.Context, userID int64, module, step string, ...) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnswerCallback ¶
AnswerCallback 响应内联按钮回调 (无提示)
func AnswerCallbackWithAlert ¶
func AnswerCallbackWithAlert(ctx context.Context, b *bot.Bot, callbackID string, text string) (bool, error)
AnswerCallbackWithAlert 响应内联按钮回调 (弹窗提示)
func AnswerCallbackWithText ¶
func AnswerCallbackWithText(ctx context.Context, b *bot.Bot, callbackID string, text string) (bool, error)
AnswerCallbackWithText 响应内联按钮回调 (Toast 提示)
func BackButton ¶
func BackButton(callbackData string) models.InlineKeyboardButton
BackButton 返回按钮 (通用)
func BuildGrid ¶
func BuildGrid(items []string, columns int, prefix string) *models.InlineKeyboardMarkup
BuildGrid 基于列表数据动态生成网格键盘 items: 按钮文本列表 columns: 每行列数 prefix: callback_data 前缀,最终格式为 prefix_index
func DeleteMessage ¶
DeleteMessage 删除消息
func EditInlineMessage ¶
EditInlineMessage 编辑 inline 模式消息文本 (HTML 格式) 用于编辑通过 inline query 发送的消息,使用 InlineMessageID 而非 ChatID+MessageID
func EditInlineMessageWithKeyboard ¶
func EditInlineMessageWithKeyboard(ctx context.Context, b *bot.Bot, inlineMessageID string, text string, keyboard *models.InlineKeyboardMarkup)
EditInlineMessageWithKeyboard 编辑 inline 模式消息文本并更新键盘 (HTML 格式)
func EditMessage ¶
func EditMessage(ctx context.Context, b *bot.Bot, chatID int64, messageID int, text string) (*models.Message, error)
EditMessage 编辑消息文本 (HTML 格式)
func EditMessageWithKeyboard ¶
func EditMessageWithKeyboard(ctx context.Context, b *bot.Bot, chatID int64, messageID int, text string, keyboard *models.InlineKeyboardMarkup) (*models.Message, error)
EditMessageWithKeyboard 编辑消息文本并更新内联键盘 (HTML 格式)
func SendMessage ¶
func SendMessage(ctx context.Context, b *bot.Bot, chatID int64, text string) (*models.Message, error)
SendMessage 发送纯文本消息 (HTML 格式)
func SendMessageWithKeyboard ¶
func SendMessageWithKeyboard(ctx context.Context, b *bot.Bot, chatID int64, text string, keyboard *models.InlineKeyboardMarkup) (*models.Message, error)
SendMessageWithKeyboard 发送带内联键盘的消息 (HTML 格式)
func SendPhoto ¶
func SendPhoto(ctx context.Context, b *bot.Bot, chatID int64, photo *models.InputFileString, caption string, keyboard *models.InlineKeyboardMarkup) (*models.Message, error)
SendPhoto 发送图片消息
func SendPhotoBytes ¶
func SendPhotoBytes(ctx context.Context, b *bot.Bot, chatID int64, photoData []byte, filename string, caption string, keyboard *models.InlineKeyboardMarkup) (*models.Message, error)
SendPhotoBytes 发送图片消息 (字节流)
func SwitchInlineButton ¶
func SwitchInlineButton(text, query string) models.InlineKeyboardButton
SwitchInlineButton 创建选择聊天内联查询按钮 注意: 同 SwitchInlineCurrentChatButton,空字符串需要零宽空格 workaround
func SwitchInlineCurrentChatButton ¶
func SwitchInlineCurrentChatButton(text, query string) models.InlineKeyboardButton
SwitchInlineCurrentChatButton 创建当前聊天内联查询按钮 注意: go-telegram/bot 的 SwitchInlineQueryCurrentChat 字段使用 omitempty, 空字符串会被 JSON 序列化丢弃导致按钮无效。使用零宽空格作为 workaround。
Types ¶
type InlineKeyboardBuilder ¶
type InlineKeyboardBuilder struct {
// contains filtered or unexported fields
}
InlineKeyboardBuilder 内联键盘构建器,支持链式调用
func NewInlineKeyboard ¶
func NewInlineKeyboard() *InlineKeyboardBuilder
NewInlineKeyboard 创建内联键盘构建器
func (*InlineKeyboardBuilder) Build ¶
func (b *InlineKeyboardBuilder) Build() *models.InlineKeyboardMarkup
Build 构建内联键盘
func (*InlineKeyboardBuilder) Row ¶
func (b *InlineKeyboardBuilder) Row(buttons ...models.InlineKeyboardButton) *InlineKeyboardBuilder
Row 添加一行按钮
type Session ¶
type Session struct {
Module string `json:"module"` // 当前模块 (Wallet/Transfer/Exchange 等)
Step string `json:"step"` // 当前步骤
Data map[string]interface{} `json:"data"` // 临时数据
}
Session 用户会话数据
type SessionService ¶
type SessionService struct {
// contains filtered or unexported fields
}
SessionService Redis 会话管理服务
func NewSessionService ¶
func NewSessionService(rdb *redis.Client) *SessionService
NewSessionService 创建会话管理服务
func (*SessionService) Clear ¶
func (s *SessionService) Clear(ctx context.Context, userID int64) error
Clear 清除用户会话
func (*SessionService) Get ¶
func (s *SessionService) Get(ctx context.Context, userID int64) *Session
Get 获取用户当前会话 如果不存在返回空闲状态会话
func (*SessionService) GetDataFloat ¶
GetDataFloat 获取临时数据为浮点数
func (*SessionService) GetDataString ¶
GetDataString 获取临时数据为字符串
func (*SessionService) GetStep ¶
func (s *SessionService) GetStep(ctx context.Context, userID int64) (module, step string)
GetStep 获取当前步骤
func (*SessionService) IsIdle ¶
func (s *SessionService) IsIdle(ctx context.Context, userID int64) bool
IsIdle 判断用户是否处于空闲状态 (无进行中的会话)
func (*SessionService) SetData ¶
func (s *SessionService) SetData(ctx context.Context, userID int64, key string, value interface{}) error
SetData 存储临时数据
func (*SessionService) SetStepWithData ¶
func (s *SessionService) SetStepWithData(ctx context.Context, userID int64, module, step string, data map[string]interface{}) error
SetStepWithData 设置步骤同时写入数据