From 9fa2c02c9db5b0f7a587112f01f69cf3ee5fa96b Mon Sep 17 00:00:00 2001 From: kalipso Date: Thu, 9 Nov 2023 14:15:31 +0100 Subject: [PATCH] [triggers/receive_telegram] init --- triggers/receive_telegram.go | 136 +++++++++++++++++++++++++++++++++++ triggers/triggers.go | 1 + 2 files changed, 137 insertions(+) create mode 100644 triggers/receive_telegram.go diff --git a/triggers/receive_telegram.go b/triggers/receive_telegram.go new file mode 100644 index 0000000..12f4bca --- /dev/null +++ b/triggers/receive_telegram.go @@ -0,0 +1,136 @@ +package triggers + +import ( + "fmt" + "encoding/json" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + + "unknown.com/gokill/internal" + "unknown.com/gokill/actions" +) + +type ReceiveTelegram struct { + Token string `json:"token"` + ChatId int64 `json:"chatId"` + Message string `json:"message"` + action actions.Action +} + +func (s ReceiveTelegram) Listen() { + bot, err := tgbotapi.NewBotAPI(s.Token) + + if err != nil { + return //fmt.Errorf("ReceiveTelegram waitForMessage error: %s", err) + } + + bot.Debug = false + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + msg := tgbotapi.NewMessage(-746157642, "BOT TEST MESSAGE") + bot.Send(msg) + + chatId := s.ChatId + updates := bot.GetUpdatesChan(u) + for update := range updates { + if update.Message != nil { // If we got a message + if(update.Message.Chat.ID != chatId) { + internal.LogDoc(s).Debugf("ReceiveTelegram received wrong ChatId. Got %s, wanted %s", update.Message.Chat.ID, s.ChatId) + continue + } + + if(update.Message.Text != s.Message) { + internal.LogDoc(s).Debug("ReceiveTelegram received wrong Message") + continue + } + + internal.LogDoc(s).Info("ReceiveTelegram received secret message") + actions.Fire(s.action) + } + } +} + + + +func CreateReceiveTelegram(config internal.KillSwitchConfig) (ReceiveTelegram, error) { + result := ReceiveTelegram{ + ChatId: 0, + } + + err := json.Unmarshal(config.Options, &result) + + if err != nil { + return ReceiveTelegram{}, fmt.Errorf("Error during CreateReceiveTelegram: %s", err) + } + + if result.Token == "" { + return ReceiveTelegram{}, internal.OptionMissingError{"token"} + } + + if result.ChatId == 0 { + return ReceiveTelegram{}, internal.OptionMissingError{"chadId"} + } + + if result.Message == "" { + return ReceiveTelegram{}, internal.OptionMissingError{"message"} + } + + action, err := actions.NewAction(config.Actions) + + if err != nil { + return ReceiveTelegram{}, fmt.Errorf("Error during CreateReceiveTelegram: %s", err) + } + + result.action = action + + return result, nil +} + +func (e ReceiveTelegram) Create(config internal.KillSwitchConfig) (Trigger, error) { + return CreateReceiveTelegram(config) +} + +func (p ReceiveTelegram) GetName() string { + return "ReceiveTelegram" +} + +func (p ReceiveTelegram) GetDescription() string { + return "Waits for a specific message in a given chat. Once the message is received, the trigger fires." +} + +func (p ReceiveTelegram) GetExample() string { + return ` + { + "type": "ReceiveTelegram", + "options": { + "token": "5349923487:FFGrETxa0pA29d02Akslw-lkwjdA92KAH2", + "chatId": -832345892, + "message": "secretmessagethatfiresthetrigger" + } + } + ` +} + +func (p ReceiveTelegram) 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 received from.", + Default: "", + }, + { + Name: "message", + Type: "string", + Description: "actual message that, when received, fires the trigger", + Default: "", + }, + } +} diff --git a/triggers/triggers.go b/triggers/triggers.go index 9fe7247..9207194 100644 --- a/triggers/triggers.go +++ b/triggers/triggers.go @@ -29,6 +29,7 @@ func NewTrigger(config internal.KillSwitchConfig) (Trigger, error) { func GetAllTriggers() []DocumentedTrigger { return []DocumentedTrigger{ EthernetDisconnect{}, + ReceiveTelegram{}, TimeOut{}, UsbDisconnect{}, }