diff --git a/actions/actions.go b/actions/actions.go index 616d4fd..15af3e2 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -124,6 +124,7 @@ func GetAllActions() []DocumentedAction { ShellScript{}, Shutdown{}, SendMatrix{}, + SendTelegram{}, TimeOut{}, } } diff --git a/actions/send_telegram.go b/actions/send_telegram.go new file mode 100644 index 0000000..0b88f8f --- /dev/null +++ b/actions/send_telegram.go @@ -0,0 +1,143 @@ +package actions + +import ( + "fmt" + "encoding/json" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + + "unknown.com/gokill/internal" +) + +type SendTelegram struct { + Token string `json:"token"` + ChatId string `json:"chatId"` + Message string `json:"message"` + TestMessage string `json:"testMessage"` + ActionChan ActionResultChan +} + +func (s SendTelegram) sendMessage(message string) error { + bot, err := tgbotapi.NewBotAPI("5221828879:AAGrETcxOpAhzPJUl-fUqMJGUAe6ShSuuRs") + if err != nil { + return fmt.Errorf("SendTelegram sendMessage error: %s", err) + } + + bot.Debug = true + + fmt.Printf("Authorized on account %s", bot.Self.UserName) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + msg := tgbotapi.NewMessage(-746157642, message) + bot.Send(msg) + + return nil +} + +func (s SendTelegram) DryExecute() { + fmt.Println("SendTelegram: Trying to send test message") + err := s.sendMessage(s.TestMessage) + + if err != nil { + fmt.Println("SendTelegram: failed to send test message") + } + + s.ActionChan <- err +} + +func (s SendTelegram) Execute() { + fmt.Println("SendTelegram: Trying to send message") + err := s.sendMessage(s.Message) + + if err != nil { + fmt.Println("SendTelegram: failed to send message") + } + + s.ActionChan <- err +} + +func CreateSendTelegram(config internal.ActionConfig, c ActionResultChan) (SendTelegram, error) { + result := SendTelegram{} + + err := json.Unmarshal(config.Options, &result) + + if err != nil { + return SendTelegram{}, err + } + + if result.Token == "" { + return SendTelegram{}, internal.OptionMissingError{"token"} + } + + if result.ChatId == "" { + return SendTelegram{}, internal.OptionMissingError{"chatId"} + } + + if result.Message == "" { + return SendTelegram{}, internal.OptionMissingError{"message"} + } + + if result.TestMessage == "" { + return SendTelegram{}, internal.OptionMissingError{"testMessage"} + } + + result.ActionChan = c + return result, nil +} + +func (s SendTelegram) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) { + return CreateSendTelegram(config, c) +} + +func (p SendTelegram) GetName() string { + return "SendTelegram" +} + +func (p SendTelegram) GetDescription() string { + return "Sends a message to a given room. The user needs to be part of that room already." +} + +func (p SendTelegram) GetExample() string { + return ` + { + "type": "SendTelegram", + "options": { + "token": "5349923487:FFGrETxa0pA29d02Akslw-lkwjdA92KAH2", + "chatId": "-832345892", + "message": "attention, intruders got my device!", + "testMessage": "this is just a test, no worries" + } + } + ` +} + +func (p SendTelegram) GetOptions() []internal.ConfigOption { + return []internal.ConfigOption{ + { + Name: "token", + Type: "string", + Description: "telegram bot token (ask botfather)", + Default: "", + }, + { + Name: "chatId", + Type: "int", + Description: "chatId of group or chat you want the message be sent to.", + Default: "", + }, + { + Name: "message", + Type: "string", + Description: "actual message that should be sent", + Default: "", + }, + { + Name: "testMessage", + Type: "string", + Description: "message sent during test run", + Default: "", + }, + } +} diff --git a/go.mod b/go.mod index ff69295..5c3e620 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module unknown.com/gokill go 1.21.3 require ( + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/mattn/go-sqlite3 v1.14.17 maunium.net/go/mautrix v0.16.1 ) diff --git a/go.sum b/go.sum index 4816867..91de7a2 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=