I am writing a C# code and I want a link from the WhatsApp API to run at the code to send message automatically and I cant find it what should I do?

Viewed 78

I am writing a ASP.net using C# code targeting to send a WhatsApp message when I run it, by browsing a link and this link send a message once browsed. so I started using the WhatsApp cloud API and it provided me with this text but I don't know how to use it. Note:i tried using the link written between the text but it didn't run properly

curl -i -X POST `
  https://graph.facebook.com/v14.0/104958105698690/messages `
  -H 'Authorization: Bearer EABOoKYETUE4BANvFhvdZAX2udBdrHQ8ZBGJQjX2GJUskGlZCpRf17WZB4Etks1SMA2uGCBZAEl0cZB9Rw57hMpB6CPZCZCU9wIWYZCzsoxGcztZBkJHyNd1A8LZCIq5QFz1h6oLVLeacnpDAG05nhKkli43beAd6pDnrh5sKhnoOsIqzvK7uQXtLgZCENGH7wbUmtKfXOOb6ZAZBq7vA6Om78FVTKb' `
  -H 'Content-Type: application/json' `
  -d '{ \"messaging_product\": \"whatsapp\", \"to\": \"\", \"type\": \"template\", \"template\": { \"name\": \"hello_world\", \"language\": { \"code\": \"en_US\" } } }'

I have used many API's and it worked properly because they give me a direct link to use in my code unlike WhatsApp API which gives me this text above.

here is the code I am using to send the message but i want a link to put inside HttpWebRequest.Create(" ")

            WebRequest request = HttpWebRequest.Create(" ");
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string urltext = reader.ReadToEnd();

Thanks in advance.

1 Answers

Here is a quick example of how to use an HttpClient to send the data you need to send to the WhatsApp On-Premises Business API. This example is not complete and cannot be directly implemented into production but does show you how to form the post to the WhatsApp API. Do bear in mind you may need to add auth headers and other parts to the request and this is a minimum example that only shows how to form the request itself.

The URI for the API will be different so this example will always return an error message from Facebook.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace testingapp
{
    class Program
    {
        static readonly HttpClient client = new HttpClient();
        static async Task Main(string[] args)
        {
            //var x will either contain the message ID if needed for
            //future use or it will contain the error message from the server.
            var x = await Task.Run(() => sendMessage());
        }
        static async Task<string> sendMessage()
        {
            try
            {
                var textMessage = "Test message";
                var message = new Dictionary<string, string>
                {
                    {"preview_url", "false" },
                    { "recipient_type", "individual"},
                    { "to", "whatsapp-id" },
                    { "type", "text"},
                    { "body", textMessage}
                };
                var content = new FormUrlEncodedContent(message);
                HttpResponseMessage response = await client
                    .PostAsync("https://example.facebook.com/whatsapp/api/messages/text"
                    , content);
                string responseBody = await response.Content.ReadAsStringAsync();
                return responseBody;
            }
            catch (HttpRequestException e)
            {
                return e.Message.ToString();
            }
        }
    }
}

If you want to respond to a message you will need to implement a similar solution using WhatsApps API to respond to a specific message. Definitely refer to their documentation for the proper structure of a message to them.

Related