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>
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
// Package origami reads outreach campaigns and their results from
|
||||
// origami.chat's v3 API (https://docs.origami.chat). It only reads:
|
||||
// Supermarket never starts, changes or sends anything there.
|
||||
//
|
||||
// The v3 spec documents requests but not response bodies, so responses
|
||||
// are read as loose JSON and fields are looked up by the names the docs
|
||||
// use. Check them against a real account with Sample.
|
||||
package origami
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const apiBase = "https://origami.chat/api/v3"
|
||||
|
||||
// ErrUnauthorized means the key was refused.
|
||||
var ErrUnauthorized = errors.New("origami.chat refused the API key")
|
||||
|
||||
// Object is one JSON object from the API.
|
||||
type Object map[string]any
|
||||
|
||||
// Client calls the API with one key, optionally scoped to a project.
|
||||
type Client struct {
|
||||
key, project, base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(key, project string) *Client {
|
||||
return &Client{key: key, project: strings.TrimSpace(project), 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) (Object, 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 nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.key)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if c.project != "" {
|
||||
req.Header.Set("x-origami-project", c.project)
|
||||
}
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("origami.chat %s: HTTP %d: %s", path, res.StatusCode, strings.TrimSpace(string(body[:min(len(body), 300)])))
|
||||
}
|
||||
var out Object
|
||||
if err := json.Unmarshal(body, &out); err != nil {
|
||||
return nil, fmt.Errorf("origami.chat %s: %w", path, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// list pages through a list endpoint ({items, next_cursor}).
|
||||
func (c *Client) list(ctx context.Context, path string, q url.Values) ([]Object, error) {
|
||||
if q == nil {
|
||||
q = url.Values{}
|
||||
}
|
||||
q.Set("limit", "100")
|
||||
var all []Object
|
||||
for page := 0; page < 500; page++ {
|
||||
out, err := c.get(ctx, path, q)
|
||||
if err != nil {
|
||||
return all, err
|
||||
}
|
||||
items, _ := out["items"].([]any)
|
||||
for _, it := range items {
|
||||
if o, ok := it.(map[string]any); ok {
|
||||
all = append(all, o)
|
||||
}
|
||||
}
|
||||
next := out.Str("next_cursor")
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
q.Set("cursor", next)
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// Account checks the key; it returns the organization's name.
|
||||
func (c *Client) Account(ctx context.Context) (string, error) {
|
||||
out, err := c.get(ctx, "/account", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if n := out.Str("name", "org.name", "organization.name"); n != "" {
|
||||
return n, nil
|
||||
}
|
||||
return "the key works", nil
|
||||
}
|
||||
|
||||
// Campaigns lists every campaign.
|
||||
func (c *Client) Campaigns(ctx context.Context) ([]Object, error) {
|
||||
return c.list(ctx, "/send/campaigns", nil)
|
||||
}
|
||||
|
||||
// Stats are a campaign's counts and rates.
|
||||
func (c *Client) Stats(ctx context.Context, campaignID string) (Object, error) {
|
||||
return c.get(ctx, "/send/campaigns/"+url.PathEscape(campaignID)+"/stats", nil)
|
||||
}
|
||||
|
||||
// People lists a campaign's enrolled people.
|
||||
func (c *Client) People(ctx context.Context, campaignID string) ([]Object, error) {
|
||||
return c.list(ctx, "/send/campaigns/"+url.PathEscape(campaignID)+"/people", nil)
|
||||
}
|
||||
|
||||
// Replies lists replies from leads since a time (zero: all).
|
||||
func (c *Client) Replies(ctx context.Context, since time.Time) ([]Object, error) {
|
||||
q := url.Values{"from": {"lead"}}
|
||||
if !since.IsZero() {
|
||||
q.Set("since", since.UTC().Format(time.RFC3339))
|
||||
}
|
||||
return c.list(ctx, "/send/replies", q)
|
||||
}
|
||||
|
||||
// Sample returns the first campaign, its stats, one enrolled person and one
|
||||
// reply, as the API sent them, to check the field names against.
|
||||
func (c *Client) Sample(ctx context.Context) (map[string]any, error) {
|
||||
out := map[string]any{}
|
||||
camps, err := c.Campaigns(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out["campaigns"] = len(camps)
|
||||
if len(camps) > 0 {
|
||||
out["campaign"] = camps[0]
|
||||
id := camps[0].Str("id")
|
||||
if st, err := c.Stats(ctx, id); err == nil {
|
||||
out["stats"] = st
|
||||
} else {
|
||||
out["stats_error"] = err.Error()
|
||||
}
|
||||
if p, err := c.get(ctx, "/send/campaigns/"+url.PathEscape(id)+"/people", url.Values{"limit": {"1"}}); err == nil {
|
||||
out["people"] = p
|
||||
} else {
|
||||
out["people_error"] = err.Error()
|
||||
}
|
||||
}
|
||||
if r, err := c.get(ctx, "/send/replies", url.Values{"limit": {"1"}, "from": {"lead"}}); err == nil {
|
||||
out["replies"] = r
|
||||
} else {
|
||||
out["replies_error"] = err.Error()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Str is the first non-empty string (or number) among these fields; a
|
||||
// dotted name looks inside nested objects.
|
||||
func (o Object) Str(names ...string) string {
|
||||
for _, n := range names {
|
||||
var v any = map[string]any(o)
|
||||
for _, part := range strings.Split(n, ".") {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
v = nil
|
||||
break
|
||||
}
|
||||
v = m[part]
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
if s := strings.TrimSpace(x); s != "" {
|
||||
return s
|
||||
}
|
||||
case float64:
|
||||
return strings.TrimSuffix(fmt.Sprintf("%f", x), ".000000")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Num is the first numeric field among these names.
|
||||
func (o Object) Num(names ...string) (float64, bool) {
|
||||
for _, n := range names {
|
||||
var v any = map[string]any(o)
|
||||
for _, part := range strings.Split(n, ".") {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
v = nil
|
||||
break
|
||||
}
|
||||
v = m[part]
|
||||
}
|
||||
if f, ok := v.(float64); ok {
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Time is the first field among these names that parses as a time.
|
||||
func (o Object) Time(names ...string) time.Time {
|
||||
for _, n := range names {
|
||||
if t := ParseTime(o.Str(n)); !t.IsZero() {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// ParseTime reads the ISO 8601 forms these APIs send.
|
||||
func ParseTime(s string) time.Time {
|
||||
for _, f := range []string{time.RFC3339Nano, "2006-01-02T15:04:05", "2006-01-02 15:04:05", "2006-01-02"} {
|
||||
if t, err := time.Parse(f, s); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
Reference in New Issue
Block a user