GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix: rename phone parameter & fix validation error message
Aldino Kemal815f074a665e
diff --git a/controllers/send_controller.go b/controllers/send_controller.goindex 69a84ec9..ca53abb1 100644--- a/controllers/send_controller.go+++ b/controllers/send_controller.go@@ -30,7 +30,7 @@ func (controller *SendController) SendText(c *fiber.Ctx) error {// add validation send messagevalidations.ValidateSendMessage(request)- request.PhoneNumber = request.PhoneNumber + "@s.whatsapp.net"+ request.Phone = request.Phone + "@s.whatsapp.net"response, err := controller.Service.SendText(c, request)utils.PanicIfNeeded(err)@@ -54,7 +54,7 @@ func (controller *SendController) SendImage(c *fiber.Ctx) error {//add validation send imagevalidations.ValidateSendImage(request)- request.PhoneNumber = request.PhoneNumber + "@s.whatsapp.net"+ request.Phone = request.Phone + "@s.whatsapp.net"response, err := controller.Service.SendImage(c, request)utils.PanicIfNeeded(err)@@ -78,7 +78,7 @@ func (controller *SendController) SendFile(c *fiber.Ctx) error {//add validation send imagevalidations.ValidateSendFile(request)- request.PhoneNumber = request.PhoneNumber + "@s.whatsapp.net"+ request.Phone = request.Phone + "@s.whatsapp.net"response, err := controller.Service.SendFile(c, request)utils.PanicIfNeeded(err)diff --git a/controllers/user_controller.go b/controllers/user_controller.goindex 931ec168..382ec3a4 100644--- a/controllers/user_controller.go+++ b/controllers/user_controller.go@@ -31,7 +31,7 @@ func (controller *UserController) UserInfo(c *fiber.Ctx) error {// add validation send messagevalidations.ValidateUserInfo(request)- request.PhoneNumber = request.PhoneNumber + "@s.whatsapp.net"+ request.Phone = request.Phone + "@s.whatsapp.net"response, err := controller.Service.UserInfo(c, request)utils.PanicIfNeeded(err)@@ -50,7 +50,7 @@ func (controller *UserController) UserAvatar(c *fiber.Ctx) error {// add validation send messagevalidations.ValidateUserAvatar(request)- request.PhoneNumber = request.PhoneNumber + "@s.whatsapp.net"+ request.Phone = request.Phone + "@s.whatsapp.net"response, err := controller.Service.UserAvatar(c, request)utils.PanicIfNeeded(err)diff --git a/readme.md b/readme.mdindex 9c320a62..1a53ce71 100644--- a/readme.md+++ b/readme.md@@ -28,14 +28,14 @@ Current API| ✅ | Login | GET | /app/login | || ✅ | Logout | GET | /app/logout | || ✅ | Reconnect | GET | /app/reconnect | |-| ✅ | User Info | GET | /user/info | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone_number</td><td>string</td><td>querystring</td><td>6289685024099</td></tr></tbody></table> |-| ✅ | User Avatar | GET | /user/avatar | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone_number</td><td>string</td><td>querystring</td><td>6289685024099</td></tr></tbody></table> |+| ✅ | User Info | GET | /user/info | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone</td><td>string</td><td>querystring</td><td>6289685024099</td></tr></tbody></table> |+| ✅ | User Avatar | GET | /user/avatar | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone</td><td>string</td><td>querystring</td><td>6289685024099</td></tr></tbody></table> || ✅ | User My Group List | GET | /user/my/groups | || ✅ | User My Privacy Setting | GET | /user/my/privacy | |-| ✅ | Send Message (Text) | POST | /send/message | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone_number</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr> <td>message</td><td>string</td><td>form-data</td><td>Hello guys this is testing</td></tr></tbody></table> |-| ✅ | Send Message (Image) | POST | /send/image | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone_number</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr> <td>caption</td><td>string</td><td>form-data</td><td>Hello guys this is caption</td></tr><tr> <td>view_once</td><td>bool</td><td>form-data</td><td>false</td></tr><tr> <td>image</td><td>binary</td><td>form-data</td><td>image/jpg,image/jpeg,image/png</td></tr></tbody></table> |-| ✅ | Send Message (File) | POST | /send/file | <table><thead><tr><th>Param</th><th>Type</th><th>Type</th><th>Example</th></tr></thead><tbody><tr><td>phone_number</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr><td>file</td><td>binary</td><td>form-data</td><td>any (max: 10MB)</td></tr></tbody></table> |-| ❌ | Send Message (Video) | POST | /send/video | <table><thead><tr><th>Param</th><th>Type</th><th>Type</th><th>Example</th></tr></thead><tbody><tr><td>phone_number</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr><td>video</td><td>binary</td><td>form-data</td><td>mp4/avi/mkv</td></tr></tbody></table> |+| ✅ | Send Message (Text) | POST | /send/message | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr> <td>message</td><td>string</td><td>form-data</td><td>Hello guys this is testing</td></tr></tbody></table> |+| ✅ | Send Message (Image) | POST | /send/image | <table> <thead> <tr> <th>Param</th> <th>Type</th> <th>Type</th> <th>Example</th> </tr></thead> <tbody> <tr> <td>phone</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr> <td>caption</td><td>string</td><td>form-data</td><td>Hello guys this is caption</td></tr><tr> <td>view_once</td><td>bool</td><td>form-data</td><td>false</td></tr><tr> <td>image</td><td>binary</td><td>form-data</td><td>image/jpg,image/jpeg,image/png</td></tr></tbody></table> |+| ✅ | Send Message (File) | POST | /send/file | <table><thead><tr><th>Param</th><th>Type</th><th>Type</th><th>Example</th></tr></thead><tbody><tr><td>phone</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr><td>file</td><td>binary</td><td>form-data</td><td>any (max: 10MB)</td></tr></tbody></table> |+| ❌ | Send Message (Video) | POST | /send/video | <table><thead><tr><th>Param</th><th>Type</th><th>Type</th><th>Example</th></tr></thead><tbody><tr><td>phone</td><td>string</td><td>form-data</td><td>6289685024099</td></tr><tr><td>video</td><td>binary</td><td>form-data</td><td>mp4/avi/mkv</td></tr></tbody></table> |```✅ = Availablediff --git a/services/send_service_impl.go b/services/send_service_impl.goindex 41f6349a..2d08e3b4 100644--- a/services/send_service_impl.go+++ b/services/send_service_impl.go@@ -28,12 +28,11 @@ func NewSendService(waCli *whatsmeow.Client) SendService {}func (service SendServiceImpl) SendText(_ *fiber.Ctx, request structs.SendMessageRequest) (response structs.SendMessageResponse, err error) {- if !service.WaCli.IsLoggedIn() {- panic(utils.AuthError{Message: "you are not loggin"})- }- recipient, ok := utils.ParseJID(request.PhoneNumber)+ utils.MustLogin(service.WaCli)++ recipient, ok := utils.ParseJID(request.Phone)if !ok {- return response, errors.New("invalid JID " + request.PhoneNumber)+ return response, errors.New("invalid JID " + request.Phone)}msg := &waProto.Message{Conversation: proto.String(request.Message)}ts, err := service.WaCli.SendMessage(recipient, "", msg)@@ -46,9 +45,8 @@ func (service SendServiceImpl) SendText(_ *fiber.Ctx, request structs.SendMessag}func (service SendServiceImpl) SendImage(c *fiber.Ctx, request structs.SendImageRequest) (response structs.SendImageResponse, err error) {- if !service.WaCli.IsLoggedIn() {- panic(utils.AuthError{Message: "you are not loggin"})- }+ utils.MustLogin(service.WaCli)+// Resize imageoriImagePath := fmt.Sprintf("%s/%s", config.PathSendItems, request.Image.Filename)err = c.SaveFile(request.Image, oriImagePath)@@ -80,9 +78,9 @@ func (service SendServiceImpl) SendImage(c *fiber.Ctx, request structs.SendImage// Send to WA serverdataWaCaption := request.Caption- dataWaRecipient, ok := utils.ParseJID(request.PhoneNumber)+ dataWaRecipient, ok := utils.ParseJID(request.Phone)if !ok {- return response, errors.New("invalid JID " + request.PhoneNumber)+ return response, errors.New("invalid JID " + request.Phone)}dataWaImage, err := os.ReadFile(newImagePath)if err != nil {@@ -116,9 +114,8 @@ func (service SendServiceImpl) SendImage(c *fiber.Ctx, request structs.SendImage}func (service SendServiceImpl) SendFile(c *fiber.Ctx, request structs.SendFileRequest) (response structs.SendFileResponse, err error) {- if !service.WaCli.IsLoggedIn() {- panic(utils.AuthError{Message: "you are not loggin"})- }+ utils.MustLogin(service.WaCli)+// Resize imageoriFilePath := fmt.Sprintf("%s/%s", config.PathSendItems, request.File.Filename)err = c.SaveFile(request.File, oriFilePath)@@ -138,9 +135,9 @@ func (service SendServiceImpl) SendFile(c *fiber.Ctx, request structs.SendFileRe}// Send to WA server- dataWaRecipient, ok := utils.ParseJID(request.PhoneNumber)+ dataWaRecipient, ok := utils.ParseJID(request.Phone)if !ok {- return response, errors.New("invalid JID " + request.PhoneNumber)+ return response, errors.New("invalid JID " + request.Phone)}dataWaFile, err := os.ReadFile(oriFilePath)if err != nil {diff --git a/services/user_service_impl.go b/services/user_service_impl.goindex 5ef6dc5d..253add1c 100644--- a/services/user_service_impl.go+++ b/services/user_service_impl.go@@ -21,13 +21,12 @@ func NewUserService(waCli *whatsmeow.Client) UserService {}func (service UserServiceImpl) UserInfo(_ *fiber.Ctx, request structs.UserInfoRequest) (response structs.UserInfoResponse, err error) {- if !service.WaCli.IsLoggedIn() {- panic(utils.AuthError{Message: "you are not loggin"})- }+ utils.MustLogin(service.WaCli)+var jids []types.JID- jid, ok := utils.ParseJID(request.PhoneNumber)+ jid, ok := utils.ParseJID(request.Phone)if !ok {- return response, errors.New("invalid JID " + request.PhoneNumber)+ return response, errors.New("invalid JID " + request.Phone)}jids = append(jids, jid)@@ -63,12 +62,11 @@ func (service UserServiceImpl) UserInfo(_ *fiber.Ctx, request structs.UserInfoRe}func (service UserServiceImpl) UserAvatar(_ *fiber.Ctx, request structs.UserAvatarRequest) (response structs.UserAvatarResponse, err error) {- if !service.WaCli.IsLoggedIn() {- panic(utils.AuthError{Message: "you are not loggin"})- }- jid, ok := utils.ParseJID(request.PhoneNumber)+ utils.MustLogin(service.WaCli)++ jid, ok := utils.ParseJID(request.Phone)if !ok {- return response, errors.New("invalid JID " + request.PhoneNumber)+ return response, errors.New("invalid JID " + request.Phone)}pic, err := service.WaCli.GetProfilePictureInfo(jid, false)if err != nil {@@ -85,6 +83,8 @@ func (service UserServiceImpl) UserAvatar(_ *fiber.Ctx, request structs.UserAvat}func (service UserServiceImpl) UserMyListGroups(_ *fiber.Ctx) (response structs.UserMyListGroupsResponse, err error) {+ utils.MustLogin(service.WaCli)+groups, err := service.WaCli.GetJoinedGroups()if err != nil {return@@ -99,6 +99,8 @@ func (service UserServiceImpl) UserMyListGroups(_ *fiber.Ctx) (response structs.}func (service UserServiceImpl) UserMyPrivacySetting(_ *fiber.Ctx) (response structs.UserMyPrivacySettingResponse, err error) {+ utils.MustLogin(service.WaCli)+resp, err := service.WaCli.TryFetchPrivacySettings(false)if err != nil {returndiff --git a/structs/send_struct.go b/structs/send_struct.goindex 4d808dc5..23298c87 100644--- a/structs/send_struct.go+++ b/structs/send_struct.go@@ -8,7 +8,7 @@ import (// ============================== USER ==============================type UserInfoRequest struct {- PhoneNumber string `json:"phone_number" query:"phone_number"`+ Phone string `json:"phone" query:"phone"`}type UserInfoResponseDataDevice struct {@@ -31,7 +31,7 @@ type UserInfoResponse struct {}type UserAvatarRequest struct {- PhoneNumber string `json:"phone_number" query:"phone_number"`+ Phone string `json:"phone" query:"phone"`}type UserAvatarResponse struct {@@ -57,8 +57,8 @@ type UserMyListGroupsResponse struct {// ============================== SEND ==============================type SendMessageRequest struct {- PhoneNumber string `json:"phone_number" form:"phone_number"`- Message string `json:"message" form:"message"`+ Phone string `json:"phone" form:"phone"`+ Message string `json:"message" form:"message"`}type SendMessageResponse struct {@@ -66,10 +66,10 @@ type SendMessageResponse struct {}type SendImageRequest struct {- PhoneNumber string `json:"phone_number" form:"phone_number"`- Caption string `json:"caption" form:"caption"`- Image *multipart.FileHeader `json:"image" form:"image"`- ViewOnce bool `json:"view_once" form:"view_once"`+ Phone string `json:"phone" form:"phone"`+ Caption string `json:"caption" form:"caption"`+ Image *multipart.FileHeader `json:"image" form:"image"`+ ViewOnce bool `json:"view_once" form:"view_once"`}type SendImageResponse struct {@@ -77,8 +77,8 @@ type SendImageResponse struct {}type SendFileRequest struct {- PhoneNumber string `json:"phone_number" form:"phone_number"`- File *multipart.FileHeader `json:"file" form:"file"`+ Phone string `json:"phone" form:"phone"`+ File *multipart.FileHeader `json:"file" form:"file"`}type SendFileResponse struct {diff --git a/utils/whatsapp.go b/utils/whatsapp.goindex fec3823a..5d4b924f 100644--- a/utils/whatsapp.go+++ b/utils/whatsapp.go@@ -76,6 +76,14 @@ func InitWaCLI(storeContainer *sqlstore.Container) *whatsmeow.Client {return cli}+func MustLogin(waCli *whatsmeow.Client) {+ if !waCli.IsConnected() {+ panic(AuthError{Message: "you are not connect to whatsapp server, please reconnect"})+ } else if !waCli.IsLoggedIn() {+ panic(AuthError{Message: "you are not login"})+ }+}+func handler(rawEvt interface{}) {switch evt := rawEvt.(type) {case *events.AppStateSyncComplete:diff --git a/validations/send_validation.go b/validations/send_validation.goindex 1fa6d04f..16d463fa 100644--- a/validations/send_validation.go+++ b/validations/send_validation.go@@ -10,7 +10,7 @@ import (func ValidateSendMessage(request structs.SendMessageRequest) {err := validation.ValidateStruct(&request,- validation.Field(&request.PhoneNumber, validation.Required, is.E164, validation.Length(10, 15)),+ validation.Field(&request.Phone, validation.Required, is.E164, validation.Length(10, 15)),validation.Field(&request.Message, validation.Required, validation.Length(4, 50)),)@@ -18,7 +18,7 @@ func ValidateSendMessage(request structs.SendMessageRequest) {panic(utils.ValidationError{Message: err.Error(),})- } else if !strings.HasPrefix(request.PhoneNumber, "62") {+ } else if !strings.HasPrefix(request.Phone, "62") {panic(utils.ValidationError{Message: "this is only work for indonesia country (start with 62)",})@@ -27,7 +27,7 @@ func ValidateSendMessage(request structs.SendMessageRequest) {func ValidateSendImage(request structs.SendImageRequest) {err := validation.ValidateStruct(&request,- validation.Field(&request.PhoneNumber, validation.Required, is.E164, validation.Length(10, 15)),+ validation.Field(&request.Phone, validation.Required, is.E164, validation.Length(10, 15)),validation.Field(&request.Caption, validation.When(true, validation.Length(4, 200))),validation.Field(&request.Image, validation.Required),)@@ -36,7 +36,7 @@ func ValidateSendImage(request structs.SendImageRequest) {panic(utils.ValidationError{Message: err.Error(),})- } else if !strings.HasPrefix(request.PhoneNumber, "62") {+ } else if !strings.HasPrefix(request.Phone, "62") {panic(utils.ValidationError{Message: "this is only work for indonesia country (start with 62)",})@@ -58,7 +58,7 @@ func ValidateSendImage(request structs.SendImageRequest) {func ValidateSendFile(request structs.SendFileRequest) {err := validation.ValidateStruct(&request,- validation.Field(&request.PhoneNumber, validation.Required, is.E164, validation.Length(10, 15)),+ validation.Field(&request.Phone, validation.Required, is.E164, validation.Length(10, 15)),validation.Field(&request.File, validation.Required),)@@ -66,7 +66,7 @@ func ValidateSendFile(request structs.SendFileRequest) {panic(utils.ValidationError{Message: err.Error(),})- } else if !strings.HasPrefix(request.PhoneNumber, "62") {+ } else if !strings.HasPrefix(request.Phone, "62") {panic(utils.ValidationError{Message: "this is only work for indonesia country (start with 62)",})diff --git a/validations/user_validation.go b/validations/user_validation.goindex cdbe067e..5d6e83be 100644--- a/validations/user_validation.go+++ b/validations/user_validation.go@@ -9,7 +9,7 @@ import (func ValidateUserInfo(request structs.UserInfoRequest) {err := validation.ValidateStruct(&request,- validation.Field(&request.PhoneNumber, validation.Required, is.E164, validation.Length(10, 15)),+ validation.Field(&request.Phone, validation.Required, is.E164, validation.Length(10, 15)),)if err != nil {@@ -20,7 +20,7 @@ func ValidateUserInfo(request structs.UserInfoRequest) {}func ValidateUserAvatar(request structs.UserAvatarRequest) {err := validation.ValidateStruct(&request,- validation.Field(&request.PhoneNumber, validation.Required, is.E164, validation.Length(10, 15)),+ validation.Field(&request.Phone, validation.Required, is.E164, validation.Length(10, 15)),)if err != nil {