X-Git-Url: https://git.jsancho.org/?p=midgaard_bot.git;a=blobdiff_plain;f=midgaard_bot.go;fp=midgaard_bot.go;h=a311c97e54c31553ae52b0e910de83b9e2a14f72;hp=d4c3410d81e4b188c49bd3bd078dc07c9fbda800;hb=54d3318115d6fd32364f5993ff67d0461c162aef;hpb=287bb6b379833a10a1eca80a5fbe52422e345c76 diff --git a/midgaard_bot.go b/midgaard_bot.go index d4c3410..a311c97 100644 --- a/midgaard_bot.go +++ b/midgaard_bot.go @@ -4,7 +4,6 @@ import ( "context" "log" - "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jessevdk/go-flags" ) @@ -12,55 +11,29 @@ var config struct { Token string `short:"t" long:"token" description:"Telegram API Token" required:"true"` } -func recv_task(bot *tgbotapi.BotAPI, sendChannel chan tgbotapi.Chattable, ctx context.Context) { - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 - - updates, _ := bot.GetUpdatesChan(u) - - for { - select { - case update := <-updates: - log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) - msg.ReplyToMessageID = update.Message.MessageID - sendChannel <- msg - case <-ctx.Done(): - return - } - } -} - -func send_task(bot *tgbotapi.BotAPI, sendChannel chan tgbotapi.Chattable, ctx context.Context) { - for { - select { - case msg := <-sendChannel: - bot.Send(msg) - case <-ctx.Done(): - return - } - } -} - func main() { _, err := flags.Parse(&config) if err != nil { log.Panic(err) } - bot, err := tgbotapi.NewBotAPI(config.Token) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + err = initSessions(ctx) if err != nil { log.Panic(err) } - bot.Debug = true - - log.Printf("Authorized on account %s", bot.Self.UserName) + err = initTelegramWorkers(config.Token, ctx) + if err != nil { + log.Panic(err) + } - ctx, cancel := context.WithCancel(context.Background()) - sendChannel := make(chan tgbotapi.Chattable) - go send_task(bot, sendChannel, ctx) - recv_task(bot, sendChannel, ctx) - cancel() + for { + select { + case <-ctx.Done(): + break + } + } }