Introducción
En este fragmento de código, se aprende cómo enviar una API de WhatsApp utilizando Objective-C, para enviar un mensaje de texto, plantillas de WhatsApp, medios de comunicación, botones interactivos, listas, productos … etc, Alvochat cuenta es necesaria para ejecutar los siguientes códigos. Crea una cuenta si no la tienes.
Enviar plantilla con la API de WhatsApp
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/template"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&name=hello_world" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&language=en_us" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&buttons=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar un mensaje de texto con la API de WhatsApp
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/chat"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=WhatsApp API on alvochat.com works good" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&preview_url=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar imagen
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/image"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&image=https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=image caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar audio
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/audio"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&audio=https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar vídeo
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/video"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&video=https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&caption=video caption" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar documento
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/document"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&document=https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&filename=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar pegatina
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/sticker"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&sticker=https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar contacto
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/contact"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&contact=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar ubicación
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/location"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lat=37.484296" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&lng=-122.148703" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&address=Menlo Park, California, United States" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&name=Meta Headquarters" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar lista
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/list"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body= please select one of the following options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&button=options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"§ions=option_1,option_2,option_3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Botón de envío
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/button"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body= please select one of the following options" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&buttons=option_1,option_2,option_3" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Enviar productos
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.alvochat.com/instance1199/messages/product"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSDictionary *headers = @{
@"Content-Type": @"application/x-www-form-urlencoded"
};
[request setAllHTTPHeaderFields:headers];
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"send=true" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&token=YourToken" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&to=16315555555" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&header=header" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&body=Hi , check out our new products" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&footer=footer" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&catalog_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&product=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&priority=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"&message_id=" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
dispatch_semaphore_signal(sema);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"%@",responseDictionary);
dispatch_semaphore_signal(sema);
}
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
Puede ver la documentación completa de la API de Whatsapp aquí.