The best way to control Claude Code, Codex and more from any computer or phone

main
ts 274 lines 10.8 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as SocialMessaging from "@alchemy.run/aws/SocialMessaging";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
10
11 // Resolved at deploy time (the test process exports it before deploying).
12 // At Lambda runtime the module re-evaluates with the fallback, which is
13 // fine — the bound resource's attributes resolve from deployed state, not
14 // from these props.
15 const WABA_ID = process.env.AWS_TEST_SOCIALMESSAGING_WABA_ID ?? "waba-unset";
16
17 // Well-formed but nonexistent identifiers for typed-error probe routes.
18 const BOGUS_PHONE_ID = "phone-number-id-0123456789abcdef0123456789abcdef";
19 const BOGUS_MEDIA_ID = "alchemy-nonexistent-media-id";
20
21 export class SocialMessagingBindingsFunction extends Lambda.Function<Lambda.Function>()(
22 "SocialMessagingBindingsFunction",
23 ) {}
24
25 export default SocialMessagingBindingsFunction.make(
26 {
27 main,
28 functionUrl: true,
29 },
30 Effect.gen(function* () {
31 // Adopts the console-linked WABA (see LinkedWhatsAppBusinessAccount
32 // docs) — stack.destroy() disassociates it, requiring console re-signup.
33 const account = yield* SocialMessaging.LinkedWhatsAppBusinessAccount("BindingsWaba", {
34 accountId: WABA_ID,
35 });
36
37 const sendMessage = yield* SocialMessaging.SendWhatsAppMessage(account);
38 const postMedia = yield* SocialMessaging.PostWhatsAppMessageMedia(account);
39 const getMedia = yield* SocialMessaging.GetWhatsAppMessageMedia(account);
40 const deleteMedia = yield* SocialMessaging.DeleteWhatsAppMessageMedia(account);
41 const getPhoneNumber =
42 yield* SocialMessaging.GetLinkedWhatsAppBusinessAccountPhoneNumber(account);
43 const createTemplate = yield* SocialMessaging.CreateWhatsAppMessageTemplate(account);
44 const createFromLibrary =
45 yield* SocialMessaging.CreateWhatsAppMessageTemplateFromLibrary(account);
46 const uploadTemplateMedia = yield* SocialMessaging.CreateWhatsAppMessageTemplateMedia(account);
47 const getTemplate = yield* SocialMessaging.GetWhatsAppMessageTemplate(account);
48 const listTemplates = yield* SocialMessaging.ListWhatsAppMessageTemplates(account);
49 const browseLibrary = yield* SocialMessaging.ListWhatsAppTemplateLibrary(account);
50 const updateTemplate = yield* SocialMessaging.UpdateWhatsAppMessageTemplate(account);
51 const deleteTemplate = yield* SocialMessaging.DeleteWhatsAppMessageTemplate(account);
52 const createFlow = yield* SocialMessaging.CreateWhatsAppFlow(account);
53 const getFlow = yield* SocialMessaging.GetWhatsAppFlow(account);
54 const getFlowPreview = yield* SocialMessaging.GetWhatsAppFlowPreview(account);
55 const listFlows = yield* SocialMessaging.ListWhatsAppFlows(account);
56 const listFlowAssets = yield* SocialMessaging.ListWhatsAppFlowAssets(account);
57 const updateFlow = yield* SocialMessaging.UpdateWhatsAppFlow(account);
58 const updateFlowAssets = yield* SocialMessaging.UpdateWhatsAppFlowAssets(account);
59 const publishFlow = yield* SocialMessaging.PublishWhatsAppFlow(account);
60 const deprecateFlow = yield* SocialMessaging.DeprecateWhatsAppFlow(account);
61 const deleteFlow = yield* SocialMessaging.DeleteWhatsAppFlow(account);
62
63 const bound = {
64 sendMessage,
65 postMedia,
66 getMedia,
67 deleteMedia,
68 getPhoneNumber,
69 createTemplate,
70 createFromLibrary,
71 uploadTemplateMedia,
72 getTemplate,
73 listTemplates,
74 browseLibrary,
75 updateTemplate,
76 deleteTemplate,
77 createFlow,
78 getFlow,
79 getFlowPreview,
80 listFlows,
81 listFlowAssets,
82 updateFlow,
83 updateFlowAssets,
84 publishFlow,
85 deprecateFlow,
86 deleteFlow,
87 };
88
89 return {
90 fetch: Effect.gen(function* () {
91 const request = yield* HttpServerRequest;
92 const url = new URL(request.originalUrl);
93 const pathname = url.pathname;
94
95 if (request.method === "GET" && pathname === "/bindings") {
96 return yield* HttpServerResponse.json({
97 bound: Object.keys(bound),
98 });
99 }
100
101 // Read-only round-trips against the linked account — prove the
102 // WABA-scoped grant + id injection end-to-end.
103 if (request.method === "GET" && pathname === "/templates") {
104 const response = yield* listTemplates({ maxResults: 25 });
105 return yield* HttpServerResponse.json({
106 count: (response.templates ?? []).length,
107 });
108 }
109
110 if (request.method === "GET" && pathname === "/flows") {
111 const response = yield* listFlows({ maxResults: 25 });
112 return yield* HttpServerResponse.json({
113 count: (response.flows ?? []).length,
114 });
115 }
116
117 if (request.method === "GET" && pathname === "/library") {
118 const response = yield* browseLibrary({ maxResults: 5 });
119 return yield* HttpServerResponse.json({
120 count: (response.metaLibraryTemplates ?? []).length,
121 });
122 }
123
124 // Typed-error probes: a bogus identifier round-trips the typed tag,
125 // proving the grant reached the API (an IAM gap would surface
126 // AccessDeniedException instead).
127 if (request.method === "GET" && pathname === "/phone/typed-not-found") {
128 const typed = yield* getPhoneNumber({ id: BOGUS_PHONE_ID }).pipe(
129 Effect.map(() => false),
130 Effect.catchTag(["ResourceNotFoundException", "InvalidParametersException"], () =>
131 Effect.succeed(true),
132 ),
133 );
134 return yield* HttpServerResponse.json({ typed });
135 }
136
137 if (request.method === "GET" && pathname === "/template/typed-not-found") {
138 const typed = yield* getTemplate({ metaTemplateId: "0" }).pipe(
139 Effect.map(() => false),
140 Effect.catchTag(
141 [
142 "ResourceNotFoundException",
143 "InvalidParametersException",
144 "AccessDeniedByMetaException",
145 "DependencyException",
146 "ValidationException",
147 ],
148 () => Effect.succeed(true),
149 ),
150 );
151 return yield* HttpServerResponse.json({ typed });
152 }
153
154 if (request.method === "GET" && pathname === "/flow/typed-not-found") {
155 const typed = yield* getFlow({ flowId: "0" }).pipe(
156 Effect.map(() => false),
157 Effect.catchTag(
158 [
159 "ResourceNotFoundException",
160 "InvalidParametersException",
161 "AccessDeniedByMetaException",
162 "DependencyException",
163 "ValidationException",
164 ],
165 () => Effect.succeed(true),
166 ),
167 );
168 return yield* HttpServerResponse.json({ typed });
169 }
170
171 if (request.method === "POST" && pathname === "/media/typed-not-found") {
172 const get = yield* getMedia({
173 mediaId: BOGUS_MEDIA_ID,
174 originationPhoneNumberId: BOGUS_PHONE_ID,
175 metadataOnly: true,
176 }).pipe(
177 Effect.map(() => false),
178 Effect.catchTag(
179 [
180 "ResourceNotFoundException",
181 "InvalidParametersException",
182 "AccessDeniedByMetaException",
183 "DependencyException",
184 "ValidationException",
185 ],
186 () => Effect.succeed(true),
187 ),
188 );
189 const del = yield* deleteMedia({
190 mediaId: BOGUS_MEDIA_ID,
191 originationPhoneNumberId: BOGUS_PHONE_ID,
192 }).pipe(
193 Effect.map(() => false),
194 Effect.catchTag(
195 [
196 "ResourceNotFoundException",
197 "InvalidParametersException",
198 "AccessDeniedByMetaException",
199 "DependencyException",
200 "ValidationException",
201 ],
202 () => Effect.succeed(true),
203 ),
204 );
205 return yield* HttpServerResponse.json({ get, del });
206 }
207
208 if (request.method === "POST" && pathname === "/send/typed-invalid") {
209 // A bogus phone-number id is rejected with a typed error before
210 // any message is sent — proves the SendWhatsAppMessage grant
211 // without messaging a real user.
212 const typed = yield* sendMessage({
213 originationPhoneNumberId: BOGUS_PHONE_ID,
214 metaApiVersion: "v20.0",
215 message: new TextEncoder().encode(
216 JSON.stringify({
217 messaging_product: "whatsapp",
218 to: "+10000000000",
219 type: "text",
220 text: { body: "alchemy-socialmessaging-probe" },
221 }),
222 ),
223 }).pipe(
224 Effect.map(() => false),
225 Effect.catchTag(
226 [
227 "ResourceNotFoundException",
228 "InvalidParametersException",
229 "AccessDeniedByMetaException",
230 "DependencyException",
231 "ValidationException",
232 ],
233 () => Effect.succeed(true),
234 ),
235 );
236 return yield* HttpServerResponse.json({ typed });
237 }
238
239 return yield* HttpServerResponse.json(
240 { error: "Not found", method: request.method, pathname },
241 { status: 404 },
242 );
243 }).pipe(Effect.orDie),
244 };
245 }).pipe(
246 Effect.provide(
247 Layer.mergeAll(
248 SocialMessaging.SendWhatsAppMessageHttp,
249 SocialMessaging.PostWhatsAppMessageMediaHttp,
250 SocialMessaging.GetWhatsAppMessageMediaHttp,
251 SocialMessaging.DeleteWhatsAppMessageMediaHttp,
252 SocialMessaging.GetLinkedWhatsAppBusinessAccountPhoneNumberHttp,
253 SocialMessaging.CreateWhatsAppMessageTemplateHttp,
254 SocialMessaging.CreateWhatsAppMessageTemplateFromLibraryHttp,
255 SocialMessaging.CreateWhatsAppMessageTemplateMediaHttp,
256 SocialMessaging.GetWhatsAppMessageTemplateHttp,
257 SocialMessaging.ListWhatsAppMessageTemplatesHttp,
258 SocialMessaging.ListWhatsAppTemplateLibraryHttp,
259 SocialMessaging.UpdateWhatsAppMessageTemplateHttp,
260 SocialMessaging.DeleteWhatsAppMessageTemplateHttp,
261 SocialMessaging.CreateWhatsAppFlowHttp,
262 SocialMessaging.GetWhatsAppFlowHttp,
263 SocialMessaging.GetWhatsAppFlowPreviewHttp,
264 SocialMessaging.ListWhatsAppFlowsHttp,
265 SocialMessaging.ListWhatsAppFlowAssetsHttp,
266 SocialMessaging.UpdateWhatsAppFlowHttp,
267 SocialMessaging.UpdateWhatsAppFlowAssetsHttp,
268 SocialMessaging.PublishWhatsAppFlowHttp,
269 SocialMessaging.DeprecateWhatsAppFlowHttp,
270 SocialMessaging.DeleteWhatsAppFlowHttp,
271 ),
272 ),
273 ),
274 );