最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Azure communication service when using WhatsApp template throws an exception - Stack Overflow

programmeradmin2浏览0评论

I have an azure communication service where whatsapp channel is connected with the template also available. The template looks like this

The inputs supplied to the API are invalid Status: 400 (Bad Request) ErrorCode: ModelValidationError

Content: {"error":{"code":"ModelValidationError","message":"The inputs supplied to the API are invalid","details":[{"code":"ModelValidationError","message":"notificationContent: The notificationContent field is required.","target":"notificationContent"},{"code":"ModelValidationError","message":"buttons[0].subType: Error converting value "otpcode" to type 'Microsoft.Azure.Communication.CrossPlatformMessages.Contract.Model.V3.Notifications.Requests.WhatsAppMessageButtonSubType'. Path 'buttons[0].subType', line 1, position 193.","target":"buttons[0].subType"}]}}

Headers: Date: Fri, 14 Mar 2025 22:26:04 GMT Transfer-Encoding: chunked Connection: keep-alive Request-Context: REDACTED MS-CV: o9PMlhHpZ0OyQzokOm+uDQ.0 Strict-Transport-Security: REDACTED Repeatability-Result: REDACTED X-Processing-Time: REDACTED x-azure-ref: REDACTED X-Cache: REDACTED Content-Type: application/json; charset=utf-8

The whatsapp template has authentication template and which has a code button. The Azure communication service communicates with WhatsApp with the following code

      var channelRegistrationId = new Guid(options.Value.ChannelRegistrationId);
 var recipientList = new List<string> { phoneNumber };
 var value1 = new MessageTemplateText("otpcode", otpCode);
 var bindings = new WhatsAppMessageTemplateBindings();
  bindings.Body.Add(new(value1.Name));
 bindings.Buttons.Add(new WhatsAppMessageTemplateBindingsButton("otpcode", otpCode));
 var template = new MessageTemplate(options.Value.PhoneNumberValidationTemplateName, "en_us");
 template.Values.Add(value1);
 template.Bindings = bindings;           
 var content = new TemplateNotificationContent(channelRegistrationId, recipientList, template);
 Response<SendMessageResult> response = await notificationMessagesClient.SendAsync(content);

But when running it i get the following exception

> Given request contains a parameter for WhatsApp which was invalid. This includes channel ID and template parameters.
Status: 400 (Bad Request)
ErrorCode: BadRequest

Content:
{"error":{"code":"BadRequest","message":"Given request contains a parameter for WhatsApp which was invalid. This includes channel ID and template parameters.","innererror":{"code":"WhatsAppAdminClient+WaErrorCode.InvalidParameter","message":"InvalidParameter: (#131008) Required parameter is missing. buttons: Button at index 0 of type Url requires a parameter"}}}

I have an azure communication service where whatsapp channel is connected with the template also available. The template looks like this

The inputs supplied to the API are invalid Status: 400 (Bad Request) ErrorCode: ModelValidationError

Content: {"error":{"code":"ModelValidationError","message":"The inputs supplied to the API are invalid","details":[{"code":"ModelValidationError","message":"notificationContent: The notificationContent field is required.","target":"notificationContent"},{"code":"ModelValidationError","message":"buttons[0].subType: Error converting value "otpcode" to type 'Microsoft.Azure.Communication.CrossPlatformMessages.Contract.Model.V3.Notifications.Requests.WhatsAppMessageButtonSubType'. Path 'buttons[0].subType', line 1, position 193.","target":"buttons[0].subType"}]}}

Headers: Date: Fri, 14 Mar 2025 22:26:04 GMT Transfer-Encoding: chunked Connection: keep-alive Request-Context: REDACTED MS-CV: o9PMlhHpZ0OyQzokOm+uDQ.0 Strict-Transport-Security: REDACTED Repeatability-Result: REDACTED X-Processing-Time: REDACTED x-azure-ref: REDACTED X-Cache: REDACTED Content-Type: application/json; charset=utf-8

The whatsapp template has authentication template and which has a code button. The Azure communication service communicates with WhatsApp with the following code

      var channelRegistrationId = new Guid(options.Value.ChannelRegistrationId);
 var recipientList = new List<string> { phoneNumber };
 var value1 = new MessageTemplateText("otpcode", otpCode);
 var bindings = new WhatsAppMessageTemplateBindings();
  bindings.Body.Add(new(value1.Name));
 bindings.Buttons.Add(new WhatsAppMessageTemplateBindingsButton("otpcode", otpCode));
 var template = new MessageTemplate(options.Value.PhoneNumberValidationTemplateName, "en_us");
 template.Values.Add(value1);
 template.Bindings = bindings;           
 var content = new TemplateNotificationContent(channelRegistrationId, recipientList, template);
 Response<SendMessageResult> response = await notificationMessagesClient.SendAsync(content);

