Cara Mengirim API WhatsApp menggunakan Swift

Pendahuluan

Dalam cuplikan kode ini, Anda belajar cara mengirim WhatsApp API menggunakan Swift, untuk mengirim pesan teks, Template WhatsApp, media, tombol interaktif, daftar, produk … dll, akun Alvochat diperlukan untuk menjalankan kode berikut. Buat Akun jika Anda tidak memilikinya.

Kirim templat menggunakan 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()

Kirim pesan teks menggunakan 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()

Kirim gambar

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()

Kirim audio

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()

Kirim video

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()

Kirim dokumen

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()

Kirim stiker

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()

Kirim Kontak

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()

Kirim Lokasi

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()

Kirim daftar

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()

Tombol Kirim

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()

Kirim produk

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()

Anda dapat melihat Dokumentasi API Whatsapp lengkap di sini.