Skip to content

Commit d1ff63e

Browse files
edilsonoliveiramaDavidsonGomes
authored andcommitted
fix(instance): resolve empty pairing code on POST /instance/pair
The Pair function was calling PairPhone directly without checking if the client was initialized, and was silently swallowing errors from PairPhone. This caused two problems: 1. If the client was nil or disconnected, PairPhone returned an error but the function ignored it and returned HTTP 200 with an empty PairingCode field, misleading the caller into thinking it succeeded. 2. The client was never started before PairPhone was called. WhatsApp requires the client to be connected to the WA websocket (waiting for auth) before a pairing code can be generated. Fix: - Start the instance automatically if no active connection exists, mirroring the QR code flow - Wait 3 seconds for the WA websocket connection to establish and the initial QR generation to begin (required by whatsmeow before PairPhone can be called) - Reject early if the instance is already authenticated - Return PairPhone errors to the caller instead of swallowing them, so the handler correctly responds with HTTP 500 and an actionable error message Fixes #21
1 parent afbdef4 commit d1ff63e

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

pkg/instance/service/instance_service.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,35 @@ func (i instances) GetQr(instance *instance_model.Instance) (*QrcodeStruct, erro
471471
}
472472

473473
func (i instances) Pair(data *PairStruct, instance *instance_model.Instance) (*PairReturnStruct, error) {
474-
code, err := i.clientPointer[instance.Id].PairPhone(context.Background(), data.Phone, true, whatsmeow.PairClientChrome, "Chrome (Linux)")
474+
logger := i.loggerWrapper.GetLogger(instance.Id)
475+
client := i.clientPointer[instance.Id]
476+
477+
if client == nil || !client.IsConnected() {
478+
if client != nil && client.IsLoggedIn() {
479+
return nil, fmt.Errorf("instance is already authenticated")
480+
}
481+
logger.LogInfo("[%s] No active connection, starting instance for phone pairing", instance.Id)
482+
if err := i.whatsmeowService.StartInstance(instance.Id); err != nil {
483+
logger.LogError("[%s] Failed to start instance for pairing: %v", instance.Id, err)
484+
return nil, fmt.Errorf("failed to start instance: %w", err)
485+
}
486+
// Wait for the WA websocket connection and initial QR generation to establish.
487+
// PairPhone must be called after the QR event is received per whatsmeow docs.
488+
time.Sleep(3 * time.Second)
489+
client = i.clientPointer[instance.Id]
490+
if client == nil {
491+
return nil, fmt.Errorf("failed to initialize client for pairing")
492+
}
493+
}
494+
495+
if client.IsLoggedIn() {
496+
return nil, fmt.Errorf("instance is already authenticated")
497+
}
498+
499+
code, err := client.PairPhone(context.Background(), data.Phone, true, whatsmeow.PairClientChrome, "Chrome (Linux)")
475500
if err != nil {
476-
i.loggerWrapper.GetLogger(instance.Id).LogError("[%s] something went wrong calling pair phone", instance.Id)
501+
logger.LogError("[%s] PairPhone failed: %v", instance.Id, err)
502+
return nil, fmt.Errorf("pairing failed: %w", err)
477503
}
478504

479505
return &PairReturnStruct{PairingCode: code}, nil

0 commit comments

Comments
 (0)