परिचय
इस लेख में, हम c# का उपयोग करके व्हाट्सएप एपीआई के माध्यम से संदेश भेजने के लिए सरल उदाहरण बनाएंगे।
प्रारंभ में, आपको यह सुनिश्चित करना चाहिए कि रेस्टशर्प पुस्तकालय अंतिम Ver: 108.0.1 तैयार है और क्योंकि हम इस लेख में इस पुस्तकालय का उपयोग Ultramsg गेटवे पर संदेश भेजने के लिए करेंगे।
सी # का उपयोग कर व्हाट्सएप एपीआई के लिए त्वरित उदाहरण
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);
}
}
}
वीकार्ड भेजें
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);
}
}
}
रेस्टशर्प का उपयोग करके छवि को बेस 64 के रूप में भेजने का उदाहरण
स्थानीय पीसी या सर्वर से चित्र और दस्तावेज़ भेजने के लिए, आप फ़ाइलों को बेस 64 प्रारूप में परिवर्तित कर सकते हैं और उन्हें निम्न कोड के माध्यम से आसानी से भेज सकते हैं:
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);
}
}
}
रेस्टशर्प का उपयोग करके दस्तावेज़ को बेस 64 के रूप में भेजने का उदाहरण
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);
}
}
}
व्हाट्सएप एपीआई वेब क्लाइंट के साथ सी # का उपयोग कर रहा है
यह नए काम के लिए अनुशंसित नहीं है, लेकिन यह बेहद आसान है, अगर आपको एक 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);
}
///////////
}
}
सी # और वेब क्लाइंट का उपयोग करके दस्तावेज़ों को बेस 64 के रूप में भेजने का उदाहरण
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);
}
///////////
}
}
अंत में, आप पूर्ण व्हाट्सएप एपीआई दस्तावेज़ीकरण और अक्सर पूछे जाने वाले प्रश्न देख सकते हैं।