Browse Source

feat: add reply message

pull/95/head
Aldino Kemal 2 years ago
parent
commit
127b71e65c
  1. 6
      docs/openapi.yaml
  2. 2
      src/cmd/root.go
  3. 2
      src/config/settings.go
  4. 15
      src/domains/app/app.go
  5. 6
      src/domains/app/devices.go
  6. 9
      src/domains/app/login.go
  7. 5
      src/domains/send/text.go
  8. 2
      src/go.mod
  9. 24
      src/services/app.go
  10. 36
      src/services/send.go

6
docs/openapi.yaml

@ -1,7 +1,7 @@
openapi: 3.0.0 openapi: 3.0.0
info: info:
title: WhatsApp API MultiDevice title: WhatsApp API MultiDevice
version: 3.6.0
version: 3.7.0
description: This API is used for sending whatsapp via API description: This API is used for sending whatsapp via API
servers: servers:
- url: http://localhost:3000 - url: http://localhost:3000
@ -212,6 +212,10 @@ paths:
type: string type: string
example: selamat malam example: selamat malam
description: Message to send description: Message to send
reply_message_id:
type: string
example: 3EB089B9D6ADD58153C561
description: Message ID that you want reply
responses: responses:
'200': '200':
description: OK description: OK

2
src/cmd/root.go

