pengenalan
Dalam coretan kod ini, anda belajar cara menghantar API WhatsApp menggunakan Swift, untuk menghantar mesej teks, Templat WhatsApp, media, butang interaktif, senarai, produk … dll, akaun Alvochat diperlukan untuk menjalankan kod berikut. Buat Akaun jika anda tidak mempunyainya.
Hantar 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()
Hantar mesej 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()
Hantar imej
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()
Hantar 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()
Hantar 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()
Hantar 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()
Hantar pelekat
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()
Hantar Kenalan
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()
Hantar 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()
Hantar senarai
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()
Butang hantar
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()
Hantar 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 boleh melihat Dokumentasi API Whatsapp Penuh di sini .