Browse Source

feat: set webhook downloadable image

pull/51/head
Aldino Kemal 3 years ago
parent
commit
2bf60a3495
  1. 2
      src/config/settings.go
  2. 5
      src/go.mod
  3. 3
      src/go.sum
  4. 17
      src/pkg/whatsapp/whatsapp.go

2
src/config/settings.go

@ -6,7 +6,7 @@ import (
)
var (
AppVersion = "v4.4.0"
AppVersion = "v4.4.1"
AppPort = "3000"
AppDebug = false
AppOs = fmt.Sprintf("AldinoKemal")

5
src/go.mod

@ -11,10 +11,12 @@ require (
github.com/h2non/bimg v1.1.9
github.com/markbates/pkger v0.17.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/sirupsen/logrus v1.9.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.0
github.com/valyala/fasthttp v1.43.0
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf
go.mau.fi/whatsmeow v0.0.0-20221213225758-70ef67df3c68
google.golang.org/protobuf v1.28.1
)
@ -35,12 +37,11 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

3
src/go.sum

@ -696,8 +696,9 @@ golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0=
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=

17
src/pkg/whatsapp/whatsapp.go

@ -8,6 +8,7 @@ import (
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/internal/websocket"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/sirupsen/logrus"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/appstate"
waProto "go.mau.fi/whatsmeow/binary/proto"
@ -211,9 +212,9 @@ func handler(rawEvt interface{}) {
}
if config.WhatsappAutoReplyWebhook != "" && !isGroupJid(evt.Info.Chat.String()) {
go func() {
_ = sendAutoReplyWebhook(evt)
}()
if err := sendAutoReplyWebhook(evt); err != nil {
logrus.Error("Failed to send webhoook", err)
}
}
case *events.Receipt:
if evt.Type == events.ReceiptTypeRead || evt.Type == events.ReceiptTypeReadSelf {
@ -254,6 +255,7 @@ func handler(rawEvt interface{}) {
}
func sendAutoReplyWebhook(evt *events.Message) error {
logrus.Info("Sending webhook to", config.WhatsappAutoReplyWebhook)
client := &http.Client{Timeout: 10 * time.Second}
imageMedia := evt.Message.GetImageMessage()
body := map[string]any{
@ -281,17 +283,18 @@ func sendAutoReplyWebhook(evt *events.Message) error {
body["image"] = path
}
postBody, err := json.Marshal(body)
if err != nil {
return pkgError.WebhookError(fmt.Sprintf("Failed to marshal body: %v", err))
}
postBody, _ := json.Marshal(body)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, config.WhatsappAutoReplyWebhook, bytes.NewBuffer(postBody))
req, err := http.NewRequest(http.MethodPost, config.WhatsappAutoReplyWebhook, bytes.NewBuffer(postBody))
if err != nil {
log.Errorf("error when create http object %v", err)
return pkgError.WebhookError(fmt.Sprintf("error when create http object %v", err))
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Errorf("error when submit webhook %v", err)
return pkgError.WebhookError(fmt.Sprintf("error when submit webhook %v", err))
}
defer resp.Body.Close()

Loading…
Cancel
Save