feat: send images

This commit is contained in:
eneller
2026-05-14 23:42:00 +02:00
parent fa027ea484
commit b1ae8cb397
3 changed files with 38 additions and 1 deletions

1
go.mod
View File

@@ -16,6 +16,7 @@ require (
github.com/beeper/argo-go v1.1.2 // indirect github.com/beeper/argo-go v1.1.2 // indirect
github.com/coder/websocket v1.8.14 // indirect github.com/coder/websocket v1.8.14 // indirect
github.com/elliotchance/orderedmap/v3 v3.1.0 // indirect github.com/elliotchance/orderedmap/v3 v3.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.22 // indirect github.com/mattn/go-isatty v0.0.22 // indirect

2
go.sum
View File

@@ -14,6 +14,8 @@ 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/orderedmap/v3 v3.1.0 h1:j4DJ5ObEmMBt/lcwIecKcoRxIQUEnw0L804lXYDt/pg= github.com/elliotchance/orderedmap/v3 v3.1.0 h1:j4DJ5ObEmMBt/lcwIecKcoRxIQUEnw0L804lXYDt/pg=
github.com/elliotchance/orderedmap/v3 v3.1.0/go.mod h1:G+Hc2RwaZvJMcS4JpGCOyViCnGeKf0bTYCGTO4uhjSo= github.com/elliotchance/orderedmap/v3 v3.1.0/go.mod h1:G+Hc2RwaZvJMcS4JpGCOyViCnGeKf0bTYCGTO4uhjSo=
github.com/gabriel-vasile/mimetype v1.4.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
github.com/gabriel-vasile/mimetype v1.4.13/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=

View File

@@ -155,7 +155,7 @@ func main() {
Name: "poll", Name: "poll",
Usage: "send a poll using <JID> <HEADER> <OPTIONS>", Usage: "send a poll using <JID> <HEADER> <OPTIONS>",
Arguments: []cli.Argument{ Arguments: []cli.Argument{
&cli.StringArg{Name: "jid", Destination: &jidStr}, // use id field of group &cli.StringArg{Name: "jid", Destination: &jidStr},
&cli.StringArg{Name: "header", Destination: &header}, &cli.StringArg{Name: "header", Destination: &header},
&cli.StringArgs{Name: "options", Min: 2, Max: -1}, &cli.StringArgs{Name: "options", Min: 2, Max: -1},
}, },
@@ -166,6 +166,40 @@ func main() {
return nil return nil
}, },
}, },
{
// send an image using https://pkg.go.dev/go.mau.fi/whatsmeow#Client.Upload
Name: "image",
Usage: "send an image using <JID> <PATH> <CAPTION>",
Arguments: []cli.Argument{
&cli.StringArg{Name: "jid", Destination: &jidStr},
&cli.StringArg{Name: "path"},
&cli.StringArg{Name: "caption"},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
data, err := os.ReadFile(cmd.StringArg("path"))
resp, err := client.Upload(ctx, data, whatsmeow.MediaImage)
// figure out the mimetype to specify
// https://stackoverflow.com/questions/51209439/mime-type-checking-of-files-uploaded-golang
if err != nil {
slog.Error("Failed to open file", "error", err)
}
imageMsg := &waE2E.ImageMessage{
Caption: proto.String(cmd.StringArg("caption")),
// TODO replace this with the actual mime type
Mimetype: proto.String("image/jpeg"),
// you can also optionally add other fields like ContextInfo and JpegThumbnail here
URL: &resp.URL,
DirectPath: &resp.DirectPath,
MediaKey: resp.MediaKey,
FileEncSHA256: resp.FileEncSHA256,
FileSHA256: resp.FileSHA256,
FileLength: &resp.FileLength,
}
sendMessage(&waE2E.Message{ImageMessage: imageMsg}, jidStr, client)
return nil
},
},
}, },
} }
// before // before