But when running it i get the following exception

> Given request contains a parameter for WhatsApp which was invalid. This includes channel ID and template parameters.
Status: 400 (Bad Request)
ErrorCode: BadRequest

Content:
{"error":{"code":"BadRequest","message":"Given request contains a parameter for WhatsApp which was invalid. This includes channel ID and template parameters.","innererror":{"code":"WhatsAppAdminClient+WaErrorCode.InvalidParameter","message":"InvalidParameter: (#131008) Required parameter is missing. buttons: Button at index 0 of type Url requires a parameter"}}}
Share Improve this question edited Mar 14 at 22:33 Arshad Badar Khan asked Mar 14 at 20:29 Arshad Badar KhanArshad Badar Khan 1,0021 gold badge16 silver badges34 bronze badges 8
  • The notificationContent field is required. – Sampath Commented Mar 17 at 4:53
  • Still the same but the message is diff {"error":{"code":"ModelValidationError","message":"WhatsApp binding { Component = Button, Idx = 1, RefValue = whatsapp/otp/code/?otp_type=COPY_CODE&code=otp8525 } does not have corresponding value"}} – Arshad Badar Khan Commented Mar 18 at 2:02
  • i ran your code and this is the error message I get {"error":{"code":"ModelValidationError","message":"WhatsApp binding { Component = Button, Idx = 1, RefValue = whatsapp/otp/code/… } does not have corresponding value"}} – Arshad Badar Khan Commented Mar 18 at 3:50
  • 1drv.ms/f/s!AuVYVJx_rpbJ3jPGb_mV6CLF5K6E?e=rCFs6d heres a link to the code setup and the error – Arshad Badar Khan Commented Mar 18 at 3:58
  • Can you give detail error in text – Sampath Commented Mar 18 at 4:02
 |  Show 3 more comments

1 Answer 1

Reset to default 1

The API expects a notificationContent field, but it's missing in the request. Without this, Azure rejects the request with a 400 Bad Request (ModelValidationError).

csharp bindings.Buttons.Add(new WhatsAppMessageTemplateBindingsButton("otpcode", otpCode));

To this:

bindings.Buttons.Add(new WhatsAppMessageTemplateBindingsButton(WhatsAppMessageButtonSubType.Url.ToString(), otpCode));

Bellow is to send the WhatsApp OTP message via Azure Communication Services:


using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure;
using Azure.Communication.Messages;
using Azure.Communication.Messages.Models.Channels;

namespace WhatsAppAuthOTP
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Sending WhatsApp Authentication Template with OTP...\n");

            string connectionString = "connection string";

            NotificationMessagesClient notificationMessagesClient = new NotificationMessagesClient(connectionString);

            var channelRegistrationId = new Guid("c8b57863-73bf-4da3-961a-64d23f59e0c6");
            var recipientList = new List<string> { "+9196" };

            string templateName = "phonenumbervalidation";
            string templateLanguage = "en";
            string oneTimePassword = "3516517";

            MessageTemplate messageTemplate = new MessageTemplate(templateName, templateLanguage);
            WhatsAppMessageTemplateBindings bindings = new();

            var otpBody = new MessageTemplateText(name: "code", text: oneTimePassword);
            bindings.Body.Add(new(otpBody.Name));

            var otpButton = new MessageTemplateQuickAction("url")
            {
                Text = oneTimePassword
            };
            bindings.Buttons.Add(new WhatsAppMessageTemplateBindingsButton(WhatsAppMessageButtonSubType.Url.ToString(), otpButton.Name));


            messageTemplate.Values.Add(otpBody);
            messageTemplate.Values.Add(otpButton);
            messageTemplate.Bindings = bindings;
            TemplateNotificationContent templateContent = new TemplateNotificationContent(channelRegistrationId, recipientList, messageTemplate);
            Response<SendMessageResult> result = await notificationMessagesClient.SendAsync(templateContent);

            PrintResponse(result);
        }

        public static void PrintResponse(Response<SendMessageResult> response)
        {
            Console.WriteLine($"Response: {response.GetRawResponse().Status} - {response.GetRawResponse().ReasonPhrase}");
            foreach (var receipt in response.Value.Receipts)
            {
                Console.WriteLine($"Message ID: {receipt.MessageId}");
            }
        }
    }
}

发布评论

评论列表(0)

  1. 暂无评论