More management tools
This commit is contained in:
@@ -12,8 +12,9 @@ import (
|
||||
|
||||
// ListCatalogs returns all product catalogs linked to the business account.
|
||||
func (c *Client) ListCatalogs(ctx context.Context) (*CatalogListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
@@ -21,7 +22,7 @@ func (c *Client) ListCatalogs(ctx context.Context) (*CatalogListResponse, error)
|
||||
}
|
||||
|
||||
var resp CatalogListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/catalogs", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/product_catalogs", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -19,6 +19,8 @@ import (
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
)
|
||||
|
||||
const defaultBusinessAPIMediaTimeout = 5 * time.Minute
|
||||
|
||||
// Client represents a WhatsApp Business API client
|
||||
type Client struct {
|
||||
id string
|
||||
@@ -61,7 +63,7 @@ func NewClient(cfg config.WhatsAppConfig, eventBus *events.EventBus, mediaConfig
|
||||
phoneNumber: cfg.PhoneNumber,
|
||||
config: *cfg.BusinessAPI,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
Timeout: defaultBusinessAPIMediaTimeout,
|
||||
},
|
||||
eventBus: eventBus,
|
||||
mediaConfig: mediaConfig,
|
||||
|
||||
@@ -7,8 +7,9 @@ import (
|
||||
|
||||
// ListFlows returns all flows for the business account.
|
||||
func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
@@ -16,7 +17,7 @@ func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
}
|
||||
|
||||
var resp FlowListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/flows", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/flows", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -24,12 +25,13 @@ func (c *Client) ListFlows(ctx context.Context) (*FlowListResponse, error) {
|
||||
|
||||
// CreateFlow creates a new flow and returns its ID.
|
||||
func (c *Client) CreateFlow(ctx context.Context, flow FlowCreateRequest) (*FlowCreateResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp FlowCreateResponse
|
||||
if err := c.graphAPIPost(ctx, c.config.BusinessAccountID+"/flows", flow, &resp); err != nil {
|
||||
if err := c.graphAPIPost(ctx, wabaID+"/flows", flow, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UploadMedia uploads a media file to Meta and returns the media ID.
|
||||
@@ -26,8 +28,16 @@ func (c *Client) uploadMedia(ctx context.Context, data []byte, mimeType string)
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
|
||||
// Add the file
|
||||
part, err := writer.CreateFormFile("file", "media")
|
||||
if strings.TrimSpace(mimeType) == "" {
|
||||
mimeType = "application/octet-stream"
|
||||
}
|
||||
|
||||
// Add the file with explicit MIME type so Meta does not treat it as octet-stream.
|
||||
fileHeader := make(textproto.MIMEHeader)
|
||||
fileHeader.Set("Content-Disposition", `form-data; name="file"; filename="media"`)
|
||||
fileHeader.Set("Content-Type", mimeType)
|
||||
|
||||
part, err := writer.CreatePart(fileHeader)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create form file: %w", err)
|
||||
}
|
||||
|
||||
@@ -2,22 +2,43 @@ package businessapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) resolveWABAID(ctx context.Context) (string, error) {
|
||||
if c.wabaID != "" {
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
if c.config.WABAId != "" {
|
||||
c.wabaID = c.config.WABAId
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
id, err := c.fetchWABAID(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not resolve WABA ID: %w", err)
|
||||
}
|
||||
|
||||
c.wabaID = id
|
||||
return c.wabaID, nil
|
||||
}
|
||||
|
||||
// ListTemplates returns all message templates for the business account.
|
||||
// Requires BusinessAccountID in the client config.
|
||||
// Uses the WhatsApp Business Account (WABA) ID.
|
||||
func (c *Client) ListTemplates(ctx context.Context) (*TemplateListResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
"fields": {"id,name,status,language,category,created_at,components,rejection_reasons,quality_score"},
|
||||
"fields": {"id,name,status,language,category,created_at,components,quality_score"},
|
||||
}
|
||||
|
||||
var resp TemplateListResponse
|
||||
if err := c.graphAPIGet(ctx, c.config.BusinessAccountID+"/message_templates", params, &resp); err != nil {
|
||||
if err := c.graphAPIGet(ctx, wabaID+"/message_templates", params, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -25,12 +46,13 @@ func (c *Client) ListTemplates(ctx context.Context) (*TemplateListResponse, erro
|
||||
|
||||
// UploadTemplate creates a new message template.
|
||||
func (c *Client) UploadTemplate(ctx context.Context, tmpl TemplateUploadRequest) (*TemplateUploadResponse, error) {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return nil, errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp TemplateUploadResponse
|
||||
if err := c.graphAPIPost(ctx, c.config.BusinessAccountID+"/message_templates", tmpl, &resp); err != nil {
|
||||
if err := c.graphAPIPost(ctx, wabaID+"/message_templates", tmpl, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
@@ -38,13 +60,14 @@ func (c *Client) UploadTemplate(ctx context.Context, tmpl TemplateUploadRequest)
|
||||
|
||||
// DeleteTemplate deletes a template by name and language.
|
||||
func (c *Client) DeleteTemplate(ctx context.Context, name, language string) error {
|
||||
if c.config.BusinessAccountID == "" {
|
||||
return errNoBusinessAccount
|
||||
wabaID, err := c.resolveWABAID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := url.Values{
|
||||
"name": {name},
|
||||
"language": {language},
|
||||
}
|
||||
return c.graphAPIDelete(ctx, c.config.BusinessAccountID+"/message_templates", params)
|
||||
return c.graphAPIDelete(ctx, wabaID+"/message_templates", params)
|
||||
}
|
||||
|
||||
@@ -622,7 +622,7 @@ type TemplateInfo struct {
|
||||
CreatedAt string `json:"created_at"`
|
||||
Components []TemplateComponentDef `json:"components"`
|
||||
RejectionReasons []string `json:"rejection_reasons,omitempty"`
|
||||
QualityScore string `json:"quality_score,omitempty"`
|
||||
QualityScore any `json:"quality_score,omitempty"`
|
||||
}
|
||||
|
||||
type TemplateComponentDef struct {
|
||||
|
||||
Reference in New Issue
Block a user