package tgBot import ( "context" "fmt" "github.com/go-telegram/bot" "github.com/go-telegram/bot/models" log "github.com/sirupsen/logrus" "tg-disc-bot/config" "tg-disc-bot/dto" ) type TgBot struct { ctx context.Context bot *bot.Bot chatID int64 } func NewTgBot(ctx context.Context, config config.TelegramConfig) (*TgBot, error) { tgBot := &TgBot{ctx: ctx, chatID: config.ChatID} var err error tgBot.bot, err = bot.New(config.Token) if err != nil { return nil, err } return tgBot, nil } func (b *TgBot) Start(fromDiscord chan dto.DiscordDTO) chan dto.TelegramDTO { msgChan := make(chan dto.TelegramDTO, 100) b.bot.RegisterHandler( bot.HandlerTypeMessageText, "", bot.MatchTypeContains, func(ctx context.Context, bt *bot.Bot, update *models.Update) { if update.Message != nil || !update.Message.From.IsBot { msg := dto.TelegramDTO{ AuthorName: update.Message.From.Username, Content: update.Message.Text, } fmt.Println(update.Message.Chat.ID) msgChan <- msg } }) go func() { log.Info("Starting telegram bot...") b.bot.Start(b.ctx) }() go func() { for msg := range fromDiscord { log.WithField("content", msg).Debug("TG | Message from Discord") m := fmt.Sprintf("[%s]\n%s", msg.AuthorName, msg.Content) b.bot.SendMessage(b.ctx, &bot.SendMessageParams{ ChatID: b.chatID, Text: m, }) } }() return msgChan }