Skip to content

Commit

Permalink
Merge pull request #7 from IanKeen/bot-cleanup
Browse files Browse the repository at this point in the history
Bot Beautification!
  • Loading branch information
IanKeen committed Jun 18, 2016
2 parents bb1fc07 + 1d21c7a commit 143e98a
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 117 deletions.
9 changes: 2 additions & 7 deletions Sources/App/Services/HelloBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@ import RTMAPI
import WebAPI
import Models

final class HelloBot: SlackBotAPI {
func connected(slackBot: SlackBot, botUser: BotUser, team: Team, users: [User], channels: [Channel], groups: [Group], ims: [IM]) { }
func disconnected(slackBot: SlackBot, error: ErrorProtocol?) { }
func error(slackBot: SlackBot, error: ErrorProtocol) { }
func event(slackBot: SlackBot, event: RTMAPIEvent, webApi: WebAPI) { }

final class HelloBot: SlackMessageService {
func message(slackBot: SlackBot, message: MessageAdaptor, previous: MessageAdaptor?) {
let greetings = ["hello", "hi", "hey"]
guard
let target = message.target, sender = message.sender
else { return }

if (message.text.hasPrefix(options: greetings) && message.mentioned_users.contains(slackBot.me)) {
slackBot.chat(target: target, text: "hey, <@\(sender.id)>")
slackBot.chat(with: target, text: "hey, <@\(sender.id)>")
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions Sources/App/Services/KarmaBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ extension KarmaBot {
}
}

final class KarmaBot: SlackBotAPI {
final class KarmaBot: SlackRTMEventService, SlackMessageService {
private let options: Options

init(options: Options) {
self.options = options
}

func connected(slackBot: SlackBot, botUser: BotUser, team: Team, users: [User], channels: [Channel], groups: [Group], ims: [IM]) { }
func disconnected(slackBot: SlackBot, error: ErrorProtocol?) { }
func error(slackBot: SlackBot, error: ErrorProtocol) { }
func event(slackBot: SlackBot, event: RTMAPIEvent, webApi: WebAPI) {
switch event {
case .reaction_added(let reaction, let user, let itemCreator, let target):
Expand All @@ -70,7 +67,7 @@ final class KarmaBot: SlackBotAPI {
self.adjustKarma(of: itemCreator, action: karma, storage: slackBot.storage)

slackBot.chat(
target: target,
with: target,
text: karma.randomMessage(user: itemCreator, storage: slackBot.storage)
)

Expand All @@ -97,10 +94,7 @@ final class KarmaBot: SlackBotAPI {

guard !response.isEmpty else { return }

slackBot.chat(
target: target,
text: response
)
slackBot.chat(with: target, text: response)
}

private func karma(for user: User, from message: MessageAdaptor) -> KarmaAction? {
Expand Down
6 changes: 3 additions & 3 deletions Sources/App/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ let config = try SlackBotConfig.makeConfig(from: Environment())
let bot = SlackBot(
config: config,
storage: try RedisStorage(url: config.storageUrl!),
apis: [
HelloBot(),
KarmaBot(options: KarmaBot.Options(
services: [
HelloBot(),
KarmaBot(options: KarmaBot.Options(
targets: ["*"],
addText: "++",
addReaction: "+1",
Expand Down
35 changes: 29 additions & 6 deletions Sources/Bot/Adaptors/MessageAdaptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@

import Models

/// A wrapper around a `Message` model to provide additional functionality
public struct MessageAdaptor {
//MARK: - Private
private let slackModels: SlackBot.SlackModelClosure

//MARK: - Public Raw Properties
/// The underlying `Message` this `MessageAdaptor` uses
public let message: Message

//MARK: - Public Derived Properties
/// `String` representing the text of the `Message`
public var text: String { return self.message.text ?? "" }

/// The `User` representing the sender of this `Message`
public var sender: User? {
return
self.message.user ??
self.message.edited?.user ??
self.message.inviter ??
self.message.bot
}

/// The `Target` for this `Message`
public var target: Target? {
return self.message.channel?.value
}

/// A sequence of mentioned `User`s in the `Message`
public var mentioned_users: [User] {
let users = slackModels().users + slackModels().users.botUsers()

Expand All @@ -35,6 +44,8 @@ public struct MessageAdaptor {
users.filter { $0.id == link.link }
}
}

/// A sequence of mentioned `Channel`s in the `Message`
public var mentioned_channels: [Channel] {
let channels = slackModels().channels

Expand All @@ -43,17 +54,32 @@ public struct MessageAdaptor {
channels.filter { $0.id == link.link }
}
}

/// A sequence of mentioned `Link`s in the `Message` that are not `Channel`s or `User`s
public var mentioned_links: [Link] {
return self.mentionedLinks() {
!$0.link.hasPrefix("@") && !$0.link.hasPrefix("#")
}
}

//MARK: - Lifecycle
init(message: Message, slackModels: SlackBot.SlackModelClosure) {
self.message = message
self.slackModels = slackModels
}
}

//MARK: - Link Extraction
extension MessageAdaptor {
private func mentionedLinks(prefix: String = "", filter: ((Link) -> Bool) = { _ in true }) -> [Link] {
guard self.text.characters.contains("<") && self.text.characters.contains(">") else { return [] }

//NOTE: so far I've just been avoid RegEx for the sake of it...
// however if I turn to the dark side the expression used should be <(.*?)>
//
//TODO
//UPDATE: The more I look at the code below the more I dislike it :P - pony up and change to regex...
//
//From Slack: https://api.slack.com/docs/formatting
//1. Detect all sequences matching <(.*?)>
//2. Within those sequences, format content starting with #C as a channel link
Expand All @@ -71,15 +97,11 @@ public struct MessageAdaptor {
.flatMap { Link(link: $0) }
.filter { filter($0) }
}

//MARK: - Lifecycle
init(message: Message, slackModels: SlackBot.SlackModelClosure) {
self.message = message
self.slackModels = slackModels
}
}

//MARK: - Message.Link
extension MessageAdaptor {
/// Represents a link extracted from a `Message`
public struct Link {
public let link: String
public let displayText: String
Expand All @@ -97,6 +119,7 @@ extension MessageAdaptor {
}
}

//MARK: - Convenience: Message to MessageAdaptor
extension Message {
func toAdaptor(slackModels: SlackBot.SlackModelClosure) -> MessageAdaptor {
return MessageAdaptor(message: self, slackModels: slackModels)
Expand Down
8 changes: 8 additions & 0 deletions Sources/Bot/Builders/SlackMessageBuilder+Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
import Models
import Foundation

/**
These are a set of optional operators to make constructing a `SlackMessage` made of lots of components a little more fluent
Everything these operators do can be done with the `SlackMessage` object directly
*/


//MARK: - SlackMessage Operators

func +(builder: SlackMessage, value: String) -> SlackMessage {
return builder.text(value)
}
Expand Down

0 comments on commit 143e98a

Please sign in to comment.