1 | |
2 | type Buttondown = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create Automation |
7 | * |
8 | */ |
9 | export async function main( |
10 | auth: Buttondown, |
11 | body: { |
12 | name: string; |
13 | trigger: |
14 | | "subscriber.created" |
15 | | "subscriber.unsubscribed" |
16 | | "subscriber.changed_email" |
17 | | "subscriber.confirmed" |
18 | | "subscriber.trial_started" |
19 | | "subscriber.trial_ended" |
20 | | "subscriber.type.changed" |
21 | | "subscriber.tags.changed" |
22 | | "subscriber.clicked" |
23 | | "subscriber.opened" |
24 | | "subscriber.paid" |
25 | | "subscriber.churned" |
26 | | "subscriber.updated" |
27 | | "subscriber.deleted" |
28 | | "subscriber.viewed_checkout_page" |
29 | | "subscriber.replied" |
30 | | "subscriber.paused" |
31 | | "subscriber.resumed" |
32 | | "subscriber.responded_to_survey" |
33 | | "subscriber.referred" |
34 | | "subscriber.referred.paid" |
35 | | "subscriber.commented" |
36 | | "email.created" |
37 | | "email.sent" |
38 | | "email.updated" |
39 | | "email.deleted" |
40 | | "email.status.changed" |
41 | | "scheduled_email.converted" |
42 | | "mention.created" |
43 | | "advertising_slot.purchased" |
44 | | "social_mention.created" |
45 | | "export.created" |
46 | | "export.completed" |
47 | | "export.failed" |
48 | | "automation.invoked" |
49 | | "stripe.checkout.session.completed" |
50 | | "stripe.subscription.activated" |
51 | | "stripe.subscription.churning" |
52 | | "stripe.subscription.deactivated" |
53 | | "stripe.customer.updated" |
54 | | "memberful.subscription.created" |
55 | | "memberful.subscription.deleted" |
56 | | "memberful.member.updated"; |
57 | timing: { |
58 | time: "immediate" | "delay"; |
59 | delay?: { |
60 | value: string; |
61 | unit: "minutes" | "hours" | "days" | "weeks"; |
62 | time_of_day?: "" | "morning" | "evening"; |
63 | }; |
64 | }; |
65 | actions: {}[]; |
66 | filters: { |
67 | filters: { |
68 | field: string; |
69 | operator: |
70 | | "equals" |
71 | | "not_equals" |
72 | | "contains" |
73 | | "not_contains" |
74 | | "is_empty" |
75 | | "is_not_empty" |
76 | | "greater_than" |
77 | | "less_than"; |
78 | value: string; |
79 | }[]; |
80 | groups: {}[]; |
81 | predicate: "and" | "or"; |
82 | }; |
83 | metadata?: {}; |
84 | }, |
85 | ) { |
86 | const url = new URL(`https://api.buttondown.com/v1/automations`); |
87 |
|
88 | const response = await fetch(url, { |
89 | method: "POST", |
90 | headers: { |
91 | "Content-Type": "application/json", |
92 | Authorization: "Token " + auth.token, |
93 | }, |
94 | body: JSON.stringify(body), |
95 | }); |
96 | if (!response.ok) { |
97 | const text = await response.text(); |
98 | throw new Error(`${response.status} ${text}`); |
99 | } |
100 | return await response.json(); |
101 | } |
102 |
|