Skip to content

Commit

Permalink
Moved CommandsRecognizerBot into core
Browse files Browse the repository at this point in the history
  • Loading branch information
marioosh committed Jul 2, 2015
1 parent 6efca4a commit 3d3be1c
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.scalac.slack.bots.system

import io.scalac.slack.MessageEventBus
import io.scalac.slack.bots.IncomingMessageListener
import io.scalac.slack.common.{BaseMessage, BotInfoKeeper, Command}

class CommandsRecognizerBot(override val bus: MessageEventBus) extends IncomingMessageListener {

val commandChar = '$'

def receive: Receive = {

case bm@BaseMessage(text, channel, user, dateTime, edited) =>
//COMMAND links list with bot's nam jack can be called:
// jack link list
// jack: link list
// @jack link list
// @jack: link list
// $link list
def changeIntoCommand(pattern: String): Boolean = {
if (text.trim.startsWith(pattern)) {
val tokenized = text.trim.drop(pattern.length).trim.split("\\s")
publish(Command(tokenized.head, tokenized.tail.toList.filter(_.nonEmpty), bm))
true
}
false
}

//call by commad character
if (!changeIntoCommand(commandChar.toString))
BotInfoKeeper.current match {
case Some(bi) =>
//call by name
changeIntoCommand(bi.name + ":") ||
changeIntoCommand(bi.name) ||
//call by ID
changeIntoCommand(s"<@${bi.id}>:") ||
changeIntoCommand(s"<@${bi.id}>")

case None => //nothing to do!
}
}
}

0 comments on commit 3d3be1c

Please sign in to comment.