Files
supermarket/internal/api/explee/client.go
T
davidandClaude Opus 5.5 b025a97e4a Phase 2 part 3: buying signals, the funnel and the scorecard
Schema 7 adds signals (hypothesis, how to spot it, angle, status, utm
slug), funnel_events (stage, channel, subject, campaign, source, deduped
by external ID) and outside_campaigns. Prospects -> Signals is the
scorecard: each signal's funnel from found to paid, people once per
stage, with revenue and outside spend, by period and channel. Each
signal's page shows its funnel by channel, its campaigns and its latest
steps. Prospects carry a signal (filter, change, "found" recorded) and
outcomes can be recorded by hand.

Settings -> Lead sources holds origami.chat and explee.com API keys. Every
6 hours (or Import now) Supermarket reads their campaigns, numbers and
spend, contacted people, replies and hot leads, read-only: interested
people and repliers become prospects, unsubscribes are suppressed, and
each campaign is assigned to a signal. The explee.com client follows its
OpenAPI schema; origami.chat's responses are undocumented, so its client
reads fields loosely and its Test shows the real response.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:06:58 -07:00

206 lines
6.3 KiB
Go

// Package explee reads outreach campaigns and their results from
// explee.com's public API (https://api.explee.com/public/api/docs). It only
// reads: Supermarket never starts, changes or sends anything there.
package explee
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const apiBase = "https://api.explee.com/public/api/v1"
// ErrUnauthorized means the key was refused.
var ErrUnauthorized = errors.New("explee.com refused the API key")
// Client calls the API with one key.
type Client struct {
key, base string
http *http.Client
}
func New(key string) *Client {
return &Client{key: key, base: apiBase, http: &http.Client{Timeout: 60 * time.Second}}
}
// WithBase points the client at another server (for tests).
func (c *Client) WithBase(u string) *Client {
c.base = strings.TrimRight(u, "/")
return c
}
func (c *Client) get(ctx context.Context, path string, q url.Values, out any) error {
u := c.base + path
if len(q) > 0 {
u += "?" + q.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return err
}
req.Header.Set("X-API-Key", c.key)
req.Header.Set("Accept", "application/json")
res, err := c.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
if err != nil {
return err
}
if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
return ErrUnauthorized
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("explee.com %s: HTTP %d: %s", path, res.StatusCode, strings.TrimSpace(string(body[:min(len(body), 300)])))
}
return json.Unmarshal(body, out)
}
// Me checks the key; it returns the key owner's email or name.
func (c *Client) Me(ctx context.Context) (string, error) {
var out map[string]any
if err := c.get(ctx, "/me", nil, &out); err != nil {
return "", err
}
for _, k := range []string{"email", "name", "organization_name"} {
if v, ok := out[k].(string); ok && v != "" {
return v, nil
}
}
return "the key works", nil
}
// Campaign is an outreach campaign.
type Campaign struct {
ID int64 `json:"id"`
ProjectID int64 `json:"project_id"`
Name string `json:"name"`
TargetURL string `json:"target_url"` // the booking link
Status string `json:"status"`
}
// Campaigns lists every campaign, archived ones too.
func (c *Client) Campaigns(ctx context.Context) ([]Campaign, error) {
var out struct {
Campaigns []Campaign `json:"campaigns"`
}
err := c.get(ctx, "/autogtm/campaigns", url.Values{"include_archived": {"true"}}, &out)
return out.Campaigns, err
}
// Analytics are a campaign's lifetime numbers.
type Analytics struct {
Status string `json:"status"`
EmailsSent int `json:"emails_sent"`
TotalReplies int `json:"total_replies"`
ReplyRatePct float64 `json:"reply_rate_pct"`
HotLeads int `json:"hot_leads"`
SpendUSD float64 `json:"spend_usd"`
CostPerLeadUSD float64 `json:"cost_per_lead_usd"`
LeadsPoolUsed int `json:"leads_pool_used"`
ManualStatus map[string]int `json:"manual_status_counts"`
ColdLost int `json:"cold_lost"`
}
func (c *Client) Analytics(ctx context.Context, campaignID int64) (*Analytics, error) {
var out Analytics
err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/analytics", url.Values{"period": {"all"}}, &out)
return &out, err
}
// Lead is a contacted lead.
type Lead struct {
PersonID string `json:"person_id"`
CampaignID int64 `json:"campaign_id"`
Erased bool `json:"erased"` // they asked to be removed: no personal data
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Name string `json:"name"`
Email string `json:"email"`
JobTitle string `json:"job_title"`
LinkedInURL string `json:"linkedin_url"`
CompanyName string `json:"company_name"`
CompanyDomain string `json:"company_domain"`
FirstSentAt string `json:"first_sent_at"`
Status string `json:"status"` // set by the team in the inbox, e.g. Hot, Won
Replied bool `json:"replied"`
Bounced bool `json:"bounced"`
Unsubscribed bool `json:"unsubscribed"`
LastReplyAt string `json:"last_reply_at"`
}
// Leads lists every contacted lead of a campaign.
func (c *Client) Leads(ctx context.Context, campaignID int64) ([]Lead, error) {
var all []Lead
offset := 0
for page := 0; page < 200; page++ {
var out struct {
Leads []Lead `json:"leads"`
HasMore bool `json:"has_more"`
NextOffset *int `json:"next_offset"`
}
q := url.Values{"limit": {"1000"}, "offset": {strconv.Itoa(offset)}}
if err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/leads", q, &out); err != nil {
return all, err
}
all = append(all, out.Leads...)
if !out.HasMore || out.NextOffset == nil {
break
}
offset = *out.NextOffset
}
return all, nil
}
// HotLead is a lead whose reply showed interest.
type HotLead struct {
PersonID string `json:"person_id"`
CampaignID int64 `json:"campaign_id"`
Erased bool `json:"erased"`
Name string `json:"name"`
Email string `json:"email"`
JobTitle string `json:"job_title"`
CompanyName string `json:"company_name"`
CompanyDomain string `json:"company_domain"`
LinkedInURL string `json:"linkedin_url"`
Phone string `json:"phone"`
WhyHot string `json:"why_hot"`
BecameHotAt string `json:"became_hot_at"`
}
// HotLeads lists hot leads that became hot at or after since (zero: all).
func (c *Client) HotLeads(ctx context.Context, since time.Time) ([]HotLead, error) {
var all []HotLead
offset := 0
for page := 0; page < 200; page++ {
var out struct {
Leads []HotLead `json:"leads"`
HasMore bool `json:"has_more"`
NextOffset *int `json:"next_offset"`
}
q := url.Values{"limit": {"500"}, "offset": {strconv.Itoa(offset)}}
if !since.IsZero() {
q.Set("since", since.UTC().Format(time.RFC3339))
}
if err := c.get(ctx, "/autogtm/hot-leads", q, &out); err != nil {
return all, err
}
all = append(all, out.Leads...)
if !out.HasMore || out.NextOffset == nil {
break
}
offset = *out.NextOffset
}
return all, nil
}