Closed arnab77in closed 2 years ago
@arnab77in This is the class (TextTemplateMessageParameterRequest.cs) I created to send a text template with parameters that I'm going to test soon since it can be a complex class depending on the use case. The components class property is where it contains the list of parameters specified on your text template specified. Trying to figure out how to map specified placeholders to the parameter property of the component class.
This is a sample request from a WhatsApp example with predefined objects supported by cloud api.
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{Recipient-Phone-Number}}",
"type": "template",
"template": {
"name": "template-name",
"language": {
"code": "language-and-locale-code"
},
"components": [
{
"type": "body",
"parameters": [
{
"type": "text",
"text": "text-string"
},
{
"type": "currency",
"currency": {
"fallback_value": "$100.99",
"code": "USD",
"amount_1000": 100990
}
},
{
"type": "date_time",
"date_time": {
"fallback_value": "February 25, 1977",
"day_of_week": 5,
"year": 1977,
"month": 2,
"day_of_month": 25,
"hour": 15,
"minute": 33,
"calendar": "GREGORIAN"
}
}
]
}
]
}
}
I will update you if I'm able to solve it or if you have any ideas to solve with custom placeholders then you can update the source code.
@arnab77in I have managed to solve the issue and added more examples for template messages with parameters on the sample code. You will need to update to the latest version of the nuget. Here is an example of how to send text message with parameters
// For Text Template message with parameters supported component type is body only
TextTemplateMessageRequest textTemplateMessage = new TextTemplateMessageRequest();
textTemplateMessage.To = sendTemplateMessageViewModel.RecipientPhoneNumber;
textTemplateMessage.Template = new TextMessageTemplate();
textTemplateMessage.Template.Name = sendTemplateMessageViewModel.TemplateName;
textTemplateMessage.Template.Language = new TextMessageLanguage();
textTemplateMessage.Template.Language.Code = LanguageCode.English_US;
textTemplateMessage.Template.Components = new List<TextMessageComponent>();
textTemplateMessage.Template.Components.Add(new TextMessageComponent()
{
Type = "body",
Parameters = new List<TextMessageParameter>()
{
new TextMessageParameter()
{
Type = "text",
Text = "Testing Parameter Placeholder Position 1"
},
new TextMessageParameter()
{
Type = "text",
Text = "Testing Parameter Placeholder Position 2"
}
}
});
var results = await _whatsAppBusinessClient.SendTextMessageTemplateAsync(textTemplateMessage);
For example, if your template message is defined as: Hi {{1}}, welcome to {{2}}
The components list property will present the position of your placeholders defined in the template message.
Let me know if that solved your issue
i have problems sending a message from a template that has two parameters in the body and i get this exception
@carloslopezjoya If you have defined your template message with two parameters in WhatsApp Manager then the message template name containing the two parameters placeholder should work.
Example a template message like this: Hi {{1}}, welcome to {{2}} contains two parameter placeholder.
The number of parameters should match the number of elements on the text message body.
@gabrieldwight I have already validated and it works correctly, i would like to know where i can modify the following exception messages when a wrong number is entered
@carloslopezjoya The exception sent by cloud api come with this response format:
{
"error": {
"message": "(#131006) Resource not found", // error description
"type": "OAuthException",
"code": 131006, // please refer to the Error Codes section
"error_data": {
"messaging_product": "whatsapp",
"details": "invalid parameter" // detailed error message to help with debugging
},
"error_subcode": 2494007,
"fbtrace_id": "Az8or2yhqkZfEZ-_4Qn_Bam"
}
}
The exception message I deserialize with this class WhatsAppErrorResponse. I use the WhatsappBusinessCloudAPIException class to get the exception message from WhatsAppErrorResponse object.
using WhatsappBusiness.CloudApi.Exceptions;
try
{
return await _whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest);
}
catch (WhatsappBusinessCloudAPIException ex)
{
_logger.LogError(ex, ex.Message);
}
@gabrieldwight thanks for your explanation
I could not find and send method to send Text Text Template with parameter through class object is present there.
can you help? or I have to update your source code