You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/communication-services/quickstarts/advanced-messaging/whatsapp/includes/templates/messages-quickstart-template-messages-javascript.md
+204-1Lines changed: 204 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,208 @@ To set up an environment for sending messages, complete the steps in the followi
19
19
20
20
## Code examples
21
21
22
-
- TODO
22
+
Follow these steps to add required code snippets to the main function of your `App.java` file.
23
+
-[List WhatsApp templates in the Azure portal](#list-whatsapp-templates-in-the-azure-portal).
24
+
-[Send template message with text parameters in the body](#send-template-message-with-text-parameters-in-the-body).
25
+
26
+
### List WhatsApp templates in the Azure portal
27
+
28
+
To view your templates in the Azure portal, go to your Azure Communication Services resource > **Advanced Messaging** > **Templates**.
29
+
30
+
:::image type="content" source="../../media/template-messages/list-templates-azure-portal.png" lightbox="../../media/template-messages/list-templates-azure-portal.png" alt-text="Screenshot that shows an Azure Communication Services resource in the Azure portal, viewing the advanced messaging templates tab.":::
31
+
32
+
Selecting a template to view the template details.
33
+
34
+
The **Content** field of the template details can include parameter bindings. The parameter bindings can be denoted as:
35
+
- A `"format"` field with a value such as `IMAGE`.
36
+
- Double brackets surrounding a number, such as `{{1}}`. The number, index started at 1, indicates the order in which the binding values must be supplied to create the message template.
37
+
38
+
:::image type="content" source="../../media/template-messages/sample-movie-ticket-confirmation-azure-portal.png" lightbox="../../media/template-messages/sample-movie-ticket-confirmation-azure-portal.png" alt-text="Screenshot that shows template details.":::
39
+
40
+
Alternatively, you can view and edit all of your WhatsApp Business Account templates in the [WhatsApp Manager](https://business.facebook.com/wa/manage/home/) > Account tools > [Message templates](https://business.facebook.com/wa/manage/message-templates/).
41
+
42
+
To list out your templates programmatically, you can fetch all templates for your channel ID using the following code:
thrownewError("Failed to get template for the channel.");
70
+
}
71
+
72
+
// The paginate helper creates a paged async iterator using metadata from the first page.
73
+
constitems=paginate(client, response);
74
+
75
+
// We get an PageableAsyncIterator so we need to do `for await`.
76
+
forawait (constitemofitems) {
77
+
console.log(JSON.stringify(item, null, 2));
78
+
}
79
+
}
80
+
81
+
main().catch((error) => {
82
+
console.error("Encountered an error while sending message: ", error);
83
+
throw error;
84
+
});
85
+
```
86
+
87
+
### Send template message with text parameters in the body
88
+
89
+
To define parameters in the body denoted with double brackets surrounding a number, such as `{{1}}`. The number, index started at 1, indicates the order in which the binding values must be supplied to create the message template. Including parameters not in the template is invalid.
90
+
91
+
Template definition with two parameters:
92
+
```json
93
+
{
94
+
"type": "BODY",
95
+
"text": "Message with two parameters: {{1}} and {{2}}"
96
+
}
97
+
```
98
+
99
+
#### Examples
100
+
101
+
`sample_shipping_confirmation` template:
102
+
103
+
:::image type="content" source="../../media/template-messages/sample-shipping-confirmation-details-azure-portal.png" lightbox="../../media/template-messages/sample-shipping-confirmation-details-azure-portal.png" alt-text="Screenshot that shows template details for template named sample_shipping_confirmation.":::
104
+
105
+
In this sample, the body of the template has one parameter:
106
+
107
+
```json
108
+
{
109
+
"type": "BODY",
110
+
"text": "Your package has been shipped. It will be delivered in {{1}} business days."
111
+
},
112
+
```
113
+
114
+
Parameters are defined with the `values` values and `bindings` bindings. Use the values and bindings to assemble the `template` object.
115
+
116
+
```javascript
117
+
/*
118
+
* This sample shows how to send template message with below details
console.log("Message sent to:"+receipt.to+" with message id:"+receipt.messageId);
214
+
});
215
+
}
216
+
217
+
main().catch((error) => {
218
+
console.error("Encountered an error while sending message: ", error);
219
+
throw error;
220
+
});
221
+
222
+
module.exports= { main };
223
+
```
23
224
24
225
## Run the code
25
226
Use the node command to run the code you added to the send-messages.js file.
@@ -29,3 +230,5 @@ node ./send-messages.js
29
230
```
30
231
31
232
## Full sample code
233
+
234
+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-messages-rest/samples/v2/javascript/).
0 commit comments