उद्देश्य-सी के साथ व्हाट्सएप एपीआई कैसे भेजें

परिचय

इस ट्यूटोरियल में, हम Objective-C का उपयोग करके WhatsApp API के माध्यम से संदेश भेजने के लिए सरल उदाहरण बनाएंगे।

उद्देश्य-सी का उपयोग करते हुए पहला व्हाट्सएप एपीआई संदेश


#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=WhatsApp API on UltraMsg.com works good" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=10" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/chat"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

छवि भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=image Caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/image"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

दस्तावेज़ भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&filename=hello.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/document"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

ऑडियो भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/audio"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

आवाज भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/voice"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

वीडियो भेजना

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/video"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];
#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&link=https://en.wikipedia.org/wiki/COVID-19" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/link"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

संपर्क भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&[email protected]" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/contact"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

स्थान भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&address=ABC company \n Sixth floor , office 38" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lat=25.197197" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lng=55.2721877" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/location"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

वीकार्ड भेजें

#import <Foundation/Foundation.h>

NSDictionary *headers = @{ @"content-type": @"application/x-www-form-urlencoded" };

NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"token=token_here" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=1408XXXXXXX" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&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" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&referenceId=" dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.ultramsg.com/instance1150/messages/vcard"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);
                                                }
                                            }];
[dataTask resume];

अंत में, आप पूर्ण व्हाट्सएप एपीआई दस्तावेज़ीकरण और अक्सर पूछे जाने वाले प्रश्न देख सकते हैं।