Documentation
¶
Overview ¶
Package meta reads Facebook and Instagram lead ads over Meta's Graph API (M66): the Pages a system user's token reaches and their own tokens, the app's and the Pages' leadgen webhook subscriptions, the Pages' lead forms with their questions and consent checkboxes, one lead by its id, and a form's leads since a moment (polling and importing past leads).
It reuses the WhatsApp Graph client (internal/whatsapp/meta) for the transport, its errors and the webhook's verification handshake and X-Hub-Signature-256. Every call made with a system user's or a Page's token carries appsecret_proof, so an app that requires it keeps working and a token on its own is useless to whoever reads it. It knows nothing about workspaces: internal/leads keeps the connection and its sealed credentials. metatest is an in-process Graph API for tests and the demo.
Index ¶
- Constants
- Variables
- func Expired(err error) bool
- func PermissionMissing(err error) bool
- func Proof(appSecret, token string) string
- func RateLimited(err error) bool
- func Sign(appSecret string, body []byte) string
- func SignatureShape(header string) bool
- func TokenRejected(err error) bool
- func Unavailable(err error) bool
- func VerifyChallenge(q url.Values, verifyToken string) (string, bool)
- func VerifySignature(appSecret string, body []byte, header string) bool
- type Answer
- type Checkbox
- type Client
- func (c *Client) App(ctx context.Context) (string, error)
- func (c *Client) Form(ctx context.Context, p Page, formID string) (Form, error)
- func (c *Client) FormLeads(ctx context.Context, p Page, formID string, since time.Time, after string) (LeadsPage, error)
- func (c *Client) Forms(ctx context.Context, p Page) ([]Form, error)
- func (c *Client) GetLead(ctx context.Context, p Page, leadgenID string) (Lead, error)
- func (c *Client) Me(ctx context.Context) (id, name string, err error)
- func (c *Client) Pages(ctx context.Context) ([]Page, error)
- func (c *Client) Subscribe(ctx context.Context, p Page) error
- func (c *Client) SubscribeApp(ctx context.Context, callbackURL, verifyToken string) error
- func (c *Client) Unsubscribe(ctx context.Context, p Page) error
- type Credentials
- type Error
- type Form
- type Lead
- type LeadChange
- type LeadsPage
- type Page
- type Question
Constants ¶
const ( DefaultBaseURL = wa.DefaultBaseURL DefaultVersion = wa.DefaultVersion )
DefaultBaseURL and DefaultVersion are the WhatsApp client's: one Graph API version for every Meta call ReputeCast makes.
Variables ¶
var ErrNotLeads = errors.New("meta: not a Page webhook")
ErrNotLeads is a delivery that isn't a Page's webhook.
var Permissions = []string{"leads_retrieval", "pages_show_list", "pages_read_engagement", "pages_manage_metadata", "ads_management"}
Permissions are what the system user's token needs, in the order the connect dialog lists them.
Functions ¶
func PermissionMissing ¶
PermissionMissing reports whether the token lacks a permission (10, 200 to 299) or lead access (Leads Access Manager answers with 100/33 or 200).
func Proof ¶
Proof is appsecret_proof for a token: the hex HMAC-SHA256 of the token with the app secret.
func RateLimited ¶
RateLimited reports whether Meta asks to slow down: the app, user, Page or lead ads rate limits.
func SignatureShape ¶
SignatureShape reports whether a header can be a signature at all.
func TokenRejected ¶
TokenRejected reports whether Meta refused the token: it expired, was revoked or lost a permission.
func Unavailable ¶
Unavailable reports a failure worth retrying: no answer or a 5xx.
func VerifyChallenge ¶
VerifyChallenge answers Meta's verification request (hub.challenge when hub.verify_token is ours, compared in constant time).
Types ¶
type Checkbox ¶
type Checkbox struct {
Key string `json:"key"`
Text string `json:"text"`
Required bool `json:"required"`
}
Checkbox is one of the custom disclaimer's checkboxes.
type Client ¶
type Client struct {
Creds Credentials
// HTTP is the SSRF-guarded client in production, the in-process Graph
// API in tests and the demo.
HTTP *http.Client
// BaseURL and Version default to the Graph API and DefaultVersion.
BaseURL string
Version string
}
Client calls the Graph API for one connection.
func (*Client) App ¶
App checks the app id and secret together: the app's name, read with the app's own token.
func (*Client) FormLeads ¶
func (c *Client) FormLeads(ctx context.Context, p Page, formID string, since time.Time, after string) (LeadsPage, error)
FormLeads lists a form's leads created after since, a page at a time.
func (*Client) SubscribeApp ¶
SubscribeApp subscribes the app to Pages' leadgen webhooks at callbackURL. Meta checks the address at once with a verification request carrying verifyToken, so the connection must already be stored.
type Credentials ¶
type Credentials struct {
AppID string `json:"appId"`
AppSecret string `json:"appSecret"`
// Token is a system user's token for the app (no expiry).
Token string `json:"token"`
}
Credentials are what connecting asks for, all write-only but the app id.
type Form ¶
type Form struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Locale string `json:"locale"`
// Questions and the custom disclaimer: its text and checkboxes.
Questions []Question `json:"questions"`
Disclaimer string `json:"disclaimer"`
Checkboxes []Checkbox `json:"checkboxes"`
}
Form is a lead form as Meta lists it.
type Lead ¶
type Lead struct {
ID string
FormID string
AdID string
CreatedTime time.Time
Fields []Answer
// Checked holds each custom disclaimer checkbox's answer by key.
Checked map[string]bool
}
Lead is a person's answers to a lead form.
type LeadChange ¶
type LeadChange struct {
LeadgenID string
PageID string
FormID string
AdID string
CreatedTime time.Time
}
LeadChange is one new lead a webhook delivery names: ids only, never answers.
func ParseWebhook ¶
func ParseWebhook(body []byte) ([]LeadChange, error)
ParseWebhook reads the leads a delivery names; changes to other fields are skipped.
type LeadsPage ¶
type LeadsPage struct {
Leads []Lead
// Next is the cursor of the next page, "" at the end.
Next string
}
LeadsPage is one page of a form's leads.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package metatest is an in-process Graph API for lead ads, with Meta's payloads: the Pages a system user's token reaches and their tokens, the app's and the Pages' webhook subscriptions, lead forms, leads and the webhook bodies Meta posts.
|
Package metatest is an in-process Graph API for lead ads, with Meta's payloads: the Pages a system user's token reaches and their tokens, the app's and the Pages' webhook subscriptions, lead forms, leads and the webhook bodies Meta posts. |