refactor, cli
This commit is contained in:
2
Makefile
2
Makefile
@@ -6,7 +6,7 @@ build:
|
|||||||
$(CC)
|
$(CC)
|
||||||
|
|
||||||
arm:
|
arm:
|
||||||
env GOOS=linux GOARCH=arm64 $(CC) -o $(NAME)-arm64
|
env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 $(CC) -o $(NAME)-arm64
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
git clean -fX
|
git clean -fX
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
# Whatsup-Poll
|
# Whatsup-Poll
|
||||||
Scheduled Whatsapp Polls using [whatsmeow](https://github.com/tulir/whatsmeow).
|
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).
|
||||||
Alternatively, [neonize](https://github.com/krypton-byte/neonize) python bindings.
|
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -16,6 +16,7 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/petermattis/goid v0.0.0-20250319124200-ccd6737f222a // indirect
|
github.com/petermattis/goid v0.0.0-20250319124200-ccd6737f222a // indirect
|
||||||
github.com/rs/zerolog v1.34.0 // 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/libsignal v0.1.2 // indirect
|
||||||
go.mau.fi/util v0.8.6 // indirect
|
go.mau.fi/util v0.8.6 // indirect
|
||||||
golang.org/x/crypto v0.37.0 // indirect
|
golang.org/x/crypto v0.37.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
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 h1:Vs16DXWxSKyzVtI+EEXLCSy5pVWzzCzp/2eqFGvLyP0=
|
||||||
go.mau.fi/libsignal v0.1.2/go.mod h1:JpnLSSJptn/s1sv7I56uEMywvz8x4YzxeF5OzdPb6PE=
|
go.mau.fi/libsignal v0.1.2/go.mod h1:JpnLSSJptn/s1sv7I56uEMywvz8x4YzxeF5OzdPb6PE=
|
||||||
go.mau.fi/util v0.8.6 h1:AEK13rfgtiZJL2YsNK+W4ihhYCuukcRom8WPP/w/L54=
|
go.mau.fi/util v0.8.6 h1:AEK13rfgtiZJL2YsNK+W4ihhYCuukcRom8WPP/w/L54=
|
||||||
|
|||||||
81
main.go
81
main.go
@@ -4,12 +4,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/mdp/qrterminal/v3"
|
"github.com/mdp/qrterminal/v3"
|
||||||
|
"github.com/urfave/cli/v3"
|
||||||
"go.mau.fi/whatsmeow"
|
"go.mau.fi/whatsmeow"
|
||||||
"go.mau.fi/whatsmeow/store/sqlstore"
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
||||||
"go.mau.fi/whatsmeow/types"
|
"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)
|
dbLog := waLog.Stdout("Database", "DEBUG", true)
|
||||||
// Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite
|
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
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)
|
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 {
|
if err != nil {
|
||||||
fmt.Println("Sent Poll Succuessfully ", strJID)
|
fmt.Println("Sent Poll Succuessfully ", JID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Failed to Send Poll", gJID)
|
fmt.Println("Failed to Send Poll", JID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,3 +94,29 @@ func main() {
|
|||||||
client.Disconnect()
|
client.Disconnect()
|
||||||
container.Close()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user