diff --git a/internal/route.go b/internal/route.go index 92b328f..9787540 100644 --- a/internal/route.go +++ b/internal/route.go @@ -34,8 +34,8 @@ func Routes(e *echo.Echo) { 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/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)) + e.POST(router.BaseURL+"/send/image", ctlWhatsApp.SendImage, middleware.JWTWithConfig(authJWTConfig)) + e.POST(router.BaseURL+"/send/audio", ctlWhatsApp.SendAudio, middleware.JWTWithConfig(authJWTConfig)) // e.POST(router.BaseURL+"/send/video", ctlWhatsApp.SendVideo, middleware.JWTWithConfig(authJWTConfig)) e.POST(router.BaseURL+"/logout", ctlWhatsApp.Logout, middleware.JWTWithConfig(authJWTConfig)) } diff --git a/internal/whatsapp/whatsapp.go b/internal/whatsapp/whatsapp.go index 5baa80c..216f2d3 100644 --- a/internal/whatsapp/whatsapp.go +++ b/internal/whatsapp/whatsapp.go @@ -25,7 +25,10 @@ func jwtPayload(c echo.Context) typAuth.AuthJWTClaimsPayload { } func convertFileToBuffer(file multipart.File) ([]byte, error) { + // Create Empty Buffer buffer := bytes.NewBuffer(nil) + + // Copy File Stream to Buffer _, err := io.Copy(buffer, file) if err != nil { return bytes.NewBuffer(nil).Bytes(), err @@ -49,6 +52,13 @@ func sendContent(c echo.Context, mediaType string) error { case "document": fileStream, fileHeader, err = c.Request().FormFile("document") reqSendMessage.Message = fileHeader.Filename + + case "image": + fileStream, fileHeader, err = c.Request().FormFile("image") + reqSendMessage.Message = strings.TrimSpace(c.FormValue("caption")) + + case "audio": + fileStream, fileHeader, err = c.Request().FormFile("audio") } // Don't Forget to Close The File Stream @@ -79,6 +89,12 @@ func sendContent(c echo.Context, mediaType string) error { switch mediaType { case "document": err = pkgWhatsApp.WhatsAppSendDocument(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message) + + case "image": + err = pkgWhatsApp.WhatsAppSendImage(jid, reqSendMessage.RJID, fileBytes, fileType, reqSendMessage.Message) + + case "audio": + err = pkgWhatsApp.WhatsAppSendAudio(jid, reqSendMessage.RJID, fileBytes, fileType) } // Return Internal Server Error @@ -206,19 +222,19 @@ func SendDocument(c echo.Context) error { return sendContent(c, "document") } -/* - TODO: Send Media -*/ - -/* func SendImage(c echo.Context) error { - return router.ResponseSuccess(c, "Successfully Send Image Message") + return sendContent(c, "image") } func SendAudio(c echo.Context) error { - return router.ResponseSuccess(c, "Successfully Send Audio Message") + return sendContent(c, "audio") } +/* + TODO: Send Media +*/ + +/* func SendVideo(c echo.Context) error { return router.ResponseSuccess(c, "Successfully Send Video Message") } diff --git a/pkg/whatsapp/whatsapp.go b/pkg/whatsapp/whatsapp.go index 693b933..d164ef4 100644 --- a/pkg/whatsapp/whatsapp.go +++ b/pkg/whatsapp/whatsapp.go @@ -308,3 +308,78 @@ func WhatsAppSendDocument(jid string, rjid string, fileBytes []byte, fileType st // Return Error WhatsApp Client is not Valid return errors.New("WhatsApp Client is not Valid") } + +func WhatsAppSendImage(jid string, rjid string, imageBytes []byte, imageType string, imageCaption string) error { + if WhatsAppClient[jid] != nil { + // Make Sure WhatsApp Client is OK + err := WhatsAppClientIsOK(jid) + if err != nil { + return err + } + + // Upload File to WhatsApp Storage Server + imageUploaded, err := WhatsAppClient[jid].Upload(context.Background(), imageBytes, whatsmeow.MediaImage) + + // Compose WhatsApp Proto + content := &waproto.Message{ + ImageMessage: &waproto.ImageMessage{ + Url: proto.String(imageUploaded.URL), + DirectPath: proto.String(imageUploaded.DirectPath), + Mimetype: proto.String(imageType), + Caption: proto.String(imageCaption), + FileLength: proto.Uint64(imageUploaded.FileLength), + FileSha256: imageUploaded.FileSHA256, + FileEncSha256: imageUploaded.FileEncSHA256, + MediaKey: imageUploaded.MediaKey, + }, + } + + // 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") +} + +func WhatsAppSendAudio(jid string, rjid string, audioBytes []byte, audioType string) error { + if WhatsAppClient[jid] != nil { + // Make Sure WhatsApp Client is OK + err := WhatsAppClientIsOK(jid) + if err != nil { + return err + } + + // Upload File to WhatsApp Storage Server + audioUploaded, err := WhatsAppClient[jid].Upload(context.Background(), audioBytes, whatsmeow.MediaAudio) + + // Compose WhatsApp Proto + content := &waproto.Message{ + AudioMessage: &waproto.AudioMessage{ + Url: proto.String(audioUploaded.URL), + DirectPath: proto.String(audioUploaded.DirectPath), + Mimetype: proto.String(audioType), + FileLength: proto.Uint64(audioUploaded.FileLength), + FileSha256: audioUploaded.FileSHA256, + FileEncSha256: audioUploaded.FileEncSHA256, + MediaKey: audioUploaded.MediaKey, + }, + } + + // 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") +}