This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send message to Slack
- Loading branch information
Showing
6 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
## Slack | ||
|
||
### Post message to a channel in Slack | ||
|
||
Follow [Create a bot for your workspace](https://slack.com/intl/en-no/help/articles/115005265703-create-a-bot-for-your-workspace) to create, add a bot to your workspace. Obtain `Bot User OAuth Access Token` to use to authenticate | ||
|
||
```swift | ||
Slack { | ||
$0.post( | ||
message: .init( | ||
token: ProcessInfo().environment["slackBotToken"]!, | ||
channel: "random", | ||
text: "Hello from Puma", | ||
username: "onmyway133" | ||
) | ||
) | ||
} | ||
``` |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// Slack.swift | ||
// PumaCore | ||
// | ||
// Created by khoa on 30/12/2019. | ||
// | ||
|
||
import Foundation | ||
|
||
public class Slack { | ||
public var name: String = "Send message to Slack" | ||
public var isEnabled = true | ||
|
||
private var message: Message? | ||
|
||
public init(_ closure: (Slack) -> Void = { _ in }) { | ||
closure(self) | ||
} | ||
} | ||
|
||
public extension Slack { | ||
struct Message { | ||
let token: String | ||
let channel: String | ||
let text: String | ||
let username: String? | ||
|
||
public init( | ||
token: String, | ||
channel: String, | ||
text: String, | ||
username: String | ||
) { | ||
self.token = token | ||
self.channel = channel | ||
self.text = text | ||
self.username = username | ||
} | ||
} | ||
|
||
func post(message: Message) { | ||
self.message = message | ||
} | ||
} | ||
|
||
extension Slack: Task { | ||
public func run(workflow: Workflow, completion: @escaping TaskCompletion) { | ||
guard let message = message else { | ||
completion(.failure(PumaError.invalid)) | ||
return | ||
} | ||
|
||
let sender = MessageSender() | ||
sender.send(message: message, completion: { result in | ||
switch result { | ||
case .success: | ||
Deps.console.success("Posted: \(message.text)") | ||
case .failure(let error): | ||
Deps.console.error("Failed: \(error.localizedDescription)") | ||
} | ||
completion(result) | ||
}) | ||
} | ||
} | ||
|
||
private class MessageSender { | ||
struct Response: Decodable { | ||
let ok: Bool | ||
let error: String? | ||
} | ||
|
||
func send(message: Slack.Message, completion: @escaping (Result<(), Error>) -> Void) { | ||
guard let baseUrl = URL(string: "https://slack.com/api/chat.postMessage") else { | ||
completion(.failure(PumaError.invalid)) | ||
return | ||
} | ||
|
||
var components = URLComponents(url: baseUrl, resolvingAgainstBaseURL: false) | ||
components?.queryItems = [ | ||
URLQueryItem(name: "token", value: message.token), | ||
URLQueryItem(name: "channel", value: message.channel), | ||
URLQueryItem(name: "text", value: message.text), | ||
URLQueryItem(name: "pretty", value: "1") | ||
] | ||
|
||
if let username = message.username { | ||
components?.queryItems?.append( | ||
URLQueryItem(name: "username", value: username) | ||
) | ||
} | ||
|
||
guard let requestUrl = components?.url else { | ||
completion(.failure(PumaError.invalid)) | ||
return | ||
} | ||
|
||
var request = URLRequest(url: requestUrl) | ||
request.allHTTPHeaderFields = [ | ||
"Accept": "application/json" | ||
] | ||
|
||
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in | ||
guard let data = data else { | ||
completion(.failure(error ?? PumaError.invalid)) | ||
return | ||
} | ||
|
||
do { | ||
let response = try JSONDecoder().decode(Response.self, from: data) | ||
if response.ok { | ||
completion(.success(())) | ||
} else { | ||
completion(.failure(PumaError.from(string: response.error))) | ||
} | ||
} catch { | ||
completion(.failure(error)) | ||
} | ||
}) | ||
|
||
task.resume() | ||
} | ||
} |