Skip to content

Instantly share code, notes, and snippets.

@cardoso
Last active August 4, 2020 12:59
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/445cb692d8662cb7a209ca5a99410b51 to your computer and use it in GitHub Desktop.
Save cardoso/445cb692d8662cb7a209ca5a99410b51 to your computer and use it in GitHub Desktop.
func botReply(channelType: String, channelId: String, botId: String, completion: @escaping () -> Void) {
let apiKey = "[insert_your_api_key_here]"
let jwt = "[insert_your_jwt_here]"
let response = [
"I am a bot",
"Beep boop",
"Having trouble?",
"I can help"
].randomElement() ?? ""
let url = URL(string: "https://chat-us-east-1.stream-io-api.com/channels/\(channelType)/\(channelId)/message?api_key=\(apiKey)")!
var request = URLRequest(url: url)
request.allHTTPHeaderFields = ["Authorization": jwt, "stream-auth-type": "jwt"]
request.httpMethod = "POST"
request.httpBody =
"""
{
"message" : {
"text" : "\(response)",
"silent" : false,
"user_id": "\(botId)"
}
}
""".data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { _,_,_ in
completion()
}
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment