Hi, I’m trying to send a WhatsApp Business Cloud message in code, and would appreciate some guidance.
I found documentation for NodeJS
But I have no idea how to put it in pipedream.
If anyone can help me I would be very grateful.
This is what I got so far:
I’m probably missing a command, and other things
import { axios } from “@pipedream/platform”
export default defineComponent({
props: {
whatsapp_business: {
type: “app”,
app: “whatsapp_business”,
}
},
async run({steps, $}) {
return await axios($, {
url: https://graph.facebook.com/v15.0/${this.whatsapp_business.$auth.business_account_id}/phone_numbers
,
headers: {
Authorization: Bearer ${this.whatsapp_business.$auth.permanent_access_token}
,
},
})
},
“messaging_product”: “whatsapp”,
“recipient_type”: “individual”,
“to”: “972547351235”,
“type”: “text”,
“text”: { // the text object
“preview_url”: false,
“body”: “Hello, I am looking for a nanny in Jerusalem for a child”}
})
Hello @rrbbtalnorth,
Pipedream currently offers 3 actions for sending message on Whatsapp, could you try to see they fit your need?
I know that, how to write the code I don’t know.
Hello @rrbbtalnorth,
All of those actions are open sourced on Github. For example, you can find the source for WhatsApp business - Send Text Message here: pipedream/send-text-message.mjs at e38a1a6ed1e18381ce947d26dcd33a01365ff6b6 · PipedreamHQ/pipedream · GitHub
May I ask why do you need to write your own code instead of using the pre-built actions? Is the pre-built actions missing something?
The truth is, I tried to follow the pipedream documentation, but it doesn’t seem to be working documentation. I don’t see the appropriate parameters there according to WhatsApp’s instructions
There are many more options for sending a message that are missing from the pre-built actions. For example: add URL, headers, interactive messages, reply button and more.
And most importantly - I lack a router.
But first step I want to know how to write the code to send a simple text message, and in addition to that I will know how to add everything else.
If anyone has a simple code to send a WhatsApp message that works, I would be very happy if they would share it.
I found documentation that Moad can help with
1 Like
I wrote the code, it is correct, but not working?
I would appreciate it if someone could direct me what is wrong with the code
import { axios } from “@pipedream/platform”
export default defineComponent({
props: {
whatsapp_business: {
type: “app”,
app: “whatsapp_business”,
}
},
async run({steps, $}) {
return await axios($, {
url: https://graph.facebook.com/v15.0/${this.whatsapp_business.$auth.business_account_id}/phone_numbers
,
messaging_product: "whatsapp",
recipient_type: "individual",
to: "+972 54-735-1235",
type: "text",
text: {
preview_url: false,
body: " Hello how are you?",
},
headers: {
Authorization: `Bearer ${this.whatsapp_business.$auth.permanent_access_token}`,
},
})
},
})
Hello @rrbbtalnorth,
Maybe you’re missing the method on your axios
call? By default it is a GET request, so you might need to change it to the POST request and add the data
field for the body. Something like this:
import { axios } from "@pipedream/platform"
export default defineComponent({
props: {
whatsapp_business: {
type: "app",
app: "whatsapp_business",
}
},
async run({steps, $}) {
return await axios($, {
url: `https://graph.facebook.com/v15.0/${this.whatsapp_business.$auth.business_account_id}/phone_numbers`,
method: 'POST',
headers: {
Authorization: `Bearer ${this.whatsapp_business.$auth.permanent_access_token}`,
},
data: {
messaging_product: "whatsapp",
recipient_type: "individual",
to: waid,
type: "text",
text: {
preview_url: false,
body: reply,
},
}
})
},
})
I would suggest to read through the Pipedream’s axios to understand how it works. Also you can learn about javascript here: https://eloquentjavascript.net/
Hi, for those interested, I am attaching the two code blocks that give the solution to the problem that was here.
I was missing the “sender_phone_id” that I brought to the second block of the code, from the first block
The first code block-
import { axios } from “@pipedream/platform”
export default defineComponent({
props: {
whatsapp_business: {
type: “app”,
app: “whatsapp_business”,
}
},
async run({steps, $}) {
const result = await axios($, {
url: https://graph.facebook.com/v15.0/${this.whatsapp_business.$auth.business_account_id}/phone_numbers
,
headers: {
Authorization: Bearer ${this.whatsapp_business.$auth.permanent_access_token}
,
},
})
return result.data[0].id
},
})
The second code block-
import { axios } from “@pipedream/platform”
export default defineComponent({
props: {
whatsapp_business: { type: “app”, app: “whatsapp_business” },
sender_phone_id: { type: “string”, label: “Sender Phone ID” },
phone: { type: “string”, label: “phone” },
body: { type: “string”, label: “body” },
header: { type: “string”, label: “header” },
},
async run({steps, $}) {
return await axios($, {
method: “post”,
url: https://graph.facebook.com/v16.0/${this.sender_phone_id}/messages
,
headers: { Authorization: Bearer ${this.whatsapp_business.$auth.permanent_access_token}
},
data: {
messaging_product: "whatsapp",
recipient_type: "individual",
to: `${this.phone}`,
type: "text",
text: { preview_url: false, body: `${this.header}`}
},
})
}
})
1 Like