gabrieldwight / Whatsapp-Business-Cloud-Api-Net

This is C# wrapper of whatsapp business cloud api for .NET
MIT License
283 stars 104 forks source link

Error Invalid parameter on CreateTemplateMessage #96

Closed badjem79 closed 3 weeks ago

badjem79 commented 3 weeks ago

Describe the bug When i call CreateTemplateMessage as in the example below i get the "invalid parameter" error

To Reproduce Code that throws the exception:

            Dim list = New List(Of String)()
            Dim text = get_WA_params(list)
            Dim templateData = New With {
                Key .name = Codice_WA.Text(),
                Key .category = "UTILITY",
                Key .allow_category_change = False,
                Key .language = LanguageCode.Italian,
                Key .components = New List(Of Object)() From {
                    New With {
                    Key .type = "BODY",
                    Key .text = text,
                    Key .example = New With {
                        Key .body_text = list
                        }
                    }
                }
            }

            Dim ret As TemplateMessageCreationResponse = whatsAppBusinessClient.CreateTemplateMessage(ID_BusinessAccount, templateData)

this is the json I get if i do serialize templateData:

{
   "name":"scadenza_revisione",
   "category":"UTILITY",
   "allow_category_change":false,
   "language":"it",
   "components":[
      {
         "type":"BODY",
         "text":"Scadenza revisione per il veicolo {{1}} con targa {{2}} in data {{3}}",
         "example":{
            "body_text":[
               "FIAT 500L",
               "AA123BB",
               "21/12/2024"
            ]
         }
      }
   ]
}

and this is the error stack trace:

WhatsappBusiness.CloudApi.Exceptions.WhatsappBusinessCloudAPIException
  HResult=0x80131500
  Messaggio=Invalid parameter
  Origine=WhatsappBusiness.CloudApi
  Analisi dello stack:
   at WhatsappBusiness.CloudApi.WhatsAppBusinessClient.<WhatsAppBusinessPostAsync>d__164`1.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at WhatsappBusiness.CloudApi.WhatsAppBusinessClient.CreateTemplateMessage(String whatsAppBusinessAccountId, Object template, WhatsAppBusinessCloudApiConfig cloudApiConfig, CancellationToken cancellationToken)
   at CarelGOnet.GestioneTestiSMS.Send_WA_Click(Object sender, EventArgs e) in C:\progetti\CarelGOnet\CarelGOnet\SMS\GestioneTestiSMS.vb:line 394
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at CarelGOnet.My.MyApplication.Main(String[] Args) in :line 83

Expected behavior The template should be created

I need at least to know how to debug the call at a deeper level as I can get only the error and I don't know how to move any further

When I use the call to verify the status of another template that allready exists, it works correctly, just to confirm that the ID_BusinessAccount is correct and the auth bearer too

Any help will be apreciated

badjem79 commented 3 weeks ago

I've found that the problem was in the "body_text" part, that should be an Array of Array of strings, not just an Array of strings. Anyway it is impossible to debug without getting the detailed error that is present in the json response inside the "error_user_title" and "error_user_msg" strings.

Any suggestion on how to get that errors?

And I suggest to create a preformatted object to send to CreateTemplateMessage, as building an object like this is error prone.

thanks

gabrieldwight commented 3 weeks ago

hi, creating some template message types usually has other unique properties in defining the component structure of the template message.

Mostly the errors are returned from the API response since the graph API combines whatsapp platform errors from cloud API and business management API. I will include these two properties; "error_user_title" and "error_user_msg".

To create a preformatted object I will have an issue mostly with the components but I will see what I can achieve if I find a common pattern with all the template message types.

badjem79 commented 3 weeks ago

I will include these two properties; "error_user_title" and "error_user_msg".

This would be very apreciatet, many thanks