كيفية إرسال WhatsApp API باستخدام #C

مقدمة

في هذه المقالة ، سننشئ أمثلة بسيطة لإرسال الرسائل عبر WhatsApp API باستخدام c #.

في البداية ، يجب عليك التأكد من أن مكتبة RestSharp الإصدار الأخير : 108.0.1 جاهزة ، لأننا سنستخدم هذه المكتبة في هذه المقالة لإرسال رسائل إلى بوابة Ultramsg.

مثال سريع على WhatsApp API باستخدام C #

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";      
            string message = "WhatsApp API on UltraMsg.com works good";
            var url = "https://api.ultramsg.com/" + instanceId  +  "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("body", message);


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

عندما يتم تنفيذ الكود السابق ، وإذا تم إرسال الرسالة بنجاح ، فسيكون الرد كما يلي:

{"sent":"true","message":"ok","id":497}

إرسال صورة

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";      
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("image", "https://file-example.s3-accelerate.amazonaws.com/images/test.jpg");
            request.AddParameter("caption", "caption text");

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

إرسال ملف

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";      
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("filename", "test.pdf");
            request.AddParameter("document", "https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf");

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

إرسل صوت

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";    
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("audio", "https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3");

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

إرسال تسجيل صوتي

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";    
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/messages/voice";
            var client = new RestClient(url);
            var request = new RestRequest(url, Method.Post);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter("token", token);
            request.AddParameter("to", mobile);
            request.AddParameter("audio", "https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg");

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

إرسال فيديو

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";      
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("video", "https://file-example.s3-accelerate.amazonaws.com/video/test.mp4");

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

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";    
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/messages/link";
            var client = new RestClient(url);
            var request = new RestRequest(url, Method.Post);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter("token", token);
            request.AddParameter("to", mobile);
            request.AddParameter("link", "https://en.wikipedia.org/wiki/COVID-19");

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

إرسال جهة اتصال

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";    
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("contact", "[email protected]");

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

إرسال موقع جغرافي

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";  
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("address", "ABC company \n Sixth floor , office 38");
            request.AddParameter("lat", "25.197197");
            request.AddParameter("lng", "55.2721877");

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

إرسال Vcard

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";     
        
            var url = "https://api.ultramsg.com/" + instanceId  + "/messages/vcard";
            var client = new RestClient(url);
            var request = new RestRequest(url, Method.Post);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter("token", token);
            request.AddParameter("to", mobile);
            request.AddParameter("vcard", " BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD");
            RestResponse response = await client.ExecuteAsync(request);
            var output = response.Content;
            Console.WriteLine(output);
        }
    }
}

مثال لإرسال الصورة كـ Base64 باستخدام RestSharp

لإرسال الصور والمستندات من جهاز كمبيوتر أو خادم محلي ، يمكنك تحويل الملفات إلى تنسيق base64 وإرسالها بسهولة من خلال الكود التالي:


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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";   

            byte[] AsBytes = File.ReadAllBytes("test.jpeg");
            String AsBase64String = Convert.ToBase64String(AsBytes);
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("image", AsBase64String);
            RestResponse response = await client.ExecuteAsync(request);
            var output = response.Content;
            Console.WriteLine(output);
        }
    }
}

مثال لإرسال المستند كـ Base64 باستخدام RestSharp

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

namespace test
{
    class Program
    {
        static async Task Main(string[] args)
        {
           
            string instanceId = "instance950"; // your instanceId
            string token = "yourtoken";         //instance Token
            string mobile = "14155552671";   

            byte[] AsBytes = File.ReadAllBytes("test.pdf");
            String AsBase64String = Convert.ToBase64String(AsBytes);
            var url = "https://api.ultramsg.com/" + instanceId  + "/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", token);
            request.AddParameter("to", mobile);
            request.AddParameter("filename", "WhatsApp-API.pdf");
            request.AddParameter("document", AsBase64String);
            RestResponse response = await client.ExecuteAsync(request);
            var output = response.Content;
            Console.WriteLine(output);
        }
    }
}

WhatsApp API باستخدام C # مع WebClient

لا يوصى به للعمل الجديد ولكنه سهل للغاية ، إذا كنت بحاجة إلى تنفيذ طلب http بشكل متزامن ،WebClient لا يزال من الممكن استخدامها.

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

public class Program
{
    public static void Main()
    {
        using (var wb = new WebClient())
        {
            var data = new NameValueCollection();
            data["token"] = "95041eyo9eqixrsi";
            data["body"] = "WhatsApp API on UltraMsg.com works good";
            data["to"] = "14155552671";

            var response =
                wb
                    .UploadValues("https://api.ultramsg.com/instance950/messages/chat",
                    "POST",
                    data);
            string responseInString = Encoding.UTF8.GetString(response);
            Console.WriteLine (responseInString);
        }

        ///////////
    }
}

مثال لإرسال المستندات كـ Base64 باستخدام c # & WebClient

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

public class Program
{
    public static void Main()
    {
        using (var wb = new WebClient())
        {
            byte[] AsBytes = File.ReadAllBytes("1.pdf");
            String AsBase64String = Convert.ToBase64String(AsBytes);
            var data = new NameValueCollection();
            data["token"] = "95041eyo9eqixrsi";
            data["to"] = "14155552671";
            data["document"] = AsBase64String;
            data["filename"] = "a.pdf";
            var response =
                wb
                    .UploadValues("https://api.ultramsg.com/instance950/messages/document",
                    "POST",
                    data);
            string responseInString = Encoding.UTF8.GetString(response);
            Console.WriteLine (responseInString);
        }

        ///////////
    }
}

أخيرا يمكن مشاهدة دليل وثائق Whatsapp API Documentation. بالإضافة للأسئلة الشائعة FAQ