Browse Source

update improve whatsapp send link function

pull/48/head v1.5.2
Dimas Restu H 3 years ago
parent
commit
04ec4098e7
  1. 2
      go.mod
  2. 7
      go.sum
  3. 52
      pkg/whatsapp/whatsapp.go

2
go.mod

@ -3,6 +3,7 @@ module github.com/dimaskiddo/go-whatsapp-multidevice-rest
go 1.19
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/SporkHubr/echo-http-cache v0.0.0-20200706100054-1d7ae9f38029
github.com/go-playground/validator/v10 v10.14.1
github.com/golang-jwt/jwt v3.2.2+incompatible
@ -26,6 +27,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/frankban/quicktest v1.14.5 // indirect

7
go.sum

@ -6,6 +6,8 @@ github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7/go.mod h1:Q5Db
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
@ -14,6 +16,8 @@ github.com/SporkHubr/echo-http-cache v0.0.0-20200706100054-1d7ae9f38029 h1:XjAr0
github.com/SporkHubr/echo-http-cache v0.0.0-20200706100054-1d7ae9f38029/go.mod h1:/JdnJQCTSrjBrEmR8UtcAwsCcFMAeq3c25NK9m21YOA=
github.com/VictoriaMetrics/fastcache v1.5.7/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/benbjohnson/clock v1.0.0/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@ -281,8 +285,10 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@ -310,6 +316,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

52
pkg/whatsapp/whatsapp.go

@ -6,8 +6,10 @@ import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
webp "github.com/nickalie/go-webpbin"
"github.com/sunshineplan/imgconv"
@ -537,7 +539,7 @@ func WhatsAppSendImage(ctx context.Context, jid string, rjid string, imageBytes
imgConvEncode := new(bytes.Buffer)
err = imgconv.Write(imgConvEncode, imgConvDecode, imgconv.FormatOption{Format: imgconv.PNG})
err = imgconv.Write(imgConvEncode, imgConvDecode, &imgconv.FormatOption{Format: imgconv.PNG})
if err != nil {
return "", errors.New("Error While Encoding Convert Image Stream")
}
@ -562,8 +564,8 @@ func WhatsAppSendImage(ctx context.Context, jid string, rjid string, imageBytes
imgResizeEncode := new(bytes.Buffer)
err = imgconv.Write(imgResizeEncode,
imgconv.Resize(imgResizeDecode, imgconv.ResizeOption{Width: 1024}),
imgconv.FormatOption{})
imgconv.Resize(imgResizeDecode, &imgconv.ResizeOption{Width: 1024}),
&imgconv.FormatOption{})
if err != nil {
return "", errors.New("Error While Encoding Resize Image Stream")
@ -582,8 +584,8 @@ func WhatsAppSendImage(ctx context.Context, jid string, rjid string, imageBytes
imgThumbEncode := new(bytes.Buffer)
err = imgconv.Write(imgThumbEncode,
imgconv.Resize(imgThumbDecode, imgconv.ResizeOption{Width: 72}),
imgconv.FormatOption{Format: imgconv.JPEG})
imgconv.Resize(imgThumbDecode, &imgconv.ResizeOption{Width: 72}),
&imgconv.FormatOption{Format: imgconv.JPEG})
if err != nil {
return "", errors.New("Error While Encoding Thumbnail Image Stream")
@ -797,6 +799,7 @@ func WhatsAppSendContact(ctx context.Context, jid string, rjid string, contactNa
func WhatsAppSendLink(ctx context.Context, jid string, rjid string, linkCaption string, linkURL string) (string, error) {
if WhatsAppClient[jid] != nil {
var err error
var urlTitle, urlDescription string
// Make Sure WhatsApp Client is OK
err = WhatsAppIsClientOK(jid)
@ -814,29 +817,48 @@ func WhatsAppSendLink(ctx context.Context, jid string, rjid string, linkCaption
WhatsAppComposeStatus(jid, remoteJID, true, false)
defer WhatsAppComposeStatus(jid, remoteJID, false, false)
// Get URL Metadata
urlResponse, err := http.Get(linkURL)
if err != nil {
return "", err
}
defer urlResponse.Body.Close()
if urlResponse.StatusCode != 200 {
return "", errors.New("Error While Fetching URL Metadata!")
}
// Query URL Metadata
docData, err := goquery.NewDocumentFromReader(urlResponse.Body)
if err != nil {
return "", err
}
docData.Find("title").Each(func(index int, element *goquery.Selection) {
urlTitle = element.Text()
})
docData.Find("meta[name='description']").Each(func(index int, element *goquery.Selection) {
urlDescription, _ = element.Attr("content")
})
// Compose WhatsApp Proto
msgExtra := whatsmeow.SendRequestExtra{
ID: whatsmeow.GenerateMessageID(),
}
msgCaption := "Open Link"
msgText := linkURL
if len(strings.TrimSpace(linkCaption)) > 0 {
msgCaption = linkCaption
msgText = fmt.Sprintf("%s\n%s", linkCaption, linkURL)
}
msgContent := &waproto.Message{
ExtendedTextMessage: &waproto.ExtendedTextMessage{
Text: proto.String(msgText),
MatchedText: proto.String(msgCaption),
Title: proto.String(urlTitle),
MatchedText: proto.String(linkURL),
CanonicalUrl: proto.String(linkURL),
ContextInfo: &waproto.ContextInfo{
ActionLink: &waproto.ActionLink{
Url: proto.String(linkURL),
ButtonTitle: proto.String(msgCaption),
},
},
Description: proto.String(urlDescription),
},
}
@ -878,7 +900,7 @@ func WhatsAppSendSticker(ctx context.Context, jid string, rjid string, stickerBy
return "", errors.New("Error While Decoding Convert Sticker Stream")
}
stickerConvResize := imgconv.Resize(stickerConvDecode, imgconv.ResizeOption{Width: 512, Height: 512})
stickerConvResize := imgconv.Resize(stickerConvDecode, &imgconv.ResizeOption{Width: 512, Height: 512})
stickerConvEncode := new(bytes.Buffer)
err = webp.Encode(stickerConvEncode, stickerConvResize)

Loading…
Cancel
Save