Browse Source

add capabilities to send location

pull/4/head
Dimas Restu H 4 years ago
parent
commit
01c4dca5c1
  1. 2
      internal/route.go
  2. 42
      internal/whatsapp/whatsapp.go
  3. 29
      pkg/whatsapp/whatsapp.go

2
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))

42
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")
}

29
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")
}
Loading…
Cancel
Save