@ -96,7 +96,7 @@ func runRest(_ *cobra.Command, _ []string) {
// Service // Service
appService := services.NewAppService(cli, db) appService := services.NewAppService(cli, db)
sendService := services.NewSendService(cli)
sendService := services.NewSendService(cli, appService)
userService := services.NewUserService(cli) userService := services.NewUserService(cli)
messageService := services.NewMessageService(cli) messageService := services.NewMessageService(cli)
groupService := services.NewGroupService(cli) groupService := services.NewGroupService(cli)

2
src/config/settings.go

@ -6,7 +6,7 @@ import (
) )
var ( var (
AppVersion = "v4.7.4"
AppVersion = "v4.8.0"
AppPort = "3000" AppPort = "3000"
AppDebug = false AppDebug = false
AppOs = fmt.Sprintf("AldinoKemal") AppOs = fmt.Sprintf("AldinoKemal")

15
src/domains/app/app.go

@ -2,11 +2,24 @@ package app
import ( import (
"context" "context"
"time"
) )
type IAppService interface { type IAppService interface {
Login(ctx context.Context) (response LoginResponse, err error) Login(ctx context.Context) (response LoginResponse, err error)
Logout(ctx context.Context) (err error) Logout(ctx context.Context) (err error)
Reconnect(ctx context.Context) (err error) Reconnect(ctx context.Context) (err error)
FetchDevices(ctx context.Context) (response []FetchDevicesResponse, err error)
FirstDevice(ctx context.Context) (response DevicesResponse, err error)
FetchDevices(ctx context.Context) (response []DevicesResponse, err error)
}
type DevicesResponse struct {
Name string `json:"name"`
Device string `json:"device"`
}
type LoginResponse struct {
ImagePath string `json:"image_path"`
Duration time.Duration `json:"duration"`
Code string `json:"code"`
} }

6
src/domains/app/devices.go

@ -1,6 +0,0 @@
package app
type FetchDevicesResponse struct {
Name string `json:"name"`
Device string `json:"device"`
}

9
src/domains/app/login.go

@ -1,9 +0,0 @@
package app
import "time"
type LoginResponse struct {
ImagePath string `json:"image_path"`
Duration time.Duration `json:"duration"`
Code string `json:"code"`
}

5
src/domains/send/text.go

@ -1,8 +1,9 @@
package send package send
type MessageRequest struct { type MessageRequest struct {
Phone string `json:"phone" form:"phone"`
Message string `json:"message" form:"message"`
Phone string `json:"phone" form:"phone"`
Message string `json:"message" form:"message"`
ReplyMessageID *string `json:"reply_message_id" form:"reply_message_id"`
} }
type MessageResponse struct { type MessageResponse struct {

2
src/go.mod

@ -1,6 +1,6 @@
module github.com/aldinokemal/go-whatsapp-web-multidevice module github.com/aldinokemal/go-whatsapp-web-multidevice
go 1.20
go 1.21
require ( require (
github.com/PuerkitoBio/goquery v1.8.1 github.com/PuerkitoBio/goquery v1.8.1

24
src/services/app.go

@ -140,7 +140,27 @@ func (service serviceApp) Reconnect(_ context.Context) (err error) {
return service.WaCli.Connect() return service.WaCli.Connect()
} }
func (service serviceApp) FetchDevices(_ context.Context) (response []domainApp.FetchDevicesResponse, err error) {
func (service serviceApp) FirstDevice(ctx context.Context) (response domainApp.DevicesResponse, err error) {
if service.WaCli == nil {
return response, pkgError.ErrWaCLI
}
devices, err := service.db.GetFirstDevice()
if err != nil {
return response, err
}
response.Device = devices.ID.String()
if devices.PushName != "" {
response.Name = devices.PushName
} else {
response.Name = devices.BusinessName
}
return response, nil
}
func (service serviceApp) FetchDevices(_ context.Context) (response []domainApp.DevicesResponse, err error) {
if service.WaCli == nil { if service.WaCli == nil {
return response, pkgError.ErrWaCLI return response, pkgError.ErrWaCLI
} }
@ -151,7 +171,7 @@ func (service serviceApp) FetchDevices(_ context.Context) (response []domainApp.
} }
for _, device := range devices { for _, device := range devices {
var d domainApp.FetchDevicesResponse
var d domainApp.DevicesResponse
d.Device = device.ID.String() d.Device = device.ID.String()
if device.PushName != "" { if device.PushName != "" {
d.Name = device.PushName d.Name = device.PushName

36
src/services/send.go

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/domains/app"
domainSend "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/send" domainSend "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/send"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error" pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils" "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
@ -21,7 +22,8 @@ import (
) )
type serviceSend struct { type serviceSend struct {
WaCli *whatsmeow.Client
WaCli *whatsmeow.Client
appService app.IAppService
} }
type metadata struct { type metadata struct {
@ -29,9 +31,10 @@ type metadata struct {
Content string Content string
} }
func NewSendService(waCli *whatsmeow.Client) domainSend.ISendService {
func NewSendService(waCli *whatsmeow.Client, appService app.IAppService) domainSend.ISendService {
return &serviceSend{ return &serviceSend{
WaCli: waCli,
WaCli: waCli,
appService: appService,
} }
} }
@ -45,7 +48,34 @@ func (service serviceSend) SendText(ctx context.Context, request domainSend.Mess
return response, err return response, err
} }
// Send message
msg := &waProto.Message{Conversation: proto.String(request.Message)} msg := &waProto.Message{Conversation: proto.String(request.Message)}
// Reply message
if request.ReplyMessageID != nil && *request.ReplyMessageID != "" {
participantJID := dataWaRecipient.String()
if len(*request.ReplyMessageID) < 28 {
firstDevice, err := service.appService.FirstDevice(ctx)
if err != nil {
return response, err
}
participantJID = firstDevice.Device
}
msg = &waProto.Message{
ExtendedTextMessage: &waProto.ExtendedTextMessage{
Text: proto.String(request.Message),
ContextInfo: &waProto.ContextInfo{
StanzaId: request.ReplyMessageID,
Participant: proto.String(participantJID),
QuotedMessage: &waProto.Message{
Conversation: proto.String(request.Message),
},
},
},
}
}
ts, err := service.WaCli.SendMessage(ctx, dataWaRecipient, msg) ts, err := service.WaCli.SendMessage(ctx, dataWaRecipient, msg)
if err != nil { if err != nil {
return response, err return response, err

Loading…
Cancel
Save