From b1ae8cb39730ecf4d850c0d1bbed96482a5f0811 Mon Sep 17 00:00:00 2001 From: eneller Date: Thu, 14 May 2026 23:42:00 +0200 Subject: [PATCH] feat: send images --- go.mod | 1 + go.sum | 2 ++ src/main.go | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 20d5061..3e6b0ab 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/beeper/argo-go v1.1.2 // indirect github.com/coder/websocket v1.8.14 // 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/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.22 // indirect diff --git a/go.sum b/go.sum index ad00423..47cbb0e 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= diff --git a/src/main.go b/src/main.go index 8134f7c..8c1f1c6 100644 --- a/src/main.go +++ b/src/main.go @@ -155,7 +155,7 @@ func main() { Name: "poll", Usage: "send a poll using
", 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.StringArgs{Name: "options", Min: 2, Max: -1}, }, @@ -166,6 +166,40 @@ func main() { return nil }, }, + { + // send an image using https://pkg.go.dev/go.mau.fi/whatsmeow#Client.Upload + Name: "image", + Usage: "send an image using ", + 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