Como enviar uma API WhatsApp utilizando a Swift

Introdução

Neste trecho de código, aprende a enviar uma API WhatsApp utilizando a Swift, a enviar uma mensagem de texto, templates WhatsApp, media, botões interactivos, listas, produtos… etc, é necessário ter uma conta Alvochat para executar os seguintes códigos. Crie uma conta, se não tiver uma.

Enviar modelo utilizando o WhatsApp API & Swift

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&name=hello_world".data(using: String.Encoding.utf8)!)
postData.append("&language=en_us".data(using: String.Encoding.utf8)!)
postData.append("&header=".data(using: String.Encoding.utf8)!)
postData.append("&body=".data(using: String.Encoding.utf8)!)
postData.append("&buttons=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/template")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar uma mensagem de texto utilizando o WhatsApp API & Swift

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&body=WhatsApp API on alvochat.com works good".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&preview_url=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/chat")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar imagem

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&image=https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg".data(using: String.Encoding.utf8)!)
postData.append("&caption=image caption".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/image")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar áudio

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&audio=https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/audio")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar vídeo

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&video=https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4".data(using: String.Encoding.utf8)!)
postData.append("&caption=video caption".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/video")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar documento

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&document=https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf".data(using: String.Encoding.utf8)!)
postData.append("&filename=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/document")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar autocolante

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&sticker=https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/sticker")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar Contacto

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&contact=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/contact")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar local

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&lat=37.484296".data(using: String.Encoding.utf8)!)
postData.append("&lng=-122.148703".data(using: String.Encoding.utf8)!)
postData.append("&address=Menlo Park, California, United States".data(using: String.Encoding.utf8)!)
postData.append("&name=Meta Headquarters".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/location")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar lista

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body= please select one of the following options".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&button=options".data(using: String.Encoding.utf8)!)
postData.append("§ions=option_1,option_2,option_3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/list")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Botão Enviar

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body= please select one of the following options".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&buttons=option_1,option_2,option_3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/button")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Enviar produtos

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body=Hi , check out our new products".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&catalog_id=".data(using: String.Encoding.utf8)!)
postData.append("&product=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/product")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Pode ver a Documentação API Whatsapp completa aqui.