diff --git a/go.mod b/go.mod index 456b7b3e2..8170c174e 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/structs v1.0.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.0 // indirect github.com/golang/protobuf v1.3.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect diff --git a/go.sum b/go.sum index 29ab9d22d..ef8eccba1 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/evalphobia/logrus_sentry v0.4.6 h1:825MLGu+SW5H8hMXGeBI7TwX7vgJLd9hz0 github.com/evalphobia/logrus_sentry v0.4.6/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc= github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU= github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/gabriel-vasile/mimetype v1.4.0 h1:Cn9dkdYsMIu56tGho+fqzh7XmvY2YyGU0FnbhiOsEro= +github.com/gabriel-vasile/mimetype v1.4.0/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8= github.com/getsentry/raven-go v0.0.0-20180517221441-ed7bcb39ff10 h1:YO10pIIBftO/kkTFdWhctH96grJ7qiy7bMdiZcIvPKs= github.com/getsentry/raven-go v0.0.0-20180517221441-ed7bcb39ff10/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= @@ -114,6 +116,7 @@ golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/handlers/whatsapp/whatsapp.go b/handlers/whatsapp/whatsapp.go index e1e58af95..29675733f 100644 --- a/handlers/whatsapp/whatsapp.go +++ b/handlers/whatsapp/whatsapp.go @@ -12,6 +12,7 @@ import ( "time" "github.com/buger/jsonparser" + "github.com/gabriel-vasile/mimetype" "github.com/nyaruka/courier" "github.com/nyaruka/courier/backends/rapidpro" "github.com/nyaruka/courier/handlers" @@ -403,10 +404,10 @@ var waIgnoreStatuses = map[string]bool{ // } type mtTextPayload struct { - To string `json:"to" validate:"required"` - Type string `json:"type" validate:"required"` - PreviewURL bool `json:"preview_url,omitempty"` - Text struct { + To string `json:"to" validate:"required"` + Type string `json:"type" validate:"required"` + PreviewURL bool `json:"preview_url,omitempty"` + Text struct { Body string `json:"body" validate:"required"` } `json:"text"` } @@ -795,14 +796,14 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann var payload mtTextPayload if strings.Contains(part, "https://") || strings.Contains(part, "http://") { payload = mtTextPayload{ - To: msg.URN().Path(), - Type: "text", + To: msg.URN().Path(), + Type: "text", PreviewURL: true, } } else { payload = mtTextPayload{ - To: msg.URN().Path(), - Type: "text", + To: msg.URN().Path(), + Type: "text", } } payload.Text.Body = part @@ -934,7 +935,14 @@ func (h *handler) fetchMediaID(msg courier.Msg, mimeType, mediaURL string) (stri return "", logs, errors.Wrapf(err, "error building request to media endpoint") } setWhatsAppAuthHeader(&req.Header, msg.Channel()) - req.Header.Add("Content-Type", http.DetectContentType(rr.Body)) + mtype := http.DetectContentType(rr.Body) + + if mtype != mimeType || mtype == "application/octet-stream" || mtype == "application/zip" { + mimeT := mimetype.Detect(rr.Body) + req.Header.Add("Content-Type", mimeT.String()) + } else { + req.Header.Add("Content-Type", mtype) + } rr, err = utils.MakeHTTPRequest(req) log = courier.NewChannelLogFromRR("Uploading media to WhatsApp", msg.Channel(), msg.ID(), rr).WithError("Error uploading media to WhatsApp", err) logs = append(logs, log)