From 41f455d1e2085da1b39872a7af180d707a881bdf Mon Sep 17 00:00:00 2001 From: uri Date: Tue, 17 Dec 2024 22:33:02 +0200 Subject: [PATCH] support image param --- deno.lock | 22 ++++++++++++++++++++++ deps.ts | 2 +- src/whatsapp.ts | 37 +++++++++++++++++++++---------------- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/deno.lock b/deno.lock index 5317421..c638624 100644 --- a/deno.lock +++ b/deno.lock @@ -538,6 +538,28 @@ "https://deno.land/x/gamla@89.0.0/src/trace.ts": "cd693d510a9a1dd126f33ea48b02f30c37fec17addf0ca5ccadb0d007af583d8", "https://deno.land/x/gamla@89.0.0/src/tree.ts": "05efa1973a55acd34c0c8d55e2f21ca80cd918fc2912bec9648fbe5184386316", "https://deno.land/x/gamla@89.0.0/src/typing.ts": "1af38da721b537feb29ec9ddddd2a7597fd976dea0458944265be60b6eb30ab7", + "https://deno.land/x/gamla@96.0.0/src/array.ts": "d9320f644e23fdebfbe26659fe4b056183dca21835eff40faf700f3dce15cc87", + "https://deno.land/x/gamla@96.0.0/src/composition.ts": "d903c740130e4ade54bb71941b8b135c80c6322075353b02396889075beff383", + "https://deno.land/x/gamla@96.0.0/src/conditional.ts": "d584506010564eab38cb3ee7cf0522f039fc614dc9eac3c503c0c129e5a266df", + "https://deno.land/x/gamla@96.0.0/src/debug.ts": "40bc30343b9192acdb67954e209e03ab9859d8bd8fe14d9511c75539f9d2a234", + "https://deno.land/x/gamla@96.0.0/src/filter.ts": "049645fee6c4f02c827819e872ade85e523115cfe0acd591109ee3b50da20c0e", + "https://deno.land/x/gamla@96.0.0/src/index.ts": "acdf7c211111f2799e607236701e015ab03807206fbf764d7c7792437d83b053", + "https://deno.land/x/gamla@96.0.0/src/io.ts": "663415dab0303217632ac34e3fb5bf2d44dfebdaff0f2c985646a1a6df7e66ca", + "https://deno.land/x/gamla@96.0.0/src/juxt.ts": "901cd8194c65db2816eac98d7905768fb816ff9c06367b6e56fbb7167ad2e4e9", + "https://deno.land/x/gamla@96.0.0/src/lock.ts": "29c1433244dc36a248b47f48655bf4003fc7294a94ceb139c22d770f81406f39", + "https://deno.land/x/gamla@96.0.0/src/map.ts": "585c069a89648341cf8128a66c88dc8f4848cb6b67487bc700ea4cfa7625cb6c", + "https://deno.land/x/gamla@96.0.0/src/mapping.ts": "6cea1269e8ef5e3c68681629164cf54699742307ff3c27946fdf3ac0c1547f05", + "https://deno.land/x/gamla@96.0.0/src/math.ts": "db211fc77286e2f7b3d9da95ea7bc487cfd6115d877aa205406df2f179614377", + "https://deno.land/x/gamla@96.0.0/src/matrix.ts": "906f0f33ec754cec89e558b37e4673df3e2e5753a23cd093dfe27b04fd981396", + "https://deno.land/x/gamla@96.0.0/src/object.ts": "3be876a3f52e55904683c62679ab95ba381289e06d37f78d418eb335bc89da81", + "https://deno.land/x/gamla@96.0.0/src/operator.ts": "f76abc2018d19a0f70abcd2b4d6b4e493034e4a9a2b43c39e857e76c2264013c", + "https://deno.land/x/gamla@96.0.0/src/promise.ts": "88f7a43588fd19ae35a8f789fa81dfd70a89d8e67191a461d179794a5c0a72ea", + "https://deno.land/x/gamla@96.0.0/src/reduce.ts": "5b2e125ecf8e7fe99dc24b8120af0ec998e220a1df3e50fe2222f5e0d55434de", + "https://deno.land/x/gamla@96.0.0/src/string.ts": "d99f33c497bbf5a58dd7b14c6f171059acb01b9b88e3f8236948ab50c8ccb601", + "https://deno.land/x/gamla@96.0.0/src/time.ts": "f695cf98eeb5cc4d79836cfd72f031cff82073c5961868c5a4778576f93506fa", + "https://deno.land/x/gamla@96.0.0/src/trace.ts": "cd693d510a9a1dd126f33ea48b02f30c37fec17addf0ca5ccadb0d007af583d8", + "https://deno.land/x/gamla@96.0.0/src/tree.ts": "05efa1973a55acd34c0c8d55e2f21ca80cd918fc2912bec9648fbe5184386316", + "https://deno.land/x/gamla@96.0.0/src/typing.ts": "1af38da721b537feb29ec9ddddd2a7597fd976dea0458944265be60b6eb30ab7", "https://deno.land/x/grammy_types@v3.3.0/api.ts": "efc90a31eb6f59ae5e7a4cf5838f46529e2fa6fa7e97a51a82dbd28afad21592", "https://deno.land/x/grammy_types@v3.3.0/inline.ts": "b5669d79f8c0c6f7d6ca856d548c1ac7d490efd54ee785d18a7c4fc12abfd73b", "https://deno.land/x/grammy_types@v3.3.0/manage.ts": "e39ec87e74469f70f35aa51dc520b02136ea5e75f9d7a7e0e513846a00b63fd2", diff --git a/deps.ts b/deps.ts index a72eaaa..199525c 100644 --- a/deps.ts +++ b/deps.ts @@ -1,2 +1,2 @@ -export * as gamla from "https://deno.land/x/gamla@89.0.0/src/index.ts"; +export * as gamla from "https://deno.land/x/gamla@96.0.0/src/index.ts"; export * as grammy from "https://deno.land/x/grammy_types@v3.3.0/mod.ts"; diff --git a/src/whatsapp.ts b/src/whatsapp.ts index eaa89a6..c4bd5f7 100644 --- a/src/whatsapp.ts +++ b/src/whatsapp.ts @@ -64,20 +64,17 @@ export const sendWhatsappMessage = return (await response.json()) as SentMessageResponse; }).then(({ messages: [{ id }] }) => id)); -const textParams = (type: ParamType) => - pipe( - map(pipe(replace(/\n|\t|(\s\s\s\s)/g, " | "), convertToWhatsAppFormat)), - (texts: string[]) => ({ - type, - parameters: texts.map((text) => ({ - type: "text", - // Max length is 60, but it also includes the parameter name or something. - text: truncate(57)(text), - })), - }), - ); +const templateTextParamConstraints = pipe( + replace(/\n|\t|(\s\s\s\s)/g, " | "), + convertToWhatsAppFormat, + truncate(60), +); type ParamType = "HEADER" | "BODY" | "FOOTER" | "BUTTONS"; +type TemplateTextParam = { type: "text"; text: string }; +type TemplateImageParam = { type: "image"; image: { link: string } }; +type TemplateParam = TemplateTextParam | TemplateImageParam; +type Component = { type: ParamType; parameters: TemplateParam[] }; export const sendWhatsappTemplate = (accessToken: string, fromNumberId: string) => @@ -85,7 +82,7 @@ export const sendWhatsappTemplate = to: string, name: string, langCode: string, - params: Partial>, + components: Component[], ) => fetch(`https://graph.facebook.com/v20.0/${fromNumberId}/messages`, { method: "POST", @@ -97,9 +94,17 @@ export const sendWhatsappTemplate = template: { name, language: { code: langCode }, - components: Object.entries(params).map(( - x, - ) => textParams(x[0] as ParamType)(x[1])), + components: components.map((c) => ({ + ...c, + parameters: c.parameters.map((p) => + p.type === "text" + ? ({ + type: "text", + text: templateTextParamConstraints(p.text), + }) + : p + ), + })), }, }), headers: {