Skip to content

Commit 6dd3d24

Browse files
committed
download: add method for fetching sticker packs
1 parent 51dcc5e commit 6dd3d24

2 files changed

Lines changed: 82 additions & 8 deletions

File tree

download.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"crypto/hmac"
1212
"crypto/sha256"
1313
"encoding/base64"
14+
"encoding/json"
1415
"errors"
1516
"fmt"
1617
"io"
@@ -28,6 +29,7 @@ import (
2829
"go.mau.fi/whatsmeow/proto/waMediaTransport"
2930
"go.mau.fi/whatsmeow/proto/waServerSync"
3031
"go.mau.fi/whatsmeow/socket"
32+
"go.mau.fi/whatsmeow/types"
3133
"go.mau.fi/whatsmeow/util/cbcutil"
3234
"go.mau.fi/whatsmeow/util/hkdfutil"
3335
)
@@ -87,6 +89,7 @@ var (
8789
_ DownloadableMessage = (*waE2E.HistorySyncNotification)(nil)
8890
_ DownloadableMessage = (*waServerSync.ExternalBlobReference)(nil)
8991
_ DownloadableThumbnail = (*waE2E.ExtendedTextMessage)(nil)
92+
_ DownloadableMessage = (*types.StickerPackItem)(nil)
9093
)
9194

9295
type downloadableMessageWithLength interface {
@@ -191,15 +194,33 @@ func (cli *Client) DownloadThumbnail(ctx context.Context, msg DownloadableThumbn
191194

192195
// GetMediaType returns the MediaType value corresponding to the given protobuf message.
193196
func GetMediaType(msg DownloadableMessage) MediaType {
194-
protoReflecter, ok := msg.(proto.Message)
195-
if !ok {
196-
mediaTypeable, ok := msg.(MediaTypeable)
197-
if !ok {
198-
return ""
199-
}
200-
return mediaTypeable.GetMediaType()
197+
switch typedMsg := msg.(type) {
198+
case *types.StickerPackItem:
199+
return MediaImage
200+
case proto.Message:
201+
return classToMediaType[typedMsg.ProtoReflect().Descriptor().Name()]
202+
case MediaTypeable:
203+
return typedMsg.GetMediaType()
204+
default:
205+
return ""
206+
}
207+
}
208+
209+
func (cli *Client) FetchStickerPack(ctx context.Context, packID string) (*types.StickerPack, error) {
210+
url := fmt.Sprintf("https://static.whatsapp.net/sticker?lottie=1&cat=sticker_pack_data&id=%s&lg=en", packID)
211+
resp, err := cli.doMediaDownloadRequest(ctx, url)
212+
if err != nil {
213+
return nil, err
214+
}
215+
var packs []types.StickerPack
216+
err = json.NewDecoder(resp.Body).Decode(&packs)
217+
_ = resp.Body.Close()
218+
if err != nil {
219+
return nil, fmt.Errorf("failed to decode response: %w", err)
220+
} else if len(packs) == 0 {
221+
return nil, fmt.Errorf("no sticker pack found in response")
201222
}
202-
return classToMediaType[protoReflecter.ProtoReflect().Descriptor().Name()]
223+
return &packs[0], nil
203224
}
204225

205226
// Download downloads the attachment from the given protobuf message.

types/sticker.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package types
2+
3+
type StickerPack struct {
4+
StickerPackID string `json:"sticker-pack-id"`
5+
Name string `json:"name"`
6+
Publisher string `json:"publisher"`
7+
Description string `json:"description"`
8+
FileSize string `json:"file-size"`
9+
ImageDataHash string `json:"image-data-hash"`
10+
Stickers []StickerPackItem `json:"stickers"`
11+
Animated int `json:"animated"`
12+
Lottie int `json:"lottie"`
13+
PreviewImageIDs []string `json:"preview-image-ids"`
14+
TrayImageID string `json:"tray-image-id"`
15+
TrayImagePreview string `json:"tray-image-preview"`
16+
}
17+
18+
type StickerPackItem struct {
19+
MediaKey []byte `json:"media-key"`
20+
EncFileHash []byte `json:"enc-file-hash"`
21+
FileHash []byte `json:"file-hash"`
22+
DirectPath string `json:"direct-path"`
23+
URL string `json:"url"`
24+
FileSize int64 `json:"file-size"`
25+
MimeType string `json:"mimetype"`
26+
Height int `json:"height"`
27+
Width int `json:"width"`
28+
Emojis []string `json:"emojis"`
29+
AccessibilityText string `json:"accessibility-text"`
30+
Handle string `json:"handle"`
31+
StickerHashWithoutMeta []byte `json:"sticker-hash-without-meta"`
32+
PreviewWebpID string `json:"preview-webp-id"`
33+
}
34+
35+
func (spi *StickerPackItem) GetDirectPath() string {
36+
return spi.DirectPath
37+
}
38+
39+
func (spi *StickerPackItem) GetMediaKey() []byte {
40+
return spi.MediaKey
41+
}
42+
43+
func (spi *StickerPackItem) GetFileSHA256() []byte {
44+
return spi.FileHash
45+
}
46+
47+
func (spi *StickerPackItem) GetFileEncSHA256() []byte {
48+
return spi.EncFileHash
49+
}
50+
51+
func (spi *StickerPackItem) GetFileSizeBytes() int64 {
52+
return spi.FileSize
53+
}

0 commit comments

Comments
 (0)