Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created August 8, 2020 09:46
Show Gist options
  • Save jebai0521/75d5fa55251183f6aa87ec1fc1afa1c6 to your computer and use it in GitHub Desktop.
Save jebai0521/75d5fa55251183f6aa87ec1fc1afa1c6 to your computer and use it in GitHub Desktop.
func dingTalkMessage(_ message: String?) {
NotificationService .dingTalkMessage(message);
}
static func dingTalkMessage(_ message: String?) {
let date = Date()
let fmt = DateFormatter()
fmt.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
let session = URLSession.shared
let url = URL(string: "https://oapi.dingtalk.com/robot/send?access_token=34d24e60369700fed248820e980462b6376ec694d0dd2eb31bf6008cc1d4a5f6")
var request: NSMutableURLRequest? = nil
if let url = url {
request = NSMutableURLRequest(url: url)
}
request?.httpMethod = "POST"
request?.addValue("application/json", forHTTPHeaderField: "content-type")
let currentDevice = UIDevice.current
let bodyDict = [
"msgtype": "text",
"text": [
"content": "\(fmt.string(from: date)), [name] \(currentDevice.name),[model] \(currentDevice.model),[system] \(currentDevice.systemName)-\(currentDevice.systemVersion),[token] \(message ?? "")"
]
] as [String : Any]
var jsonData: Data? = nil
do {
jsonData = try JSONSerialization.data(withJSONObject: bodyDict, options: .prettyPrinted)
} catch {
}
if let jsonData = jsonData {
request?.httpBody = String(data: jsonData, encoding: .utf8)?.data(using: .utf8)
}
// RCTLog(@"[Token] %@", bodyDict[@"text"][@"content"]);
var dataTask: URLSessionDataTask? = nil
let req : URLRequest = request?.mutableCopy() as! URLRequest;
if request != nil {
dataTask = session.dataTask(with: req, completionHandler: { data, response, error in
})
}
dataTask?.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment