Skip to content

Commit

Permalink
Merge pull request #14 from joshuabeny1999/test-command
Browse files Browse the repository at this point in the history
Add new command for sending test messages
  • Loading branch information
joshuabeny1999 committed Jun 2, 2020
2 parents c047dea + c5fc67e commit d6b5f3e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -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",
Expand Down Expand Up @@ -62,6 +73,7 @@ func Exec() {
rootCmd.AddCommand(cmdUpdateCheck)
rootCmd.AddCommand(cmdBotSetup)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(cmdTestMessage)

err := rootCmd.Execute()
if err != nil {
Expand Down
54 changes: 54 additions & 0 deletions cmd/testmessage.go
Original file line number Diff line number Diff line change
@@ -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 := "<b>" + serverName + "</b>\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)
}
}

0 comments on commit d6b5f3e

Please sign in to comment.