Skip to content

Instantly share code, notes, and snippets.

@pvzig
Last active November 14, 2019 13:35
Show Gist options
  • Save pvzig/c8f01710d22bfb6f16a9d26819bca81d to your computer and use it in GitHub Desktop.
Save pvzig/c8f01710d22bfb6f16a9d26819bca81d to your computer and use it in GitHub Desktop.

Revisions

  1. pvzig revised this gist Nov 14, 2019. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import Foundation
    import SlackKit

    class RobotOrNotBot {

    let verdicts: [String:Bool] = [
    "Mr. Roboto" : false,
    "Service Kiosks": false,
    @@ -75,41 +75,41 @@ class RobotOrNotBot {
    "Mic N. The Robot": true,
    "Robot Or Not Bot": false
    ]

    let bot: SlackKit

    init(token: String) {
    bot = SlackKit()
    bot.addRTMBotWithAPIToken(token)
    bot.addWebAPIAccessWithToken(token)
    bot.notificationForEvent(.message) { [weak self] (event, client) in
    bot.notificationForEvent(.message) { [weak self] (event, connection) in
    guard
    let message = event.message,
    let id = client?.authenticatedUser?.id,
    let id = connection?.client?.authenticatedUser?.id,
    message.text?.contains(id) == true
    else {
    return
    }
    self?.handleMessage(message)
    }
    }

    init(clientID: String, clientSecret: String) {
    bot = SlackKit()
    let oauthConfig = OAuthConfig(clientID: clientID, clientSecret: clientSecret)
    bot.addServer(oauth: oauthConfig)
    bot.notificationForEvent(.message) { [weak self] (event, client) in
    bot.notificationForEvent(.message) { [weak self] (event, connection) in
    guard
    let message = event.message,
    let id = client?.authenticatedUser?.id,
    let id = connection?.client?.authenticatedUser?.id,
    message.text?.contains(id) == true
    else {
    return
    }
    self?.handleMessage(message)
    }
    }

    // MARK: Bot logic
    private func handleMessage(_ message: Message) {
    if let text = message.text?.lowercased(), let timestamp = message.ts, let channel = message.channel {
    @@ -132,4 +132,4 @@ class RobotOrNotBot {
    let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
    // With OAuth
    // let slackbot = RobotOrNotBot(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
    RunLoop.main.run()
    RunLoop.main.run()
  2. pvzig revised this gist Sep 8, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -112,17 +112,17 @@ class RobotOrNotBot {

    // MARK: Bot logic
    private func handleMessage(_ message: Message) {
    if let text = message.text?.lowercased(), let channel = message.channel {
    if let text = message.text?.lowercased(), let timestamp = message.ts, let channel = message.channel {
    for (robot, verdict) in verdicts {
    let lowerbot = robot.lowercased()
    if text.contains(lowerbot) {
    let reaction = verdict ? "robot_face" : "no_entry_sign"
    bot.webAPI?.addReaction(name: reaction, channel: channel, timestamp: message.ts, success: nil, failure: nil)
    bot.webAPI?.addReactionToMessage(name: reaction, channel: channel, timestamp: timestamp, success: nil, failure: nil)
    return
    }
    }
    // Not found
    bot.webAPI?.addReaction(name: "question", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    bot.webAPI?.addReactionToMessage(name: "question", channel: channel, timestamp: timestamp, success: nil, failure: nil)
    return
    }
    }
  3. pvzig revised this gist Jun 4, 2017. 1 changed file with 37 additions and 20 deletions.
    57 changes: 37 additions & 20 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import Foundation
    import SlackKit

    class RobotOrNotBot: MessageEventsDelegate {
    class RobotOrNotBot {

    let verdicts: [String:Bool] = [
    "Mr. Roboto" : false,
    @@ -76,43 +76,60 @@ class RobotOrNotBot: MessageEventsDelegate {
    "Robot Or Not Bot": false
    ]

    let client: SlackClient
    let bot: SlackKit

    init(token: String) {
    client = SlackClient(apiToken: token)
    client.messageEventsDelegate = self
    bot = SlackKit()
    bot.addRTMBotWithAPIToken(token)
    bot.addWebAPIAccessWithToken(token)
    bot.notificationForEvent(.message) { [weak self] (event, client) in
    guard
    let message = event.message,
    let id = client?.authenticatedUser?.id,
    message.text?.contains(id) == true
    else {
    return
    }
    self?.handleMessage(message)
    }
    }

    // MARK: MessageEventsDelegate
    func received(_ message: Message, client: SlackClient) {
    if let id = client.authenticatedUser?.id {
    if message.text?.contains(id) == true {
    handleMessage(message: message)
    init(clientID: String, clientSecret: String) {
    bot = SlackKit()
    let oauthConfig = OAuthConfig(clientID: clientID, clientSecret: clientSecret)
    bot.addServer(oauth: oauthConfig)
    bot.notificationForEvent(.message) { [weak self] (event, client) in
    guard
    let message = event.message,
    let id = client?.authenticatedUser?.id,
    message.text?.contains(id) == true
    else {
    return
    }
    self?.handleMessage(message)
    }
    }
    func changed(_ message: Message, client: SlackClient) {}
    func deleted(_ message: Message?, client: SlackClient) {}
    func sent(_ message: Message, client: SlackClient) {}

    // MARK: Bot logic
    private func handleMessage(message: Message) {
    private func handleMessage(_ message: Message) {
    if let text = message.text?.lowercased(), let channel = message.channel {
    for (robot, verdict) in verdicts {
    let lowerbot = robot.lowercased()
    if text.contains(lowerbot) {
    if verdict == true {
    client.webAPI.addReaction(name: "robot_face", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    } else {
    client.webAPI.addReaction(name: "no_entry_sign", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    }
    let reaction = verdict ? "robot_face" : "no_entry_sign"
    bot.webAPI?.addReaction(name: reaction, channel: channel, timestamp: message.ts, success: nil, failure: nil)
    return
    }
    }
    client.webAPI.addReaction(name: "question", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    // Not found
    bot.webAPI?.addReaction(name: "question", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    return
    }
    }
    }

    // With API token
    let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
    slackbot.client.connect()
    // With OAuth
    // let slackbot = RobotOrNotBot(clientID: "CLIENT_ID", clientSecret: "CLIENT_SECRET")
    RunLoop.main.run()
  4. pvzig revised this gist Jan 2, 2017. 1 changed file with 84 additions and 82 deletions.
    166 changes: 84 additions & 82 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -1,79 +1,81 @@
    import Foundation
    import SlackKit

    class RobotOrNotBot: MessageEventsDelegate {

    let verdicts: [String:Bool] = [
    "Mr. Roboto" : false,
    "Service Kiosks": false,
    "Darth Vader": false,
    "K-9": true,
    "Emotions": false,
    "Self-Driving Cars": false,
    "Telepresence Robots": false,
    "Roomba": true,
    "Assembly-Line Robot": false,
    "ASIMO": false,
    "KITT": false,
    "USS Enterprise": false,
    "Transformers": true,
    "Jaegers": false,
    "The Major": false,
    "Siri": false,
    "The Terminator": true,
    "Commander Data": false,
    "Marvin the Paranoid Android": true,
    "Pinocchio": false,
    "Droids": true,
    "Hitchbot": false,
    "Mars Rovers": false,
    "Space Probes": false,
    "Sasquatch": false,
    "Toaster": false,
    "Toaster Oven": false,
    "Cylons": false,
    "V'ger": true,
    "Ilia Robot": false,
    "The TARDIS": false,
    "Johnny 5": true,
    "Twiki": true,
    "Dr. Theopolis": false,
    "robots.txt": false,
    "Lobot": false,
    "Vicki": true,
    "GlaDOS": false,
    "Turrets": true,
    "Wheatley": true,
    "Herbie the Love Bug": false,
    "Iron Man": false,
    "Ultron": false,
    "The Vision": false,
    "Clockwork Droids": false,
    "Podcasts": false,
    "Cars": false,
    "Swimming Pool Cleaners": false,
    "Burritos": false,
    "Prince Robot IV": false,
    "Daleks": false,
    "Cybermen": false,
    "The Internet of Things": false,
    "Nanobots": true,
    "Two Intermeshed Gears": false,
    "Crow T. Robot": true,
    "Tom Servo": true,
    "Thomas and Friends": false,
    "Replicants": false,
    "Chatbots": false,
    "Agents": false,
    "Lego Simulated Worm Toy": true,
    "Ghosts": false,
    "Exos": true,
    "Rasputin": false,
    "Tamagotchi": false,
    "T-1000": true,
    "The Tin Woodman": false,
    "Mic N. The Robot": true,
    "Robot Or Not Bot": false
    "Mr. Roboto" : false,
    "Service Kiosks": false,
    "Darth Vader": false,
    "K-9": true,
    "Emotions": false,
    "Self-Driving Cars": false,
    "Telepresence Robots": false,
    "Roomba": true,
    "Assembly-Line Robot": false,
    "ASIMO": false,
    "KITT": false,
    "USS Enterprise": false,
    "Transformers": true,
    "Jaegers": false,
    "The Major": false,
    "Siri": false,
    "The Terminator": true,
    "Commander Data": false,
    "Marvin the Paranoid Android": true,
    "Pinocchio": false,
    "Droids": true,
    "Hitchbot": false,
    "Mars Rovers": false,
    "Space Probes": false,
    "Sasquatch": false,
    "Toaster": false,
    "Toaster Oven": false,
    "Cylons": false,
    "V'ger": true,
    "Ilia Robot": false,
    "The TARDIS": false,
    "Johnny 5": true,
    "Twiki": true,
    "Dr. Theopolis": false,
    "robots.txt": false,
    "Lobot": false,
    "Vicki": true,
    "GlaDOS": false,
    "Turrets": true,
    "Wheatley": true,
    "Herbie the Love Bug": false,
    "Iron Man": false,
    "Ultron": false,
    "The Vision": false,
    "Clockwork Droids": false,
    "Podcasts": false,
    "Cars": false,
    "Swimming Pool Cleaners": false,
    "Burritos": false,
    "Prince Robot IV": false,
    "Daleks": false,
    "Cybermen": false,
    "The Internet of Things": false,
    "Nanobots": true,
    "Two Intermeshed Gears": false,
    "Crow T. Robot": true,
    "Tom Servo": true,
    "Thomas and Friends": false,
    "Replicants": false,
    "Chatbots": false,
    "Agents": false,
    "Lego Simulated Worm Toy": true,
    "Ghosts": false,
    "Exos": true,
    "Rasputin": false,
    "Tamagotchi": false,
    "T-1000": true,
    "The Tin Woodman": false,
    "Mic N. The Robot": true,
    "Robot Or Not Bot": false
    ]

    let client: SlackClient

    init(token: String) {
    @@ -82,35 +84,35 @@ class RobotOrNotBot: MessageEventsDelegate {
    }

    // MARK: MessageEventsDelegate
    func messageReceived(message: Message) {
    func received(_ message: Message, client: SlackClient) {
    if let id = client.authenticatedUser?.id {
    if message.text?.contains(query: id) == true {
    if message.text?.contains(id) == true {
    handleMessage(message: message)
    }
    }
    }
    func changed(_ message: Message, client: SlackClient) {}
    func deleted(_ message: Message?, client: SlackClient) {}
    func sent(_ message: Message, client: SlackClient) {}

    func messageSent(message: Message){}
    func messageChanged(message: Message){}
    func messageDeleted(message: Message?){}

    // MARK: Bot logic
    private func handleMessage(message: Message) {
    if let text = message.text?.lowercased(), channel = message.channel {
    if let text = message.text?.lowercased(), let channel = message.channel {
    for (robot, verdict) in verdicts {
    let lowerbot = robot.lowercased()
    if text.contains(query: lowerbot) {
    if text.contains(lowerbot) {
    if verdict == true {
    client.webAPI.addReaction(name: "robot_face", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    client.webAPI.addReaction(name: "robot_face", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    } else {
    client.webAPI.addReaction(name: "no_entry_sign", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    client.webAPI.addReaction(name: "no_entry_sign", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    }
    return
    }
    }
    client.webAPI.addReaction(name: "question", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    client.webAPI.addReaction(name: "question", channel: channel, timestamp: message.ts, success: nil, failure: nil)
    }
    }
    }

    let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
    slackbot.client.connect(pingInterval: 300)
    slackbot.client.connect()
  5. pvzig revised this gist Aug 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.swift
    Original file line number Diff line number Diff line change
    @@ -113,4 +113,4 @@ class RobotOrNotBot: MessageEventsDelegate {
    }

    let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
    slackbot.client.connect()
    slackbot.client.connect(pingInterval: 300)
  6. pvzig created this gist May 5, 2016.
    116 changes: 116 additions & 0 deletions main.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    import SlackKit

    class RobotOrNotBot: MessageEventsDelegate {

    let verdicts: [String:Bool] = [
    "Mr. Roboto" : false,
    "Service Kiosks": false,
    "Darth Vader": false,
    "K-9": true,
    "Emotions": false,
    "Self-Driving Cars": false,
    "Telepresence Robots": false,
    "Roomba": true,
    "Assembly-Line Robot": false,
    "ASIMO": false,
    "KITT": false,
    "USS Enterprise": false,
    "Transformers": true,
    "Jaegers": false,
    "The Major": false,
    "Siri": false,
    "The Terminator": true,
    "Commander Data": false,
    "Marvin the Paranoid Android": true,
    "Pinocchio": false,
    "Droids": true,
    "Hitchbot": false,
    "Mars Rovers": false,
    "Space Probes": false,
    "Sasquatch": false,
    "Toaster": false,
    "Toaster Oven": false,
    "Cylons": false,
    "V'ger": true,
    "Ilia Robot": false,
    "The TARDIS": false,
    "Johnny 5": true,
    "Twiki": true,
    "Dr. Theopolis": false,
    "robots.txt": false,
    "Lobot": false,
    "Vicki": true,
    "GlaDOS": false,
    "Turrets": true,
    "Wheatley": true,
    "Herbie the Love Bug": false,
    "Iron Man": false,
    "Ultron": false,
    "The Vision": false,
    "Clockwork Droids": false,
    "Podcasts": false,
    "Cars": false,
    "Swimming Pool Cleaners": false,
    "Burritos": false,
    "Prince Robot IV": false,
    "Daleks": false,
    "Cybermen": false,
    "The Internet of Things": false,
    "Nanobots": true,
    "Two Intermeshed Gears": false,
    "Crow T. Robot": true,
    "Tom Servo": true,
    "Thomas and Friends": false,
    "Replicants": false,
    "Chatbots": false,
    "Agents": false,
    "Lego Simulated Worm Toy": true,
    "Ghosts": false,
    "Exos": true,
    "Rasputin": false,
    "Tamagotchi": false,
    "T-1000": true,
    "The Tin Woodman": false,
    "Mic N. The Robot": true,
    "Robot Or Not Bot": false
    ]
    let client: SlackClient

    init(token: String) {
    client = SlackClient(apiToken: token)
    client.messageEventsDelegate = self
    }

    // MARK: MessageEventsDelegate
    func messageReceived(message: Message) {
    if let id = client.authenticatedUser?.id {
    if message.text?.contains(query: id) == true {
    handleMessage(message: message)
    }
    }
    }

    func messageSent(message: Message){}
    func messageChanged(message: Message){}
    func messageDeleted(message: Message?){}

    private func handleMessage(message: Message) {
    if let text = message.text?.lowercased(), channel = message.channel {
    for (robot, verdict) in verdicts {
    let lowerbot = robot.lowercased()
    if text.contains(query: lowerbot) {
    if verdict == true {
    client.webAPI.addReaction(name: "robot_face", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    } else {
    client.webAPI.addReaction(name: "no_entry_sign", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    }
    return
    }
    }
    client.webAPI.addReaction(name: "question", timestamp: message.ts, channel: channel, success: nil, failure: nil)
    }
    }
    }

    let slackbot = RobotOrNotBot(token: "xoxb-SLACK_API_TOKEN")
    slackbot.client.connect()