Compare commits

..

3 Commits

Author SHA1 Message Date
7256763c20 [actions/send_matrix] fix typo 2023-11-02 00:51:15 +01:00
54ddafd0fc [nix] support outputs for different architectures 2023-11-02 00:50:36 +01:00
baeab6daf2 [actions/send_telegram] init 2023-11-02 00:49:06 +01:00
7 changed files with 243 additions and 58 deletions

View File

@@ -124,6 +124,7 @@ func GetAllActions() []DocumentedAction {
ShellScript{},
Shutdown{},
SendMatrix{},
SendTelegram{},
TimeOut{},
}
}

View File

@@ -98,11 +98,11 @@ func (s SendMatrix) DryExecute() {
}
func (s SendMatrix) Execute() {
fmt.Println("SendMatrix: Trying to send test message")
fmt.Println("SendMatrix: Trying to send message")
err := s.sendMessage(s.Message)
if err != nil {
fmt.Println("SendMatrix: failed to send test message")
fmt.Println("SendMatrix: failed to send message")
}
s.ActionChan <- err

143
actions/send_telegram.go Normal file
View File

@@ -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: "",
},
}
}

36
flake.lock generated
View File

@@ -18,7 +18,41 @@
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},

114
flake.nix
View File

@@ -4,13 +4,17 @@
#nixpkgs for testing framework
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs, ... }:
inputs.utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, utils, ... }:
nixpkgs.lib.attrsets.recursiveUpdate
(utils.lib.eachSystem (utils.lib.defaultSystems) ( system:
let
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ];
pkgs = nixpkgs.legacyPackages."x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShell."x86_64-linux" = pkgs.mkShell {
devShells.default = pkgs.mkShell {
packages = with pkgs; [
go
gotools
@@ -19,40 +23,52 @@
];
};
packages.x86_64-linux.gokill = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
pname = "gokill";
version = "1.0";
vendorHash = "sha256-MVIvXxASUO33Ca34ruIz4S0QDJcW2unaG4+Zo73g/9o=";
src = ./.;
packages = {
gokill = pkgs.buildGoModule rec {
pname = "gokill";
version = "1.0";
vendorHash = "sha256-8xHjVwNskgiSoDKbOpL2phHub1F21ios1t9BcZB944o=";
src = ./.;
buildInputs = [
pkgs.olm
];
buildInputs = [
pkgs.olm
];
postInstall = ''
'';
};
gokill-docbuilder = pkgs.buildGoModule rec {
pname = "docbuilder";
version = "1.0";
vendorHash = "sha256-8xHjVwNskgiSoDKbOpL2phHub1F21ios1t9BcZB944o=";
buildFLags = "-o . $dest/cmd/gokill/docbuilder";
src = ./.;
buildInputs = [
pkgs.olm
];
postInstall = ''
'';
};
docs = pkgs.callPackage (import ./docs/default.nix) { self = self; };
default = self.packages.${system}.gokill;
postInstall = ''
'';
};
packages.x86_64-linux.gokill-docbuilder = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
pname = "docbuilder";
version = "1.0";
vendorHash = "sha256-MVIvXxASUO33Ca34ruIz4S0QDJcW2unaG4+Zo73g/9o=";
buildFLags = "-o . $dest/cmd/gokill/docbuilder";
src = ./.;
buildInputs = [
pkgs.olm
];
postInstall = ''
'';
apps = {
docs = {
type = "app";
program = builtins.toString (pkgs.writeScript "docs" ''
${pkgs.python3}/bin/python3 -m http.server --directory ${self.packages."${system}".docs}/share/doc'');
};
};
packages.x86_64-linux.docs = pkgs.callPackage (import ./docs/default.nix) { self = self; };
packages.x86_64-linux.default = self.packages.x86_64-linux.gokill;
})) ({
nixosModules.gokill = import ./nixos-modules/gokill.nix { self = self; };
packages.x86_64-linux.testVm =
@@ -64,6 +80,7 @@
self.nixosModules.gokill
{
services.gokill.enable = true;
services.gokill.testRun = false;
services.gokill.triggers = [
{
type = "Timeout";
@@ -72,19 +89,12 @@
duration = 10;
};
actions = [
{
type = "Timeout";
options = {
duration = 5;
};
stage = 1;
}
{
type = "Shutdown";
options = {
};
stage = 2;
}
{
type = "Shutdown";
options = {
};
stage = 2;
}
];
}
];
@@ -104,19 +114,13 @@
'');
};
apps.x86_64-linux.docs = {
type = "app";
program = builtins.toString (nixpkgs.legacyPackages."x86_64-linux".writeScript "docs" ''
${pkgs.python3}/bin/python3 -m http.server --directory ${self.packages."x86_64-linux".docs}/share/doc'');
};
checks = forAllSystems (system: let
checks.x86_64-linux = let
checkArgs = {
pkgs = nixpkgs.legacyPackages.${system};
pkgs = nixpkgs.legacyPackages."x86_64-linux";
inherit self;
};
in {
gokill = import ./test/test.nix checkArgs;
});
};
};
}) ;
}

1
go.mod
View File

@@ -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
)

2
go.sum
View File

@@ -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=