github.com/aaabigfish/gopkg@v1.1.0/tgbotapi/tgbotapi.go (about) 1 package tgbotapi 2 3 import ( 4 tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" 5 6 "github.com/aaabigfish/gopkg/config" 7 "github.com/aaabigfish/gopkg/log" 8 ) 9 10 type BotApi tgbotapi.BotAPI 11 12 type Bot struct { 13 botapi *tgbotapi.BotAPI 14 chatIds []int64 15 } 16 17 func NewBot(token string, chatIds ...int64) *Bot { 18 bot, err := tgbotapi.NewBotAPI(token) 19 if err != nil { 20 log.Errorf("tgbotapi.NewBotAPI(%s) err(%v)", token, err) 21 return &Bot{} 22 } 23 24 if config.Env == config.EnvDev { 25 bot.Debug = true 26 } 27 28 return &Bot{botapi: bot, chatIds: chatIds} 29 } 30 31 func (b *Bot) GetBotApi() *tgbotapi.BotAPI { 32 return b.botapi 33 } 34 35 func (b *Bot) SendMsg(message string) { 36 if b.botapi == nil { 37 return 38 } 39 for _, chatId := range b.chatIds { 40 msg := tgbotapi.NewMessage(chatId, message) 41 42 if m, err := b.botapi.Send(msg); err != nil { 43 log.Errorf("tgbotapi SendMsg(%s), msg(%+v) err(%v)", message, m, err) 44 } 45 } 46 47 return 48 } 49 50 func (b *Bot) SendMarkdownMsg(message string) { 51 if b.botapi == nil { 52 return 53 } 54 for _, chatId := range b.chatIds { 55 msg := tgbotapi.NewMessage(chatId, message) 56 msg.ParseMode = tgbotapi.ModeMarkdown 57 58 if m, err := b.botapi.Send(msg); err != nil { 59 log.Errorf("tgbotapi SendMsg(%s), msg(%+v) err(%v)", message, m, err) 60 } 61 } 62 63 return 64 } 65 66 func (b *Bot) Send(chatId int64, message string) error { 67 if b.botapi == nil { 68 return nil 69 } 70 msg := tgbotapi.NewMessage(chatId, message) 71 72 _, err := b.botapi.Send(msg) 73 return err 74 } 75 76 func (b *Bot) SendChannelMsg(chanName string, message string) error { 77 if b.botapi == nil { 78 return nil 79 } 80 msg := tgbotapi.NewMessageToChannel(chanName, message) 81 82 _, err := b.botapi.Send(msg) 83 return err 84 }