Published
Edited
Jul 27, 2022
11 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof TWILIO_AUTH_TOKEN = Inputs.password()
Insert cell
Insert cell
Insert cell
sendMessageResponse = sendMessage !== 0
? fetch(
`https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json`,
{
method: "POST",
body: new URLSearchParams(
Object.entries({
From: "whatsapp:+14155238886",
To: "whatsapp:+4915114338654",
Body: "new image",
MediaUrl: pngLink.href //"https://webcode.run/observablehq.com/d/4e796c3f3029f09e;svg"
})
).toString(),
headers: {
"content-type": "application/x-www-form-urlencoded",
authorization: `Basic ${btoa(
TWILIO_ACCOUNT_SID + ":" + TWILIO_AUTH_TOKEN
)}`
}
}
)
: invalidation
Insert cell
Insert cell
Insert cell
dashboardEndpoint = endpoint("svg", async (req, res) => {
res.header("content-type", "image/svg+xml");
const arrayBuffer = await serialize(dashboard).arrayBuffer();
res.send(arrayBuffer);
})
Insert cell
Insert cell
Insert cell
Insert cell
<img src="${dashboardEndpoint.href}">
Insert cell
Insert cell
Insert cell
Insert cell
whatsapp_webhook = endpoint("whatsapp_webhook", async (req, res, ctx) => {
try {
const response = await viewof whatsappEvent.send({
req,
res,
ctx
});
res.send(response);
} catch (err) {
res.status(500).send(err.message);
}
})
Insert cell
Insert cell
Insert cell
viewof whatsappEvent = flowQueue()
Insert cell
whatsappEvent
Insert cell
// Incoming requests are URL form encoded, so we have to decode them
whatsappIncoming = Object.fromEntries(
new URLSearchParams(whatsappEvent.req.body).entries()
)
Insert cell
msg = whatsappIncoming.Body // The body contains the chat message
Insert cell
/* based on the incoming message we make a decision what to send */
chatResponse = {
const token = // Get the token from secrets if not in the notebook
TWILIO_AUTH_TOKEN || whatsappEvent.ctx.secrets["TWILIO_AUTH_TOKEN"];

if (msg === "chart") {
fetch(
`https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json`,
{
method: "POST",
body: new URLSearchParams(
Object.entries({
From: "whatsapp:+14155238886",
To: whatsappIncoming.From,
Body: "Your chart:",
MediaUrl: pngLink.href
})
).toString(),
headers: {
"content-type": "application/x-www-form-urlencoded",
authorization: `Basic ${btoa(TWILIO_ACCOUNT_SID + ":" + token)}`
}
}
);
return "Sending chart";
} else {
fetch(
`https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json`,
{
method: "POST",
body: new URLSearchParams(
Object.entries({
From: "whatsapp:+14155238886",
To: whatsappIncoming.From,
Body: `The sensor's last payload is ${JSON.stringify(
latest["dev:864475046457429"].body
)}`
})
).toString(),
headers: {
"content-type": "application/x-www-form-urlencoded",
authorization: `Basic ${btoa(TWILIO_ACCOUNT_SID + ":" + token)}`
}
}
);
return "Sending latest payload";
}
}
Insert cell
whatsappEventResolver = {
chatResponse;
viewof whatsappEvent.resolve("OK");
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more