From fb7349b69d77557aceb189efbc26497d7470d641 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Mon, 13 Jan 2025 21:10:50 -0800 Subject: [PATCH] add unlock command --- Dockerfile | 2 +- main.go | 7 +++++-- ntfy/assistant/assistant.go | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ntfy/assistant/assistant.go diff --git a/Dockerfile b/Dockerfile index f82bdb6..696d2fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ RUN go build -o /app/phoneassistant EXPOSE 8080 -CMD ["/app/phoneassistant", "--server", "--migrate", "--port", "8080", "--template-path", "/app/templates", "--database-path", "/app/db/phoneassistant.db", "--static-path", "/app/static", "--scheduler", "--ntfy-topics", "whois", "--ntfy-endpoint", "https://ntfy.simponic.hatecomputers.club"] +CMD ["/app/phoneassistant", "--server", "--migrate", "--port", "8080", "--template-path", "/app/templates", "--database-path", "/app/db/phoneassistant.db", "--static-path", "/app/static", "--scheduler", "--ntfy-topics", "passt", "--ntfy-endpoint", "https://ntfy.simponic.hatecomputers.club"] diff --git a/main.go b/main.go index b721f90..b7aea35 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "git.simponic.xyz/simponic/phoneassistant/args" "git.simponic.xyz/simponic/phoneassistant/database" "git.simponic.xyz/simponic/phoneassistant/ntfy" + "git.simponic.xyz/simponic/phoneassistant/ntfy/assistant" "git.simponic.xyz/simponic/phoneassistant/scheduler" "github.com/joho/godotenv" ) @@ -43,8 +44,10 @@ func main() { go func() { for notification := range notifications { - message := notification.Message - log.Println("got message", message) + err := assistant.Do(notification.Message) + if err != nil { + log.Println("error when sending command: ", err) + } } }() } diff --git a/ntfy/assistant/assistant.go b/ntfy/assistant/assistant.go new file mode 100644 index 0000000..3f15647 --- /dev/null +++ b/ntfy/assistant/assistant.go @@ -0,0 +1,23 @@ +package assistant + +import ( + "fmt" + "net/http" + "os" +) + +func UnlockDoor(webhook string) error { + req, _ := http.NewRequest("GET", webhook) + res, err := http.DefaultClient.Do(req) + if err != nil || res.StatusCode/100 != 2 { + return fmt.Errorf("got err sending message send req %s %v %s", message, res, err) + } + return nil +} + +func Do(command string) error { + if command == "unlock" { + return UnlockDoor(os.Getenv("UNLOCK_DOOR_WEBHOOK")) + } + return fmt.Errorf("unknown command %s", command) +}