Added new telegram bot library package
This commit is contained in:
parent
8928e8494a
commit
bcab566f41
|
|
@ -0,0 +1,68 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
tg "github.com/temoon/telegram-bots-api"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type TgBotClient struct {
|
||||
bot *tg.Bot
|
||||
config *BotConfig
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
var (
|
||||
onceTgBotClient sync.Once
|
||||
tgBotClientInstance *TgBotClient
|
||||
)
|
||||
|
||||
func NewTgBotClient(config *BotConfig) (*TgBotClient, error) {
|
||||
if config == nil || config.BotKey == "" {
|
||||
return nil, errors.New("failed to init TgBotClient: Api key is nil")
|
||||
}
|
||||
|
||||
var initErr error
|
||||
onceTgBotClient.Do(func() {
|
||||
bot := tg.NewBot(tg.BotOpts{
|
||||
Token: config.BotKey,
|
||||
Timeout: 30 * time.Second,
|
||||
Env: tg.EnvProduction,
|
||||
})
|
||||
|
||||
if bot == nil {
|
||||
initErr = errors.New("failed to create bot instance")
|
||||
return
|
||||
}
|
||||
|
||||
tgBotClientInstance = &TgBotClient{
|
||||
bot: bot,
|
||||
config: config,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
|
||||
logrus.Info("✅ Telegram BOT API success connected")
|
||||
})
|
||||
|
||||
if initErr != nil {
|
||||
return nil, initErr
|
||||
}
|
||||
|
||||
return tgBotClientInstance, nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) GetBot() *tg.Bot {
|
||||
return c.bot
|
||||
}
|
||||
|
||||
func (c *TgBotClient) WithContext(ctx context.Context) *TgBotClient {
|
||||
return &TgBotClient{
|
||||
bot: c.bot,
|
||||
config: c.config,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,213 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
tg "github.com/temoon/telegram-bots-api"
|
||||
)
|
||||
|
||||
// ============ INLINE KEYBOARD ============
|
||||
|
||||
type InlineKeyboardBuilder struct {
|
||||
rows [][]tg.InlineKeyboardButton
|
||||
}
|
||||
|
||||
func NewInlineKeyboard() *InlineKeyboardBuilder {
|
||||
return &InlineKeyboardBuilder{
|
||||
rows: make([][]tg.InlineKeyboardButton, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (kb *InlineKeyboardBuilder) AddRow(buttons ...tg.InlineKeyboardButton) *InlineKeyboardBuilder {
|
||||
kb.rows = append(kb.rows, buttons)
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *InlineKeyboardBuilder) Build() tg.InlineKeyboardMarkup {
|
||||
return tg.InlineKeyboardMarkup{
|
||||
InlineKeyboard: kb.rows,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButton создаёт кнопку с callback data
|
||||
func NewInlineButton(text, callbackData string) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
CallbackData: &callbackData,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonURL создаёт кнопку с URL
|
||||
func NewInlineButtonURL(text, url string) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
Url: &url,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonWebApp создаёт кнопку WebApp
|
||||
func NewInlineButtonWebApp(text string, webApp tg.WebAppInfo) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
WebApp: &webApp,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonLogin создаёт кнопку Login
|
||||
func NewInlineButtonLogin(text string, loginURL tg.LoginUrl) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
LoginUrl: &loginURL,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonSwitchInlineQuery создаёт кнопку переключения на inline режим
|
||||
func NewInlineButtonSwitchInlineQuery(text, query string) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
SwitchInlineQuery: &query,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonSwitchInlineQueryCurrentChat создаёт кнопку переключения в текущем чате
|
||||
func NewInlineButtonSwitchInlineQueryCurrentChat(text, query string) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
SwitchInlineQueryCurrentChat: &query,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonGame создаёт кнопку игры
|
||||
func NewInlineButtonGame(text string, callbackGame tg.CallbackGame) tg.InlineKeyboardButton {
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
CallbackGame: &callbackGame,
|
||||
}
|
||||
}
|
||||
|
||||
// NewInlineButtonPay создаёт кнопку оплаты
|
||||
func NewInlineButtonPay(text string) tg.InlineKeyboardButton {
|
||||
pay := true
|
||||
return tg.InlineKeyboardButton{
|
||||
Text: text,
|
||||
Pay: &pay,
|
||||
}
|
||||
}
|
||||
|
||||
// ============ REPLY KEYBOARD ============
|
||||
|
||||
type ReplyKeyboardBuilder struct {
|
||||
rows [][]tg.KeyboardButton
|
||||
resize bool
|
||||
oneTime bool
|
||||
inputFieldPlaceholder string
|
||||
selective bool
|
||||
isPersistent bool
|
||||
}
|
||||
|
||||
func NewReplyKeyboard() *ReplyKeyboardBuilder {
|
||||
return &ReplyKeyboardBuilder{
|
||||
rows: make([][]tg.KeyboardButton, 0),
|
||||
resize: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) AddRow(buttons ...tg.KeyboardButton) *ReplyKeyboardBuilder {
|
||||
kb.rows = append(kb.rows, buttons)
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) SetResize(resize bool) *ReplyKeyboardBuilder {
|
||||
kb.resize = resize
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) SetOneTime(oneTime bool) *ReplyKeyboardBuilder {
|
||||
kb.oneTime = oneTime
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) SetPlaceholder(text string) *ReplyKeyboardBuilder {
|
||||
kb.inputFieldPlaceholder = text
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) SetSelective(selective bool) *ReplyKeyboardBuilder {
|
||||
kb.selective = selective
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) SetPersistent(persistent bool) *ReplyKeyboardBuilder {
|
||||
kb.isPersistent = persistent
|
||||
return kb
|
||||
}
|
||||
|
||||
func (kb *ReplyKeyboardBuilder) Build() tg.ReplyKeyboardMarkup {
|
||||
markup := tg.ReplyKeyboardMarkup{
|
||||
Keyboard: kb.rows,
|
||||
ResizeKeyboard: &kb.resize,
|
||||
OneTimeKeyboard: &kb.oneTime,
|
||||
InputFieldPlaceholder: &kb.inputFieldPlaceholder,
|
||||
Selective: &kb.selective,
|
||||
IsPersistent: &kb.isPersistent,
|
||||
}
|
||||
return markup
|
||||
}
|
||||
|
||||
// NewReplyButton создаёт обычную кнопку
|
||||
func NewReplyButton(text string) tg.KeyboardButton {
|
||||
return tg.KeyboardButton{
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplyButtonContact создаёт кнопку запроса контакта
|
||||
func NewReplyButtonContact(text string) tg.KeyboardButton {
|
||||
requestContact := true
|
||||
return tg.KeyboardButton{
|
||||
Text: text,
|
||||
RequestContact: &requestContact,
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplyButtonLocation создаёт кнопку запроса локации
|
||||
func NewReplyButtonLocation(text string) tg.KeyboardButton {
|
||||
requestLocation := true
|
||||
return tg.KeyboardButton{
|
||||
Text: text,
|
||||
RequestLocation: &requestLocation,
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplyButtonPoll создаёт кнопку создания опроса
|
||||
func NewReplyButtonPoll(text string, pollType string) tg.KeyboardButton {
|
||||
return tg.KeyboardButton{
|
||||
Text: text,
|
||||
RequestPoll: &tg.KeyboardButtonPollType{
|
||||
Type: &pollType,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplyButtonWebApp создаёт кнопку WebApp
|
||||
func NewReplyButtonWebApp(text string, webApp tg.WebAppInfo) tg.KeyboardButton {
|
||||
return tg.KeyboardButton{
|
||||
Text: text,
|
||||
WebApp: &webApp,
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveKeyboard создаёт маркер для удаления клавиатуры
|
||||
func RemoveKeyboard(selective bool) tg.ReplyKeyboardRemove {
|
||||
return tg.ReplyKeyboardRemove{
|
||||
RemoveKeyboard: true,
|
||||
Selective: &selective,
|
||||
}
|
||||
}
|
||||
|
||||
// ForceReply создаёт маркер принудительного ответа
|
||||
func ForceReply(placeholder string, selective bool) tg.ForceReply {
|
||||
return tg.ForceReply{
|
||||
ForceReply: true,
|
||||
InputFieldPlaceholder: &placeholder,
|
||||
Selective: &selective,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,512 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
tg "github.com/temoon/telegram-bots-api"
|
||||
"github.com/temoon/telegram-bots-api/requests"
|
||||
)
|
||||
|
||||
func (c *TgBotClient) SendMessage(params MessageParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
req := &requests.SendMessage{
|
||||
ChatId: chatID,
|
||||
Text: params.Text,
|
||||
}
|
||||
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.Entities) > 0 {
|
||||
req.Entities = params.Entities
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) SendPhoto(params PhotoParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
req := &requests.SendPhoto{
|
||||
ChatId: chatID,
|
||||
Photo: c.prepareInputFile(params.Photo, params.ChatID, "photo"),
|
||||
}
|
||||
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) SendDocument(params DocumentParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
req := &requests.SendDocument{
|
||||
ChatId: chatID,
|
||||
Document: c.prepareInputFile(params.Document, params.ChatID, params.FileName),
|
||||
}
|
||||
|
||||
if params.Thumbnail != nil {
|
||||
thumbnail := c.prepareInputFile(params.Thumbnail, params.ChatID, "thumbnail")
|
||||
req.Thumbnail = &thumbnail
|
||||
}
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.DisableContentTypeDetection {
|
||||
req.DisableContentTypeDetection = ¶ms.DisableContentTypeDetection
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) SendVideo(params VideoParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
req := &requests.SendVideo{
|
||||
ChatId: chatID,
|
||||
Video: c.prepareInputFile(params.Video, params.ChatID, "video"),
|
||||
}
|
||||
|
||||
if params.Duration != 0 {
|
||||
req.Duration = ¶ms.Duration
|
||||
}
|
||||
if params.Width != 0 {
|
||||
req.Width = ¶ms.Width
|
||||
}
|
||||
if params.Height != 0 {
|
||||
req.Height = ¶ms.Height
|
||||
}
|
||||
if params.Thumbnail != nil {
|
||||
thumbnail := c.prepareInputFile(params.Thumbnail, params.ChatID, "thumbnail")
|
||||
req.Thumbnail = &thumbnail
|
||||
}
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.SupportsStreaming {
|
||||
req.SupportsStreaming = ¶ms.SupportsStreaming
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) SendAudio(params AudioParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
req := &requests.SendAudio{
|
||||
ChatId: chatID,
|
||||
Audio: c.prepareInputFile(params.Audio, params.ChatID, "audio"),
|
||||
}
|
||||
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.Duration != 0 {
|
||||
req.Duration = ¶ms.Duration
|
||||
}
|
||||
if params.Performer != "" {
|
||||
req.Performer = ¶ms.Performer
|
||||
}
|
||||
if params.Title != "" {
|
||||
req.Title = ¶ms.Title
|
||||
}
|
||||
if params.Thumbnail != nil {
|
||||
thumbnail := c.prepareInputFile(params.Thumbnail, params.ChatID, "thumbnail")
|
||||
req.Thumbnail = &thumbnail
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) SendLocation(params LocationParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
req := &requests.SendLocation{
|
||||
ChatId: chatID,
|
||||
Latitude: params.Latitude,
|
||||
Longitude: params.Longitude,
|
||||
}
|
||||
|
||||
if params.HorizontalAccuracy != 0 {
|
||||
req.HorizontalAccuracy = ¶ms.HorizontalAccuracy
|
||||
}
|
||||
if params.LivePeriod != 0 {
|
||||
req.LivePeriod = ¶ms.LivePeriod
|
||||
}
|
||||
if params.Heading != 0 {
|
||||
req.Heading = ¶ms.Heading
|
||||
}
|
||||
if params.ProximityAlertRadius != 0 {
|
||||
req.ProximityAlertRadius = ¶ms.ProximityAlertRadius
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) EditMessageText(params EditMessageTextParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
req := &requests.EditMessageText{
|
||||
Text: params.Text,
|
||||
}
|
||||
|
||||
if params.ChatID != 0 {
|
||||
req.ChatId = &chatID
|
||||
req.MessageId = ¶ms.MessageID
|
||||
}
|
||||
if params.InlineMessageID != "" {
|
||||
req.InlineMessageId = ¶ms.InlineMessageID
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.Entities) > 0 {
|
||||
req.Entities = params.Entities
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if msg, ok := result.(*tg.Message); ok {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) EditMessageCaption(params EditMessageCaptionParams) (*tg.Message, error) {
|
||||
req := &requests.EditMessageCaption{}
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
if params.ChatID != 0 {
|
||||
req.ChatId = &chatID
|
||||
req.MessageId = ¶ms.MessageID
|
||||
}
|
||||
if params.InlineMessageID != "" {
|
||||
req.InlineMessageId = ¶ms.InlineMessageID
|
||||
}
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if msg, ok := result.(*tg.Message); ok {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) EditMessageReplyMarkup(params EditMessageReplyMarkupParams) (*tg.Message, error) {
|
||||
req := &requests.EditMessageReplyMarkup{}
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
if params.ChatID != 0 {
|
||||
req.ChatId = &chatID
|
||||
req.MessageId = ¶ms.MessageID
|
||||
}
|
||||
if params.InlineMessageID != "" {
|
||||
req.InlineMessageId = ¶ms.InlineMessageID
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if msg, ok := result.(*tg.Message); ok {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) DeleteMessage(params DeleteMessageParams) (bool, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
|
||||
req := &requests.DeleteMessage{
|
||||
ChatId: chatID,
|
||||
MessageId: params.MessageID,
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result.(bool), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) ForwardMessage(params ForwardMessageParams) (*tg.Message, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
fromChatID := tg.NewChatId(params.FromChatID, "")
|
||||
|
||||
req := &requests.ForwardMessage{
|
||||
ChatId: chatID,
|
||||
FromChatId: fromChatID,
|
||||
MessageId: params.MessageID,
|
||||
}
|
||||
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.Message), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) CopyMessage(params CopyMessageParams) (*tg.MessageId, error) {
|
||||
chatID := tg.NewChatId(params.ChatID, "")
|
||||
fromChatID := tg.NewChatId(params.FromChatID, "")
|
||||
|
||||
req := &requests.CopyMessage{
|
||||
ChatId: chatID,
|
||||
FromChatId: fromChatID,
|
||||
MessageId: params.MessageID,
|
||||
}
|
||||
|
||||
if params.Caption != "" {
|
||||
req.Caption = ¶ms.Caption
|
||||
}
|
||||
if params.ParseMode != "" {
|
||||
req.ParseMode = ¶ms.ParseMode
|
||||
}
|
||||
if len(params.CaptionEntities) > 0 {
|
||||
req.CaptionEntities = params.CaptionEntities
|
||||
}
|
||||
if params.DisableNotification {
|
||||
req.DisableNotification = ¶ms.DisableNotification
|
||||
}
|
||||
if params.ProtectContent {
|
||||
req.ProtectContent = ¶ms.ProtectContent
|
||||
}
|
||||
if params.ReplyMarkup != nil {
|
||||
req.ReplyMarkup = params.ReplyMarkup
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.MessageId), nil
|
||||
}
|
||||
|
||||
// ============ CALLBACK QUERIES ============
|
||||
|
||||
func (c *TgBotClient) AnswerCallbackQuery(callbackQueryID string, text string, showAlert bool) error {
|
||||
req := &requests.AnswerCallbackQuery{
|
||||
CallbackQueryId: callbackQueryID,
|
||||
}
|
||||
|
||||
if text != "" {
|
||||
req.Text = &text
|
||||
}
|
||||
if showAlert {
|
||||
req.ShowAlert = &showAlert
|
||||
}
|
||||
|
||||
_, err := req.Call(c.ctx, c.bot)
|
||||
return err
|
||||
}
|
||||
|
||||
// ============ ФАЙЛЫ ============
|
||||
|
||||
func (c *TgBotClient) GetFile(fileID string) (*tg.File, error) {
|
||||
req := &requests.GetFile{
|
||||
FileId: fileID,
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.(*tg.File), nil
|
||||
}
|
||||
|
||||
func (c *TgBotClient) GetFileURL(fileID string) (string, error) {
|
||||
file, err := c.GetFile(fileID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if file.FilePath == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return "https://api.telegram.org/file/bot" + c.config.BotKey + "/" + *file.FilePath, nil
|
||||
}
|
||||
|
||||
// ============ UPDATES ============
|
||||
|
||||
func (c *TgBotClient) GetUpdates(params GetUpdatesParams) ([]tg.Update, error) {
|
||||
req := &requests.GetUpdates{}
|
||||
|
||||
if params.Offset != 0 {
|
||||
req.Offset = ¶ms.Offset
|
||||
}
|
||||
if params.Limit != 0 {
|
||||
req.Limit = ¶ms.Limit
|
||||
}
|
||||
if params.Timeout != 0 {
|
||||
req.Timeout = ¶ms.Timeout
|
||||
}
|
||||
if len(params.AllowedUpdates) > 0 {
|
||||
req.AllowedUpdates = params.AllowedUpdates
|
||||
}
|
||||
|
||||
result, err := req.Call(c.ctx, c.bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.([]tg.Update), nil
|
||||
}
|
||||
|
||||
// ============ CHAT ACTIONS ============
|
||||
|
||||
func (c *TgBotClient) SendChatAction(chatID int64, action string) error {
|
||||
chatId := tg.NewChatId(chatID, "")
|
||||
|
||||
req := &requests.SendChatAction{
|
||||
ChatId: chatId,
|
||||
Action: action,
|
||||
}
|
||||
|
||||
_, err := req.Call(c.ctx, c.bot)
|
||||
return err
|
||||
}
|
||||
|
|
@ -0,0 +1,201 @@
|
|||
package telegram
|
||||
|
||||
import tg "github.com/temoon/telegram-bots-api"
|
||||
|
||||
type BotConfig struct {
|
||||
BotKey string
|
||||
ApiURL string
|
||||
}
|
||||
|
||||
// ParseMode константы
|
||||
const (
|
||||
ParseModeHTML = tg.ParseModeHTML
|
||||
ParseModeMarkdown = tg.ParseModeMarkdown
|
||||
ParseModeMarkdownV2 = tg.ParseModeMarkdownV2
|
||||
)
|
||||
|
||||
// ChatAction константы
|
||||
const (
|
||||
ChatActionTyping = tg.ChatActionTyping
|
||||
ChatActionUploadPhoto = tg.ChatActionUploadPhoto
|
||||
ChatActionRecordVideo = tg.ChatActionRecordVideo
|
||||
ChatActionUploadVideo = tg.ChatActionUploadVideo
|
||||
ChatActionRecordAudio = tg.ChatActionRecordAudio
|
||||
ChatActionUploadAudio = tg.ChatActionUploadAudio
|
||||
ChatActionUploadDocument = tg.ChatActionUploadDocument
|
||||
ChatActionFindLocation = tg.ChatActionFindLocation
|
||||
ChatActionRecordVideoNote = tg.ChatActionRecordVideoNote
|
||||
ChatActionUploadVideoNote = tg.ChatActionUploadVideoNote
|
||||
)
|
||||
|
||||
// MessageParams параметры для отправки сообщения
|
||||
type MessageParams struct {
|
||||
ChatID int64
|
||||
Text string
|
||||
ParseMode string
|
||||
Entities []tg.MessageEntity
|
||||
DisableWebPagePreview bool
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{} // InlineKeyboardMarkup или ReplyKeyboardMarkup
|
||||
}
|
||||
|
||||
// PhotoParams параметры для отправки фото
|
||||
type PhotoParams struct {
|
||||
ChatID int64
|
||||
Photo interface{} // InputFile, string (file_id), или io.Reader
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
// DocumentParams параметры для отправки документа
|
||||
type DocumentParams struct {
|
||||
ChatID int64
|
||||
Document interface{} // InputFile, string (file_id), или io.Reader
|
||||
Thumbnail interface{}
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
DisableContentTypeDetection bool
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
FileName string
|
||||
}
|
||||
|
||||
// VideoParams параметры для отправки видео
|
||||
type VideoParams struct {
|
||||
ChatID int64
|
||||
Video interface{}
|
||||
Duration int64
|
||||
Width int64
|
||||
Height int64
|
||||
Thumbnail interface{}
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
SupportsStreaming bool
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
// AudioParams параметры для отправки аудио
|
||||
type AudioParams struct {
|
||||
ChatID int64
|
||||
Audio interface{}
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
Duration int64
|
||||
Performer string
|
||||
Title string
|
||||
Thumbnail interface{}
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
// LocationParams параметры для отправки геолокации
|
||||
type LocationParams struct {
|
||||
ChatID int64
|
||||
Latitude float64
|
||||
Longitude float64
|
||||
HorizontalAccuracy float64
|
||||
LivePeriod int64
|
||||
Heading int64
|
||||
ProximityAlertRadius int64
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
// EditMessageTextParams параметры для редактирования текста
|
||||
type EditMessageTextParams struct {
|
||||
ChatID int64
|
||||
MessageID int64
|
||||
InlineMessageID string
|
||||
Text string
|
||||
ParseMode string
|
||||
Entities []tg.MessageEntity
|
||||
DisableWebPagePreview bool
|
||||
ReplyMarkup *tg.InlineKeyboardMarkup
|
||||
}
|
||||
|
||||
// EditMessageCaptionParams параметры для редактирования подписи
|
||||
type EditMessageCaptionParams struct {
|
||||
ChatID int64
|
||||
MessageID int64
|
||||
InlineMessageID string
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
ReplyMarkup *tg.InlineKeyboardMarkup
|
||||
}
|
||||
|
||||
// EditMessageReplyMarkupParams параметры для редактирования клавиатуры
|
||||
type EditMessageReplyMarkupParams struct {
|
||||
ChatID int64
|
||||
MessageID int64
|
||||
InlineMessageID string
|
||||
ReplyMarkup *tg.InlineKeyboardMarkup
|
||||
}
|
||||
|
||||
// DeleteMessageParams параметры для удаления сообщения
|
||||
type DeleteMessageParams struct {
|
||||
ChatID int64
|
||||
MessageID int64
|
||||
}
|
||||
|
||||
// ForwardMessageParams параметры для пересылки сообщения
|
||||
type ForwardMessageParams struct {
|
||||
ChatID int64
|
||||
FromChatID int64
|
||||
MessageID int64
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
}
|
||||
|
||||
// CopyMessageParams параметры для копирования сообщения
|
||||
type CopyMessageParams struct {
|
||||
ChatID int64
|
||||
FromChatID int64
|
||||
MessageID int64
|
||||
Caption string
|
||||
ParseMode string
|
||||
CaptionEntities []tg.MessageEntity
|
||||
DisableNotification bool
|
||||
ProtectContent bool
|
||||
ReplyToMessageID int64
|
||||
AllowSendingWithoutReply bool
|
||||
ReplyMarkup interface{}
|
||||
}
|
||||
|
||||
// GetFileParams параметры для получения файла
|
||||
type GetFileParams struct {
|
||||
FileID string
|
||||
}
|
||||
|
||||
// GetUpdatesParams параметры для получения обновлений
|
||||
type GetUpdatesParams struct {
|
||||
Offset int64
|
||||
Limit int64
|
||||
Timeout int64
|
||||
AllowedUpdates []string
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package telegram
|
||||
|
||||
import (
|
||||
tg "github.com/temoon/telegram-bots-api"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (c *TgBotClient) prepareInputFile(file interface{}, chatID int64, defaultName string) tg.InputFile {
|
||||
switch v := file.(type) {
|
||||
case string:
|
||||
// File ID или URL
|
||||
return tg.NewInputFile(v, nil, "")
|
||||
case io.Reader:
|
||||
// Reader с файлом
|
||||
if defaultName == "" {
|
||||
defaultName = "file"
|
||||
}
|
||||
return tg.NewInputFile("", v, defaultName)
|
||||
case tg.InputFile:
|
||||
return v
|
||||
default:
|
||||
return tg.InputFile{}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue