Skip to content

Instantly share code, notes, and snippets.

@cardoso
Created August 3, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardoso/f47efb9a508fc9fa8fa61a20ee7eb6d9 to your computer and use it in GitHub Desktop.
Save cardoso/f47efb9a508fc9fa8fa61a20ee7eb6d9 to your computer and use it in GitHub Desktop.
import AWSLambdaEvents
import AWSLambdaRuntime
import NIO
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
Lambda.run { (context: Lambda.Context, event: APIGateway.Request, callback: @escaping (Result<APIGateway.Response, Error>) -> Void) in
context.logger.debug("hello, api gateway!")
let botId = "Agent"
guard
let bodyData = event.body?.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: bodyData, options: []) as? [String: Any],
json["type"] as? String == "message.new",
let channelType = json["channel_type"] as? String,
let channelId = json["channel_id"] as? String,
let message = json["message"] as? [String: Any],
let user = message["user"] as? [String: Any],
let userId = user["id"] as? String,
userId != botId
else {
callback(.success(APIGateway.Response(statusCode: .ok, body: "nope!")))
return
}
botReply(channelType: channelType, channelId: channelId, botId: botId) {
callback(.success(APIGateway.Response(statusCode: .ok)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment