Как отправить API WhatsApp с помощью C#

Введение

В этом фрагменте кода c# вы узнаете, как отправить WhatsApp API с помощью c-sharp, чтобы отправить текстовое сообщение, WhatsApp Шаблоны, медиа, интерактивные кнопки, списки, продукты … и т.д., Для запуска следующих кодов требуется учетная запись Alvochat. Создайте аккаунт, если у вас его нет.

Вначале необходимо убедиться, что библиотека RestSharp последней версии Ver: 108.0.1 готова, потому что мы будем использовать эту библиотеку в этой статье для отправки сообщений Whatsapp на шлюз Alvochat.

Отправка шаблона с помощью WhatsApp API и c#

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/template";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("name", "hello_world");
			request.AddParameter("language", "en_us");
			request.AddParameter("header", "");
			request.AddParameter("body", "");
			request.AddParameter("buttons", "");
			request.AddParameter("priority", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправка текстового сообщения с помощью WhatsApp API и c#

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/chat";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("body", "WhatsApp API on alvochat.com works good");
			request.AddParameter("priority", "");
			request.AddParameter("preview_url", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить изображение

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/image";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("image", "https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg");
			request.AddParameter("caption", "image caption");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить аудио

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/audio";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("audio", "https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить видео

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/video";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("video", "https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4");
			request.AddParameter("caption", "video caption");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить документ

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/document";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("document", "https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf");
			request.AddParameter("filename", "");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить наклейку

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/sticker";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("sticker", "https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить контакт

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/contact";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("contact", "");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить местоположение

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/location";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("lat", "37.484296");
			request.AddParameter("lng", "-122.148703");
			request.AddParameter("address", "Menlo Park, California, United States");
			request.AddParameter("name", "Meta Headquarters");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Список отправки

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/list";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("header", "header");
			request.AddParameter("body", " please select one of the following options");
			request.AddParameter("footer", "footer");
			request.AddParameter("button", "options");
			request.AddParameter("sections", "option_1,option_2,option_3");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Кнопка отправки

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/button";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("header", "header");
			request.AddParameter("body", " please select one of the following options");
			request.AddParameter("footer", "footer");
			request.AddParameter("buttons", "option_1,option_2,option_3");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Отправить продукцию

using System;
using System.Threading.Tasks;
using RestSharp;

public class Program
{
    public static async Task Main()
    {

        var url = "https://api.alvochat.com/instance1199/messages/product";
        var client = new RestClient(url);

        var request = new RestRequest(url, Method.Post);    
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
  			request.AddParameter("token", "YourToken");
			request.AddParameter("to", "16315555555");
			request.AddParameter("header", "header");
			request.AddParameter("body", "Hi , check out our new products");
			request.AddParameter("footer", "footer");
			request.AddParameter("catalog_id", "");
			request.AddParameter("product", "");
			request.AddParameter("priority", "");
			request.AddParameter("message_id", "");


        RestResponse response = await client.ExecuteAsync(request);
        var output = response.Content;
        Console.WriteLine(output);
    }
}

Вы можете посмотреть полную документацию Whatsapp API здесь.