How to send transactional Viber Business Message
Steps to Send a Transactional Message to Viber Users via Phone Number
Full Example Code to Send a Transactional Message
const axios = require('axios');
// Replace with your Viber Business API token
const VIBER_BUSINESS_API_TOKEN = 'YOUR_VIBER_BUSINESS_API_TOKEN';
// Function to send a transactional message via Viber using the recipient's phone number
const sendTransactionalMessage = async (phoneNumber, messageText) => {
try {
const messagePayload = {
"receiver": phoneNumber, // The recipient's phone number in international format (e.g., +65xxxxxxx)
"min_api_version": 1, // Minimum API version required for this message
"sender": {
"name": "YourBusinessName", // The sender's name as it will appear on the user's phone
"avatar": "https://example.com/avatar.jpg" // Optional: URL to the avatar image for the sender
},
"type": "text", // The type of message (in this case, it's a text message)
"text": messageText, // The transactional message content (e.g., order confirmation)
"tracking_data": "tracking_data_example", // Optional tracking data for your system
};
// Send POST request to Viber Business API endpoint to send the message
const response = await axios.post('https://chatapi.viber.com/pa/send_message', messagePayload, {
headers: {
'X-Viber-Auth-Token': VIBER_BUSINESS_API_TOKEN, // Viber Business API authentication token
}
});
// Log success response
console.log('Message sent successfully:', response.data);
} catch (error) {
// Log any errors encountered during the API request
console.error('Error sending message:', error.response ? error.response.data : error.message);
}
};
// Example usage:
// Replace with the recipient's actual Viber-registered phone number in international format
const recipientPhoneNumber = '+65xxxxxxxx'; // Example for Singapore (+65)
// Replace with your actual message content
const transactionalMessageText = 'Your order #12345 has been confirmed! Track it at https://tracking-link.com';
// Call the function to send the transactional message
sendTransactionalMessage(recipientPhoneNumber, transactionalMessageText);
Explanation of the Code:
Testing the Application:
Result:
Important Considerations:
Last updated