Created
January 12, 2017 19:54
-
-
Save Reedyuk/56d278cb6dcda8a3f5c2c95590e34aa1 to your computer and use it in GitHub Desktop.
Vapor Facebook Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
import Foundation | |
import HTTP | |
let drop = Droplet() | |
class Message { | |
var textString: String? | |
var sendJson: [String: AnyObject]? | |
init(message: JSON) { | |
let senderJson = message["sender"]! | |
let messageJson = message["message"] | |
if let messageJson = messageJson { | |
self.textString = messageJson["text"]?.string | |
} | |
let idJson = ["id": senderJson["id"]!.string!] | |
if let textString = self.textString { | |
let textJson = ["text": textString] | |
self.sendJson = ["recipient": idJson as AnyObject, "message": textJson as AnyObject] | |
} | |
} | |
} | |
let PAGE_ACCESS_TOKEN = "" | |
drop.get("fbwebhook") { request in | |
print("get webhook") | |
guard let token = request.data["hub.verify_token"]?.string else { | |
throw Abort.badRequest | |
} | |
guard let response = request.data["hub.challenge"]?.string else { | |
throw Abort.badRequest | |
} | |
if token == "2318934571" { | |
print("send response") | |
return Response(body: response) | |
} else { | |
return Response(body: "Error, invalid token") | |
} | |
} | |
drop.post("fbwebhook") { request in | |
print("Start") | |
let url = "https://graph.facebook.com/v2.6/me/messages?access_token=" + PAGE_ACCESS_TOKEN | |
//var body = request.body | |
if let json = request.json { | |
let entryArray = (json["entry"]?.array)! as! [JSON] | |
let entry = entryArray[0] | |
let messageArray = (entry["messaging"]?.array)! as! [JSON] | |
let messageJson = messageArray[0] | |
print(messageJson) | |
let message = Message(message: messageJson) | |
if let sendJson = message.sendJson { | |
let jsonData = try! JSONSerialization.data(withJSONObject: sendJson, options: JSONSerialization.WritingOptions.prettyPrinted) | |
let urlRequest = NSMutableURLRequest(url: URL(string: url)!) | |
urlRequest.httpMethod = "POST" | |
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
urlRequest.httpBody = jsonData | |
let task = URLSession.shared.dataTask(with: urlRequest as URLRequest, completionHandler: { (data, response, error) in | |
if let err = error { | |
return | |
} | |
}) | |
task.resume() | |
} else { | |
return Response(body: "error") | |
} | |
} | |
return Response(body: "text message") | |
} | |
drop.get("hello") { request in | |
return "Hello, world!" | |
} | |
drop.get { req in | |
return try drop.view.make("welcome", [ | |
"message": drop.localization[req.lang, "welcome", "title"] | |
]) | |
} | |
drop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I receive this error - 'Response had no 'Content-Type' header' here.