From 01c4dca5c1de68f06f66bb2f45773f64d30b8ebc Mon Sep 17 00:00:00 2001 From: Dimas Restu H Date: Thu, 21 Apr 2022 16:01:50 +0700 Subject: [PATCH] add capabilities to send location --- internal/route.go | 2 +- internal/whatsapp/whatsapp.go | 42 +++++++++++++++++++++++++++++------ pkg/whatsapp/whatsapp.go | 29 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/internal/route.go b/internal/route.go index 92b5083..5b2865c 100644 --- a/internal/route.go +++ b/internal/route.go @@ -32,7 +32,7 @@ func Routes(e *echo.Echo) { e.POST(router.BaseURL+"/login", ctlWhatsApp.Login, middleware.JWTWithConfig(authJWTConfig)) e.POST(router.BaseURL+"/send/text", ctlWhatsApp.SendText, middleware.JWTWithConfig(authJWTConfig)) - // e.POST(router.BaseURL+"/send/location", ctlWhatsApp.SendLocation, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/send/location", ctlWhatsApp.SendLocation, middleware.JWTWithConfig(authJWTConfig)) // e.POST(router.BaseURL+"/send/document", ctlWhatsApp.SendDocument, middleware.JWTWithConfig(authJWTConfig)) // e.POST(router.BaseURL+"/send/audio", ctlWhatsApp.SendAudio, middleware.JWTWithConfig(authJWTConfig)) // e.POST(router.BaseURL+"/send/image", ctlWhatsApp.SendImage, middleware.JWTWithConfig(authJWTConfig)) diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 97b968b..bf95dc9 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -22,6 +22,7 @@ func jwtPayload(c echo.Context) typAuth.AuthJWTClaimsPayload { } func Login(c echo.Context) error { + var err error jid := jwtPayload(c).JID var reqLogin typWhatsApp.RequestLogin @@ -31,7 +32,7 @@ func Login(c echo.Context) error { reqLogin.Output = "html" } - err := pkgWhatsApp.WhatAppConnect(jid) + err = pkgWhatsApp.WhatAppConnect(jid) if err != nil { return router.ResponseInternalError(c, err.Error()) } @@ -72,9 +73,10 @@ func Login(c echo.Context) error { } func Logout(c echo.Context) error { + var err error jid := jwtPayload(c).JID - err := pkgWhatsApp.WhatsAppLogout(jid) + err = pkgWhatsApp.WhatsAppLogout(jid) if err != nil { return router.ResponseInternalError(c, err.Error()) } @@ -83,6 +85,7 @@ func Logout(c echo.Context) error { } func SendText(c echo.Context) error { + var err error jid := jwtPayload(c).JID var reqSendMessage typWhatsApp.RequestSendMessage @@ -93,7 +96,7 @@ func SendText(c echo.Context) error { return router.ResponseBadRequest(c, "Missing Form Value MSISDN") } - err := pkgWhatsApp.WhatsAppSendText(jid, reqSendMessage.RJID, reqSendMessage.Message) + err = pkgWhatsApp.WhatsAppSendText(jid, reqSendMessage.RJID, reqSendMessage.Message) if err != nil { return router.ResponseInternalError(c, err.Error()) } @@ -101,15 +104,40 @@ func SendText(c echo.Context) error { return router.ResponseSuccess(c, "Successfully Send Text Message") } +func SendLocation(c echo.Context) error { + var err error + jid := jwtPayload(c).JID + + var reqSendLocation typWhatsApp.RequestSendLocation + reqSendLocation.RJID = strings.TrimSpace(c.FormValue("msisdn")) + + reqSendLocation.Latitude, err = strconv.ParseFloat(strings.TrimSpace(c.FormValue("latitude")), 64) + if err != nil { + return router.ResponseInternalError(c, "Error While Decoding Latitude to Float64") + } + + reqSendLocation.Longitude, err = strconv.ParseFloat(strings.TrimSpace(c.FormValue("longitude")), 64) + if err != nil { + return router.ResponseInternalError(c, "Error While Decoding Longitude to Float64") + } + + if len(reqSendLocation.RJID) == 0 { + return router.ResponseBadRequest(c, "Missing Form Value MSISDN") + } + + err = pkgWhatsApp.WhatsAppSendLocation(jid, reqSendLocation.RJID, reqSendLocation.Latitude, reqSendLocation.Longitude) + if err != nil { + return router.ResponseInternalError(c, err.Error()) + } + + return router.ResponseSuccess(c, "Successfully Send Location Message") +} + /* TODO: Send Media */ /* -func SendLocation(c echo.Context) error { - return router.ResponseSuccess(c, "Successfully Send Location Message") -} - func SendDocument(c echo.Context) error { return router.ResponseSuccess(c, "Successfully Send Document Message") } diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index cf04ce8..3de1fac 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -240,3 +240,32 @@ func WhatsAppSendText(jid string, rjid string, message string) error { // Return Error WhatsApp Client is not Valid return errors.New("WhatsApp Client is not Valid") } + +func WhatsAppSendLocation(jid string, rjid string, latitude float64, longitude float64) error { + if WhatsAppClient[jid] != nil { + // Make Sure WhatsApp Client is OK + err := WhatsAppClientIsOK(jid) + if err != nil { + return err + } + + // Compose WhatsApp Proto + content := &waproto.Message{ + LocationMessage: &waproto.LocationMessage{ + DegreesLatitude: proto.Float64(latitude), + DegreesLongitude: proto.Float64(longitude), + }, + } + + // Send WhatsApp Message Proto + _, err = WhatsAppClient[jid].SendMessage(WhatsAppComposeJID(rjid), "", content) + if err != nil { + return err + } + + return nil + } + + // Return Error WhatsApp Client is not Valid + return errors.New("WhatsApp Client is not Valid") +}