Browse Source

feat: add send contact backend service

pull/18/head
Aldino Kemal 4 years ago
parent
commit
7ff1657e08
  1. 25
      src/controllers/send_controller.go
  2. 1
      src/services/send_service.go
  3. 22
      src/services/send_service_impl.go
  4. 11
      src/structs/send_struct.go
  5. 14
      src/validations/send_validation.go

25
src/controllers/send_controller.go

@ -21,6 +21,7 @@ func (controller *SendController) Route(app *fiber.App) {
app.Post("/send/image", controller.SendImage)
app.Post("/send/file", controller.SendFile)
app.Post("/send/video", controller.SendVideo)
app.Post("/send/contact", controller.SendContact)
}
func (controller *SendController) SendText(c *fiber.Ctx) error {
@ -135,3 +136,27 @@ func (controller *SendController) SendVideo(c *fiber.Ctx) error {
Results: response,
})
}
func (controller SendController) SendContact(c *fiber.Ctx) error {
var request structs.SendContactRequest
err := c.BodyParser(&request)
utils.PanicIfNeeded(err)
// add validation send contect
validations.ValidateSendContact(request)
if request.Type == structs.TypeGroup {
request.Phone = request.Phone + "@g.us"
} else {
request.Phone = request.Phone + "@s.whatsapp.net"
}
response, err := controller.Service.SendContact(c, request)
utils.PanicIfNeeded(err)
return c.JSON(utils.ResponseData{
Code: 200,
Message: response.Status,
Results: response,
})
}

1
src/services/send_service.go

@ -10,4 +10,5 @@ type SendService interface {
SendImage(c *fiber.Ctx, request structs.SendImageRequest) (response structs.SendImageResponse, err error)
SendFile(c *fiber.Ctx, request structs.SendFileRequest) (response structs.SendFileResponse, err error)
SendVideo(c *fiber.Ctx, request structs.SendVideoRequest) (response structs.SendVideoResponse, err error)
SendContact(c *fiber.Ctx, request structs.SendContactRequest) (response structs.SendContactResponse, err error)
}

22
src/services/send_service_impl.go

@ -286,3 +286,25 @@ func (service SendServiceImpl) SendVideo(c *fiber.Ctx, request structs.SendVideo
return response, nil
}
}
func (service SendServiceImpl) SendContact(_ *fiber.Ctx, request structs.SendContactRequest) (response structs.SendContactResponse, err error) {
utils.MustLogin(service.WaCli)
recipient, ok := utils.ParseJID(request.Phone)
if !ok {
return response, errors.New("invalid JID " + request.Phone)
}
msgVCard := fmt.Sprintf("BEGIN:VCARD\nVERSION:3.0\nN:;%v;;;\nFN:%v\nTEL;type=CELL;waid=%v:+%v\nEND:VCARD",
request.ContactName, request.ContactName, request.ContactPhone, request.ContactPhone)
msg := &waProto.Message{ContactMessage: &waProto.ContactMessage{
DisplayName: proto.String(request.ContactName),
Vcard: proto.String(msgVCard),
}}
ts, err := service.WaCli.SendMessage(recipient, "", msg)
if err != nil {
return response, err
} else {
response.Status = fmt.Sprintf("Contact sent to %s (server timestamp: %s)", request.Phone, ts)
}
return response, nil
}

11
src/structs/send_struct.go

@ -54,3 +54,14 @@ type SendVideoRequest struct {
type SendVideoResponse struct {
Status string `json:"status"`
}
type SendContactRequest struct {
Phone string `json:"phone" form:"phone"`
ContactName string `json:"contact_name" form:"contact_name"`
ContactPhone string `json:"contact_phone" form:"contact_phone"`
Type SendType `json:"type" form:"type"`
}
type SendContactResponse struct {
Status string `json:"status"`
}

14
src/validations/send_validation.go

@ -99,3 +99,17 @@ func ValidateSendVideo(request structs.SendVideoRequest) {
})
}
}
func ValidateSendContact(request structs.SendContactRequest) {
err := validation.ValidateStruct(&request,
validation.Field(&request.Phone, validation.Required, validation.Length(10, 25)),
validation.Field(&request.ContactName, validation.Required),
validation.Field(&request.ContactPhone, validation.Required),
)
if err != nil {
panic(utils.ValidationError{
Message: err.Error(),
})
}
}
Loading…
Cancel
Save