67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
log "github.com/sirupsen/logrus"
|
|
"telegramRepostBot/config"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
log.SetFormatter(&log.TextFormatter{
|
|
ForceColors: true,
|
|
ForceQuote: true,
|
|
DisableLevelTruncation: true,
|
|
PadLevelText: true,
|
|
FullTimestamp: true,
|
|
TimestampFormat: time.DateTime, // 2006-01-02 15:04:05
|
|
})
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
func main() {
|
|
|
|
// Loads Config
|
|
conf, err := config.LoadConfig()
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
bot, err := tgbotapi.NewBotAPI(conf.ApiToken)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
if conf.LogLevel == "DEBUG" {
|
|
bot.Debug = true
|
|
}
|
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
updates := bot.GetUpdatesChan(u)
|
|
for update := range updates {
|
|
if update.Message != nil { // If we got a message
|
|
log.WithFields(log.Fields{
|
|
"user": update.Message.From.UserName,
|
|
"chatID": update.Message.Chat.ID,
|
|
"message": update.Message.Text,
|
|
}).Info("message send")
|
|
log.WithFields(log.Fields{"id": update.Message.Chat.ID}).Debug("Group/Chat ID")
|
|
|
|
} else if update.ChannelPost != nil {
|
|
log.WithFields(log.Fields{"id": update.ChannelPost.Chat.ID}).Debug("Channel ID")
|
|
log.WithFields(log.Fields{"message_id": update.ChannelPost.MessageID}).Info("Repost")
|
|
|
|
repost := tgbotapi.NewForward(conf.GroupID, conf.ChannelID, update.ChannelPost.MessageID)
|
|
_, err := bot.Send(repost)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
}
|
|
}
|