From e8e4e25f74cbcfba70012b1aefbfb9d4392b38ea Mon Sep 17 00:00:00 2001 From: eneller Date: Sun, 27 Apr 2025 02:52:39 +0200 Subject: [PATCH] refactor, cli --- Makefile | 2 +- README.md | 3 +-- go.mod | 1 + go.sum | 2 ++ main.go | 81 +++++++++++++++++++++++-------------------------------- 5 files changed, 39 insertions(+), 50 deletions(-) diff --git a/Makefile b/Makefile index 0b42c03..9bd877b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ build: $(CC) arm: - env GOOS=linux GOARCH=arm64 $(CC) -o $(NAME)-arm64 + env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 $(CC) -o $(NAME)-arm64 clean: git clean -fX diff --git a/README.md b/README.md index b177286..b56e5c8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,2 @@ # Whatsup-Poll -Scheduled Whatsapp Polls using [whatsmeow](https://github.com/tulir/whatsmeow). -Alternatively, [neonize](https://github.com/krypton-byte/neonize) python bindings. +Scheduled Whatsapp Polls using go [whatsmeow](https://github.com/tulir/whatsmeow), which now also has python bindings with [neonize](https://github.com/krypton-byte/neonize). diff --git a/go.mod b/go.mod index 95215d6..d04978d 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/petermattis/goid v0.0.0-20250319124200-ccd6737f222a // indirect github.com/rs/zerolog v1.34.0 // indirect + github.com/urfave/cli/v3 v3.2.0 // indirect go.mau.fi/libsignal v0.1.2 // indirect go.mau.fi/util v0.8.6 // indirect golang.org/x/crypto v0.37.0 // indirect diff --git a/go.sum b/go.sum index 49add91..142ca4f 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/urfave/cli/v3 v3.2.0 h1:m8WIXY0U9LCuUl5r+0fqLWDhNYWt6qvlW+GcF4EoXf8= +github.com/urfave/cli/v3 v3.2.0/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo= go.mau.fi/libsignal v0.1.2 h1:Vs16DXWxSKyzVtI+EEXLCSy5pVWzzCzp/2eqFGvLyP0= go.mau.fi/libsignal v0.1.2/go.mod h1:JpnLSSJptn/s1sv7I56uEMywvz8x4YzxeF5OzdPb6PE= go.mau.fi/util v0.8.6 h1:AEK13rfgtiZJL2YsNK+W4ihhYCuukcRom8WPP/w/L54= diff --git a/main.go b/main.go index 39c20ad..cc04703 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,13 @@ package main import ( "context" "fmt" + "log" "os" - "strconv" "time" _ "github.com/mattn/go-sqlite3" "github.com/mdp/qrterminal/v3" + "github.com/urfave/cli/v3" "go.mau.fi/whatsmeow" "go.mau.fi/whatsmeow/store/sqlstore" "go.mau.fi/whatsmeow/types" @@ -26,10 +27,10 @@ func eventHandler(evt interface{}) { } } -func main() { +func sendPoll(JID types.JID, headline string, optionNames []string) { dbLog := waLog.Stdout("Database", "DEBUG", true) // Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite - container, err := sqlstore.New("sqlite3", "file:examplestore.db?_foreign_keys=on", dbLog) + container, err := sqlstore.New("sqlite3", "file:sqlite3.db?_foreign_keys=on", dbLog) if err != nil { panic(err) } @@ -72,55 +73,15 @@ func main() { } } - strJID := "XXXXXXXX@g.us" - var gJID types.JID - gJID, err = types.ParseJID(strJID) - if err != nil { - fmt.Println("Failed to parse JID: ", strJID) - } else { - fmt.Println("Parsed JID correctly", gJID) - - } - - var optionNames []string - optionNames = append(optionNames, "O1") - optionNames = append(optionNames, "O2") - - currentTime := time.Now() - var headline string - - var daysShift int = -1 - - if len(os.Args) > 1 { - daysShift, err = strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Println("Failed to parse days shift param: ", os.Args[1]) - os.Exit(-1) - } - - fmt.Println("Parsed days shift param: ", daysShift) - currentTime = currentTime.AddDate(0, 0, daysShift) - - } else { - // If after 8 AM, simply we are generating for the next day - if currentTime.Hour() >= 8 { - - currentTime = currentTime.AddDate(0, 0, 1) - } - } - - headline = "Auto-generated: " + fmt.Sprintf("%d/%d/%d", currentTime.Day(), currentTime.Month(), currentTime.Year()) - - fmt.Println(headline) pollMessage := client.BuildPollCreation(headline, optionNames, 1) - fmt.Println("Create Poll Message succuessfully : ", pollMessage) + fmt.Println("Created Poll Message succuessfully : ", pollMessage) - _, err = client.SendMessage(context.Background(), gJID, pollMessage) + _, err = client.SendMessage(context.Background(), JID, pollMessage) if err != nil { - fmt.Println("Sent Poll Succuessfully ", strJID) + fmt.Println("Sent Poll Succuessfully ", JID) } else { - fmt.Println("Failed to Send Poll", gJID) + fmt.Println("Failed to Send Poll", JID) } @@ -133,3 +94,29 @@ func main() { client.Disconnect() container.Close() } + +func main() { + cmd := &cli.Command{ + Action: func(ctx context.Context, cmd *cli.Command) error { + // parse JID + strJID := "XXXXXXXX@g.us" + var JID types.JID + JID, err := types.ParseJID(strJID) + if err != nil { + fmt.Println("Failed to parse JID: ", strJID) + } else { + fmt.Println("Parsed JID correctly", JID) + + } + var optionNames []string + optionNames = append(optionNames, "O1") + optionNames = append(optionNames, "O2") + headline := "Test" + sendPoll(JID, headline, optionNames) + return nil + }, + } + if err := cmd.Run(context.Background(), os.Args); err != nil { + log.Fatal(err) + } +}