From c5fc67e951f1b506d17e30c76fa6783c245716e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20H=C3=BCrzeler?= Date: Tue, 2 Jun 2020 17:49:53 +0200 Subject: [PATCH] Add new command for sending test messages --- Readme.md | 12 +++++++++++ cmd/root.go | 12 +++++++++++ cmd/testmessage.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 cmd/testmessage.go diff --git a/Readme.md b/Readme.md index c1f81c8..f2ef27b 100644 --- a/Readme.md +++ b/Readme.md @@ -62,3 +62,15 @@ For example if you want to place the config in /etc/ /path/to/telegram-server-update-bot update-check --config /etc/telegram-server-update-bot.yaml ``` +Additional Commands +------------------- +### testmessage +With this comand you could test if you would recieve an message to your telegram client. You could also send an custom Message. + +Following command will send the message "This is my custom Message" to the Bot. If you do not add any custom message, it will send an default one. +``` + /path/to/telegram-server-update-bot update-check testmessage This is my custom Message +``` + +### help +The command and every sub command has an help option. Just add option `-h` or `--help` \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index ecb9daa..5348b52 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -29,6 +30,16 @@ var cmdUpdateCheck = &cobra.Command{ }, } +var cmdTestMessage = &cobra.Command{ + Use: "testmessage", + Short: "Sends an message to the telegram client", + Long: `Use this command to test if you would recieve a message from your server. Optional you could pass an custom message after the command.`, + Run: func(cmd *cobra.Command, + args []string) { + testmessage(strings.Join(args, " ")) + }, +} + var cmdBotSetup = &cobra.Command{ Use: "botsetup", Short: "Telegram Bot setup", @@ -62,6 +73,7 @@ func Exec() { rootCmd.AddCommand(cmdUpdateCheck) rootCmd.AddCommand(cmdBotSetup) rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(cmdTestMessage) err := rootCmd.Execute() if err != nil { diff --git a/cmd/testmessage.go b/cmd/testmessage.go new file mode 100644 index 0000000..08f61c0 --- /dev/null +++ b/cmd/testmessage.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "fmt" + "os" + "strconv" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/spf13/viper" +) + +func testmessage(testmsg string) { + botAPIToken := "" + if viper.GetString("BotAPIToken") != "" { + botAPIToken = viper.GetString("BotAPIToken") + } else { + fmt.Println("'BotAPIToken' is not configured. Maybe configfile is missing.") + os.Exit(1) + } + + var chatUserID int64 + if viper.GetString("ChatUserID") != "" { + chatUserID, _ = strconv.ParseInt(viper.GetString("ChatUserID"), 10, 64) + } else { + fmt.Println("'ChatUserID' is not configured. Maybe configfile is missing.") + os.Exit(1) + } + + serverName := "Server" + if viper.GetString("ServerName") != "" { + serverName = viper.GetString("ServerName") + } + + if testmsg == "" { + testmsg = "Hello from the telegram-server-update-bot!" + } + + bot, err := tgbotapi.NewBotAPI(botAPIToken) + if err != nil { + fmt.Println("Connection to Telegram API failed:") + fmt.Println(err) + os.Exit(1) + } + + message := "" + serverName + "\n" + testmsg + msg := tgbotapi.NewMessage(chatUserID, message) + msg.ParseMode = "HTML" + + if _, err := bot.Send(msg); err != nil { + fmt.Println("Could not send message.") + fmt.Println(err) + os.Exit(1) + } +}