Skip to content

Commit

Permalink
feat: add reply message (#95)
Browse files Browse the repository at this point in the history
* feat: add reply message

* feat: add reply message id in UI
  • Loading branch information
aldinokemal authored Oct 11, 2023
1 parent ace21c6 commit 7685f39
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 27 deletions.
6 changes: 5 additions & 1 deletion docs/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: WhatsApp API MultiDevice
version: 3.6.0
version: 3.7.0
description: This API is used for sending whatsapp via API
servers:
- url: http://localhost:3000
Expand Down Expand Up @@ -212,6 +212,10 @@ paths:
type: string
example: selamat malam
description: Message to send
reply_message_id:
type: string
example: 3EB089B9D6ADD58153C561
description: Message ID that you want reply
responses:
'200':
description: OK
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func runRest(_ *cobra.Command, _ []string) {

// Service
appService := services.NewAppService(cli, db)
sendService := services.NewSendService(cli)
sendService := services.NewSendService(cli, appService)
userService := services.NewUserService(cli)
messageService := services.NewMessageService(cli)
groupService := services.NewGroupService(cli)
Expand Down
2 changes: 1 addition & 1 deletion src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
AppVersion = "v4.7.4"
AppVersion = "v4.8.0"
AppPort = "3000"
AppDebug = false
AppOs = fmt.Sprintf("AldinoKemal")
Expand Down
15 changes: 14 additions & 1 deletion src/domains/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ package app

import (
"context"
"time"
)

type IAppService interface {
Login(ctx context.Context) (response LoginResponse, err error)
Logout(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 changes: 0 additions & 6 deletions src/domains/app/devices.go

This file was deleted.

9 changes: 0 additions & 9 deletions src/domains/app/login.go

This file was deleted.

5 changes: 3 additions & 2 deletions src/domains/send/text.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package send

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 {
Expand Down
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/aldinokemal/go-whatsapp-web-multidevice

go 1.20
go 1.21

require (
github.com/PuerkitoBio/goquery v1.8.1
Expand Down
24 changes: 22 additions & 2 deletions src/services/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,27 @@ func (service serviceApp) Reconnect(_ context.Context) (err error) {
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 {
return response, pkgError.ErrWaCLI
}
Expand All @@ -151,7 +171,7 @@ func (service serviceApp) FetchDevices(_ context.Context) (response []domainApp.
}

for _, device := range devices {
var d domainApp.FetchDevicesResponse
var d domainApp.DevicesResponse
d.Device = device.ID.String()
if device.PushName != "" {
d.Name = device.PushName
Expand Down
36 changes: 33 additions & 3 deletions src/services/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"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"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
Expand All @@ -21,17 +22,19 @@ import (
)

type serviceSend struct {
WaCli *whatsmeow.Client
WaCli *whatsmeow.Client
appService app.IAppService
}

type metadata struct {
Name string
Content string
}

func NewSendService(waCli *whatsmeow.Client) domainSend.ISendService {
func NewSendService(waCli *whatsmeow.Client, appService app.IAppService) domainSend.ISendService {
return &serviceSend{
WaCli: waCli,
WaCli: waCli,
appService: appService,
}
}

Expand All @@ -45,7 +48,34 @@ func (service serviceSend) SendText(ctx context.Context, request domainSend.Mess
return response, err
}

// Send 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)
if err != nil {
return response, err
Expand Down
10 changes: 10 additions & 0 deletions src/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ <h1 class="ui header center aligned">[[ app_name ]]</h1>
aria-label="phone">
<input :value="message_phone_id" disabled aria-label="whatsapp_id">
</div>
<div class="field">
<label>Reply Message ID</label>
<input v-model="message_reply_message_id" type="text" placeholder="Optional: 57D29F74B7FC62F57D8AC2C840279B5B/3EB0288F008D32FCD0A424"
aria-label="reply_message_id">
</div>
<div class="field">
<label>Message</label>
<textarea v-model="message_text" type="text" placeholder="Hello this is message text"
Expand Down Expand Up @@ -868,6 +873,7 @@ <h1 class="ui header center aligned">[[ app_name ]]</h1>
message_type: 'user',
message_phone: '',
message_text: '',
message_reply_message_id: '',
message_loading: false,
}
},
Expand Down Expand Up @@ -900,6 +906,9 @@ <h1 class="ui header center aligned">[[ app_name ]]</h1>
let payload = new FormData();
payload.append("phone", this.message_phone_id)
payload.append("message", this.message_text)
if (this.message_reply_message_id !== '') {
payload.append("reply_message_id", this.message_reply_message_id)
}
let response = await http.post(`/send/message`, payload)
this.sendMessageReset();
resolve(response.data.message)
Expand All @@ -918,6 +927,7 @@ <h1 class="ui header center aligned">[[ app_name ]]</h1>
this.message_phone = '';
this.message_text = '';
this.message_type = 'user';
this.message_reply_message_id = '';
},
}
}
Expand Down

0 comments on commit 7685f39

Please sign in to comment.