Microsoft Advanced Messaging for Javascript users

Taiwo Triumphant 0 Reputation points
2024-08-30T09:07:59.7633333+00:00

error: POST /api/comms/sendtemplate 500 8184.408 ms Message: Failed to send message User: Anonymous {"label":"Moilapp","timestamp":"2024-08-30T08:54:21.211Z"}

I have been getting this error when using the Azure Advanced Message Javascript SDK to implement sending a template message on Whatsapp. Please can you confirm this is a correct implementation for a binding

async function sendRecommendedJobMessage(channelRegistrationId, recipientList, name, jobType, position, positionType, company, compensation, rate) {
    // Assemble the template with the provided variables
    const bindings = {
        header: [
            { name: "jobHeader", value: "Job Recommendation for You" }
        ],
        body: [
            { name: "1", value: name },               // Pass the user's name
            { name: "2", value: jobType },            // Pass the job type
            { name: "3", value: position },           // Pass the position
            { name: "4", value: positionType },       // Pass the position type
            { name: "5", value: company },            // Pass the company name
            { name: "6", value: compensation },       // Pass the compensation
            { name: "7", value: rate }            // Pass the currency
        ],
        buttons: [
            {
                name: "Check Job",
                value: `https://cand.pollind.com/jobs/${name}/`, 
                type: "url"
            }
        ]
    };

	// Use the sendTemplateMessage function to send the message
    await sendTemplateMessage(channelRegistrationId, recipientList, "recommended_job_english", "en_us", bindings);
}


async function sendTemplateMessage(channelRegistrationId, recipientList, templateName, templateLanguage, bindings) {
    const sampleTemplate = {
        name: templateName,
        language: templateLanguage,
        bindings: bindings
    };

    const messageContent = {
        channelRegistrationId: channelRegistrationId,
        to: recipientList,
        kind: "template",
        template: sampleTemplate
    };

    console.log(messageContent);

    const templateMessageResult = await client.path("/messages/notifications:send").post({
        contentType: "application/json",
        body: { ...messageContent }
    });

    // Process result
    if (templateMessageResult.status === "202") {
        templateMessageResult.body.receipts.forEach((receipt) => {
            console.log("Message sent to:" + receipt.to + " with message id:" + receipt.messageId);
        });
    } else {
        throw new Error("Failed to send message");
    }
}
Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
845 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.