From 32d5a2285d62849486d544425ee445fdb0138b8c Mon Sep 17 00:00:00 2001 From: Daniele Tricoli Date: Mon, 28 Feb 2022 23:22:49 +0100 Subject: [PATCH] Move the main code in a run sub-command --- cmd/root.go | 23 ++++++++ cmd/run.go | 93 +++++++++++++++++++++++++++++++++ main_test.go => cmd/run_test.go | 2 +- main.go | 41 +-------------- 4 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 cmd/root.go create mode 100644 cmd/run.go rename main_test.go => cmd/run_test.go (97%) diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..748a5c3 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "tg2mastodon", + Short: "Telegram bot to post messages from a Telegram group to Mastodon", +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { +} diff --git a/cmd/run.go b/cmd/run.go new file mode 100644 index 0000000..7e5c427 --- /dev/null +++ b/cmd/run.go @@ -0,0 +1,93 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "context" + "log" + "os" + "strconv" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/mattn/go-mastodon" + "github.com/spf13/cobra" +) + +const ( + DEBUG = "DEBUG" + TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" + MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS" + MASTODON_CLIENT_ID = "MASTODON_CLIENT_ID" + MASTODON_SECRET = "MASTODON_SECRET" + MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN" + MASTODON_REDIRECT_URI = "MASTODON_REDIRECT_URI" +) + +// runCmd represents the run command +var runCmd = &cobra.Command{ + Use: "run", + Short: "Run the bot", + Long: `Start the bot making it connect bot to Telegram and to Mastodon. + +Every messages posted in the Telegram groups the bot is in will be posted into +the specified Mastodon account.`, + Run: func(cmd *cobra.Command, args []string) { + mastodon_instance := os.Getenv(MASTODON_SERVER_ADDRESS) + c := mastodon.NewClient(&mastodon.Config{ + Server: mastodon_instance, + ClientID: os.Getenv(MASTODON_CLIENT_ID), + ClientSecret: os.Getenv(MASTODON_SECRET), + }) + log.Println("Crating a new client for mastondon istance:", mastodon_instance) + + err := c.AuthenticateToken(context.Background(), os.Getenv(MASTODON_ACCESS_TOKEN), os.Getenv(MASTODON_REDIRECT_URI)) + if err != nil { + log.Fatal(err) + } + + bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN)) + if err != nil { + log.Panic(err) + } + + bot.Debug = parseBoolOrFalse(os.Getenv(DEBUG)) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 30 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message != nil { + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + log.Println(update.Message) + + status, err := c.PostStatus(context.Background(), &mastodon.Toot{ + Status: update.Message.Text, + Visibility: "unlisted", + }) + + if err != nil { + log.Fatalf("Could not post status: %v", err) + } + + log.Printf("Posted status %s", status.URL) + } + } + }, +} + +func init() { + rootCmd.AddCommand(runCmd) +} + +func parseBoolOrFalse(s string) bool { + r, err := strconv.ParseBool(s) + if err != nil { + return false + } + + return r +} diff --git a/main_test.go b/cmd/run_test.go similarity index 97% rename from main_test.go rename to cmd/run_test.go index 2b1abf9..54788d8 100644 --- a/main_test.go +++ b/cmd/run_test.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "testing" diff --git a/main.go b/main.go index b32aed5..752e854 100644 --- a/main.go +++ b/main.go @@ -1,44 +1,7 @@ package main -import ( - "log" - "os" - "strconv" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" -) - -const ( - DEBUG = "DEBUG" - TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" -) +import "noa.mornie.org/eriol/tg2mastodon/cmd" func main() { - - bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN)) - if err != nil { - log.Panic(err) - } - - bot.Debug = parseBoolOrFalse(os.Getenv(DEBUG)) - - u := tgbotapi.NewUpdate(0) - u.Timeout = 30 - - updates := bot.GetUpdatesChan(u) - - for update := range updates { - if update.Message != nil { - log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) - } - } -} - -func parseBoolOrFalse(s string) bool { - r, err := strconv.ParseBool(s) - if err != nil { - return false - } - - return r + cmd.Execute() }