Browse Source

fix: rename phone parameter & fix validation error message

pull/3/head
Aldino Kemal 4 years ago
parent
commit
815f074a66
  1. 6
      controllers/send_controller.go
  2. 4
      controllers/user_controller.go
  3. 12
      readme.md
  4. 27
      services/send_service_impl.go
  5. 22
      services/user_service_impl.go
  6. 20
      structs/send_struct.go
  7. 8
      utils/whatsapp.go
  8. 12
      validations/send_validation.go
  9. 4
      validations/user_validation.go

6
controllers/send_controller.go

@ -30,7 +30,7 @@ func (controller *SendController) SendText(c *fiber.Ctx) error {
// add validation send message
validations.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 image
validations.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 image
validations.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)

4
controllers/user_controller.go

@ -31,7 +31,7 @@ func (controller *UserController) UserInfo(c *fiber.Ctx) error {
// add validation send message
validations.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 message
validations.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)

12
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> |
```
✅ = Available

27
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 image
oriImagePath := 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 server
dataWaCaption := 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 image
oriFilePath := 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 {

22
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 {
return

20
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 {

8
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:

12
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)",
})

4
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 {

Loading…
Cancel
Save