feat(api): add phone number registration endpoint and update related logic
Some checks failed
CI / Test (1.23) (push) Failing after -21m47s
CI / Test (1.22) (push) Failing after -21m38s
CI / Lint (push) Failing after -21m58s
CI / Build (push) Failing after -22m23s

This commit is contained in:
2026-02-21 00:04:16 +02:00
parent 8732b332a1
commit 4a716bb82d
10 changed files with 275 additions and 43 deletions

View File

@@ -226,27 +226,37 @@ func (c *Client) MarkAsRead(ctx context.Context, messageID string) error {
return nil
}
// ListPhoneNumbers returns all phone numbers for the business account.
// ListPhoneNumbers returns all phone numbers for the WhatsApp Business Account.
// The WABA ID is resolved from the phone number at connect time; it is distinct from
// the Facebook Business Manager ID stored in business_account_id.
func (c *Client) ListPhoneNumbers(ctx context.Context) (*PhoneNumberListResponse, error) {
if c.config.BusinessAccountID == "" {
return nil, errNoBusinessAccount
wabaID := c.wabaID
if wabaID == "" {
// Fallback: resolve on demand if Connect() was not called
id, err := c.fetchWABAID(ctx)
if err != nil {
return nil, fmt.Errorf("could not resolve WABA ID: %w", err)
}
c.wabaID = id
wabaID = id
}
params := url.Values{
"fields": {"id,display_phone_number,phone_number,verified_name,code_verification_status,quality_rating,throughput"},
"fields": {"id,display_phone_number,verified_name,code_verification_status,quality_rating,throughput"},
}
var resp PhoneNumberListResponse
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/phone_numbers", params, &resp); err != nil {
if err := c.graphAPIGet(ctx, wabaID+"/phone_numbers", params, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// RequestVerificationCode sends a verification code (SMS or VOICE) to the given phone number.
func (c *Client) RequestVerificationCode(ctx context.Context, phoneNumberID string, method string) error {
func (c *Client) RequestVerificationCode(ctx context.Context, phoneNumberID string, method string, language string) error {
body := map[string]string{
"verification_method": method,
"code_method": method,
"language": language,
}
var resp RequestCodeResponse
@@ -261,6 +271,17 @@ func (c *Client) VerifyCode(ctx context.Context, phoneNumberID string, code stri
return c.graphAPIPost(ctx, phoneNumberID+"/verify_code", body, &resp)
}
// RegisterPhoneNumber registers a phone number for use with the WhatsApp Cloud API.
// POST /{phone-number-id}/register
func (c *Client) RegisterPhoneNumber(ctx context.Context, phoneNumberID string, pin string) error {
body := RegisterPhoneNumberRequest{
MessagingProduct: "whatsapp",
Pin: pin,
}
var resp RegisterPhoneNumberResponse
return c.graphAPIPost(ctx, phoneNumberID+"/register", body, &resp)
}
// DeleteMedia deletes a previously uploaded media file.
func (c *Client) DeleteMedia(ctx context.Context, mediaID string) error {
return c.graphAPIDelete(ctx, mediaID, nil)