gabrieldwight / Whatsapp-Business-Cloud-Api-Net

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

CTA buttons for Interactive messages #54

Closed borrmann closed 8 months ago

borrmann commented 8 months ago

Is your feature request related to a problem? Please describe. Interactive Messages can also send a Click to Action Button with a url parameter

Describe the solution you'd like see here: https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages/

"action": { "name": "cta_url", "parameters": { "display_text": "See Dates", "url": "https://www.luckyshrub.com?clickID=kqDGWd24Q5TRwoEQTICY7W1JKoXvaZOXWAS7h1P76s0R7Paec4" } }

Thanks for the great library and quick support!!

gabrieldwight commented 8 months ago

Thanks, I will add this feature in the next update

borrmann commented 8 months ago

Would it also be possible to add a method that accepts a dynamic? In that case, not all message types need to be defined as class objects but the return result of this library could still be created

It could look similar to this:

public async Task<WhatsAppBusinessResultClass> SendGeneric(dynamic whatsAppMessage)
{
        var request = new HttpRequestMessage(HttpMethod.Post, $"https://graph.facebook.com/v18.0/{_waPhoneNr}/messages");
       request.Headers.Add("Authorization", $"Bearer {_waToken}");

      var stringCont = JsonConvert.SerializeObject(whatsAppMessage);
      var content = new StringContent(stringCont, Encoding.UTF8, "application/json");

       request.Content = content;
       var responsef = await _httpClient.SendAsync(request);

      dynamic result = null;
      var serializer = new Newtonsoft.Json.JsonSerializer();
        await responsef.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
      {
             using var reader = new StreamReader(stream.Result);
             using var json = new JsonTextReader(reader);
             result = serializer.Deserialize<dynamic>(json);
         });
      }
      var request = new HttpRequestMessage(HttpMethod.Post, $"https://graph.facebook.com/v18.0/{_waPhoneNr}/messages");
     request.Headers.Add("Authorization", $"Bearer {_waToken}");

      var stringCont = JsonConvert.SerializeObject(dynamicContent);
      var content = new StringContent(stringCont, Encoding.UTF8, "application/json");

      request.Content = content;
       var responsef = await _httpClient.SendAsync(request);

      dynamic result = null;
      var serializer = new Newtonsoft.Json.JsonSerializer();
     await responsef.Content.ReadAsStreamAsync().ContinueWith((Task<Stream> stream) =>
     {
            using var reader = new StreamReader(stream.Result);
             using var json = new JsonTextReader(reader);
            result = serializer.Deserialize<dynamic>(json);
      });

      // create and return resultobject

with e.g.

var dynamicContent = new
 {
     messaging_product = "whatsapp",
     recipient_type = "individual",
     to = identifier,
     type = "interactive",
     interactive = new
     {
         type = "cta_url",
         header = new
         {
             type = "text",
             text = header
         },
         body = new
         {
             text = message
         },
         footer = new
         {
             text = footer
         },
         action = new
         {
             name = "cta_url",
             parameters = new 
             {
                 display_text = "mytext",
                 url = "myurl"
             }
         }
     }
 };

await SendMessage(dynamicContent);
gabrieldwight commented 8 months ago

Yes it is possible to add the generic method

gabrieldwight commented 8 months ago

I have implemented interactive message support for CTA buttons and also added SendGenericMessage Method to support dynamic messages.