Parse response api to whatsapp
Does someone has some code to parse the response coming from typebot api to send it to other whatsapp number? After I make http posts frommybackend to typebot ai, I have seen nany text, url.... and childrens nested come from typebot api. It becomes hard capture all possibilities.
10 Replies
What you receive is a richText list
It can be transformed either manually (by parsing the nested text nodes recursively). Or using the Node.js
remark-slate
library.
Here is how it's transformed on Typebot for WhatsApp currently:
Hi @Baptiste I have adpated the code to my case. But it seems this function is not getting a tag as url. Fo sample, I have the next messages array attached. I am using the next code:
export const convertRichTextToWhatsAppText = (richText: TElement[]): string =>
richText
.map(chunk =>
serialize(chunk)?.replaceAll("*", "").replaceAll("'", "'")
)
.join("\n");
export const convertMessageToWhatsAppMessage = (
message: any
): string | undefined => {
switch (message.type) {
case "text": {
if (!message.content.richText message.content.richText.length === 0)
return undefined;
const txt = convertRichTextToWhatsAppText(message.content.richText);
console.log("txt", txt);
return txt;
}
case "image": {
if (!message.content.url isImageUrlNotCompatible(message.content.url))
return undefined;
return message.content.url;
}
case "audio": {
if (!message.content.url) return undefined;
return message.content.url;
}
case "video": {
if (
!message.content.url ||
(message.content.type !== "url" &&
isVideoUrlNotCompatible(message.content.url))
)
return undefined;
return message.content.url;
}
case "embed": {
if (!message.content.url) return undefined;
return message.content.url;
}
}
return undefined;
};
In my console.log I see only:
txt---->>>>> Clique aqui para baixar o documento
No url content is showing here
I can't format it because I put a console log in return of your function, but it´s never returned from it when the tag contains a url as type. For sample:
"children": [
{
"text": ""
},
{
"url": "https://Sistema.com.br/web.dll/AAA?Token=1002494XXX",
"type": "a",
"children": [
{
"text": "Clique aqui para baixar o documento"
}
]
},
{
"text": ""
}
]
}
The text is returned but not the url
Oh
Sorry I didn't get it, your message is not well formatted 😅
The message containing the staff to be parsed I am capturing from typebot api
I can pass the complete message to you. OK?
I have a node backend code that get the typebot http api response e format it to send to whatsapp. One moment, please and I will put here the complete messages.
No need
I can try it out on my end
Ok I can reproduce
Thank you.
Hi @Baptiste . Sorry, but this question was solved in the latest version?
Still in progress, this is tracked here: https://github.com/baptisteArno/typebot.io/issues/1056
GitHub
Message losing formatting on WhatsApp · Issue #1056 · baptisteArno...
Hello, I'm having problems receiving messages on WhatsApp, they're losing their formatting, the preview is ok. const ddds = [ "71", "73", "74", "75",...