admin管理员组文章数量:1415697
I checked the WhatsApp Business API documentation and couldn’t find a direct way to start a conversation without the customer initiating it.
Currently, I can send messages only after receiving a message from a customer:
const axios = require('axios');
const sendMessage = async () => {
const response = await axios.post(
".0/YOUR_PHONE_NUMBER_ID/messages",
{
messaging_product: "whatsapp",
to: "CUSTOMER_PHONE_NUMBER",
type: "text",
text: { body: "Hello, how can we assist you?" }
},
{
headers: {
"Authorization": `Bearer YOUR_ACCESS_TOKEN`,
"Content-Type": "application/json"
}
}
);
console.log(response.data);
};
sendMessage();
This works only if the customer has sent a message first. How can I modify this to initiate a new conversation?
I checked the WhatsApp Business API documentation and couldn’t find a direct way to start a conversation without the customer initiating it.
Currently, I can send messages only after receiving a message from a customer:
const axios = require('axios');
const sendMessage = async () => {
const response = await axios.post(
"https://graph.facebook/v17.0/YOUR_PHONE_NUMBER_ID/messages",
{
messaging_product: "whatsapp",
to: "CUSTOMER_PHONE_NUMBER",
type: "text",
text: { body: "Hello, how can we assist you?" }
},
{
headers: {
"Authorization": `Bearer YOUR_ACCESS_TOKEN`,
"Content-Type": "application/json"
}
}
);
console.log(response.data);
};
sendMessage();
This works only if the customer has sent a message first. How can I modify this to initiate a new conversation?
Share Improve this question asked Feb 4 at 19:24 MaulanaMaulana 32 bronze badges1 Answer
Reset to default 0To start a conversation on WhatsApp using the Business API, you must use a pre-approved message template. WhatsApp enforces this to prevent unsolicited messages. Free-form messages only work if the user has messaged first within the last 24 hours.
If you want to initiate contact, update your request to send a template message instead of a free-form message. Here’s a correct implementation:
const axios = require('axios');
const sendTemplateMessage = async () => {
try {
const response = await axios.post(
"https://graph.facebook/v17.0/YOUR_PHONE_NUMBER_ID/messages",
{
messaging_product: "whatsapp",
to: "CUSTOMER_PHONE_NUMBER",
type: "template",
template: {
name: "hello_user", // Must be an approved template
language: { code: "en_US" },
components: [
{
type: "body",
parameters: [
{ type: "text", text: "John" } // Ensure this matches placeholders
]
}
]
}
},
{
headers: {
"Authorization": `Bearer YOUR_ACCESS_TOKEN`,
"Content-Type": "application/json"
}
}
);
console.log("Message sent successfully:", response.data);
} catch (error) {
if (error.response) {
console.error("API Error:", error.response.data);
} else {
console.error("Request Error:", error.message);
}
}
};
sendTemplateMessage();
Common Errors and Fixes
- (100) Invalid parameter: Incorrect template name --> Fix with use an approved template from WhatsApp Manager.
- (131051) Message failed to send: Phone number issue --> Fix: ensure the number has WhatsApp active and is formatted correctly.
- (200) Permission error: Missing permissions --> Fix: ensure the access token has message sending permissions.
If you want a simpler way to manage WhatsApp API without handling approvals and errors, consider Meta’s own WhatsApp Business Manager, Mekari Qontak, Twilio, or 360dialog. They streamline integration and compliance, reducing manual setup and troubleshooting.
本文标签:
版权声明:本文标题:chatbot - WhatsApp Business API requires that the user sends the first message before the business can reply - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745237296a2649112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论