Skip to content

Commit

Permalink
whatsapp-api
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Feb 25, 2024
1 parent 40d7587 commit 64fa873
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import {
import { TaskHandler } from "./index.ts";
import { Endpoint } from "./taskBouncer.ts";

const sendMessage =
(accessToken: string, from: string, to: string) => (body: string) =>
export const sendWhatsappMessage =
(accessToken: string, fromNumberId: string) =>
(to: string) =>
(body: string) =>
fetch(
`https://graph.facebook.com/v12.0/${from}/messages?access_token=${accessToken}`,
`https://graph.facebook.com/v19.0/${fromNumberId}/messages`,
{
method: "POST",
body: JSON.stringify({
recipient_type: "individual",
type: "text",
messaging_product: "whatsapp",
to,
text: { body },
text: { preview_url: false, body },
}),
headers: { "Content-Type": "application/json" },
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
},
).then(() => {});
).then(async (response) => {
if (!response.ok) throw new Error(await response.text());
});

type TextMessage = {
id: string;
Expand Down Expand Up @@ -124,11 +133,10 @@ export const whatsappBusinessHandler = (
handler: (msg: WhatsappMessage) =>
msg.entry[0].changes[0].value.messages
? letIn(
sendMessage(
sendWhatsappMessage(
accessToken,
toNumberId(msg),
coerce(fromNumber(msg)),
),
)(coerce(fromNumber(msg))),
(send) =>
pipe(
injectBotPhone(() => toNumber(msg))<TaskHandler>,
Expand Down

0 comments on commit 64fa873

Please sign in to comment.