مقدمة
في مقتطف الشفرة هذا ، تتعلم كيفية إرسال WhatsApp API باستخدام Swift ، لإرسال رسالة نصية ، قوالب WhatsApp ، وسائط ، أزرار تفاعلية ، قوائم ، منتجات … إلخ ، حساب Alvochat مطلوب لتشغيل الرموز التالية. قم بإنشاء حساب إذا لم يكن لديك حساب.
أرسل نموذجًا باستخدام 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()
أرسل رسالة نصية باستخدام 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()
إرسال صورة
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()
إرسال الصوت
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()
ارسل مقطع فيديو
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()
إرسال المستندات
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()
إرسال الستيكر
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()
إرسال جهة اتصال
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()
إرسال موقع
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()
إرسال القوائم
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()
إرسال أزرار الواتس اب التفاعليه
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()
إرسال المنتجات
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()
يمكنك الاطلاع على وثائق Whatsapp API الكاملة هنا .