X-Git-Url: https://git.jsancho.org/?p=midgaard_bot.git;a=blobdiff_plain;f=midgaard_bot.go;h=10aab4b06a134f37768ac8a2df5302416de89b72;hp=d4c3410d81e4b188c49bd3bd078dc07c9fbda800;hb=HEAD;hpb=48a6f7ba6c6435a4e973745fceef07b521de81ff diff --git a/midgaard_bot.go b/midgaard_bot.go index d4c3410..10aab4b 100644 --- a/midgaard_bot.go +++ b/midgaard_bot.go @@ -1,66 +1,67 @@ +/* +midgaard_bot, a Telegram bot which sets a bridge to Midgaard Merc MUD +Copyright (C) 2017 by Javier Sancho Fernandez + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + package main import ( "context" "log" + "os" + "os/signal" - "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jessevdk/go-flags" ) -var config struct { +type TelegramConfig 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 - } - } +type MercConfig struct { + Host string `short:"h" long:"host" description:"Host and port for Merc MUD" required:"true"` } -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 - } - } +var Config struct { + Telegram TelegramConfig `group:"Telegram config"` + Merc MercConfig `group:"Merc MUD config"` } func main() { - _, err := flags.Parse(&config) + _, 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(Config.Merc.Host, ctx) if err != nil { log.Panic(err) } - bot.Debug = true + err = initTelegramWorkers(Config.Telegram.Token, ctx) + if err != nil { + log.Panic(err) + } - log.Printf("Authorized on account %s", bot.Self.UserName) + intChannel := make(chan os.Signal, 1) + signal.Notify(intChannel, os.Interrupt) + <-intChannel - ctx, cancel := context.WithCancel(context.Background()) - sendChannel := make(chan tgbotapi.Chattable) - go send_task(bot, sendChannel, ctx) - recv_task(bot, sendChannel, ctx) - cancel() + log.Print("Exit") }