Created
March 6, 2016 11:19
-
-
Save sambott/2a3718ed0cebf305111c to your computer and use it in GitHub Desktop.
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
package hackingit | |
import akka.actor.ActorSystem | |
import play.api.libs.json.{JsSuccess, Format, Json} | |
import slack.api.SlackApiClient | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
object Main extends App { | |
val token = "###########" | |
case class SlackMessage(`type`: String, user: String, text: String, ts: String) | |
implicit val slackMessageFmt: Format[SlackMessage] = Json.format[SlackMessage] | |
implicit val system = ActorSystem("slacking") | |
val client = SlackApiClient(token) | |
val quote = "The bosom of America is closed to receive not only the Opulent and respectable Stranger, but the oppressed and persecuted of all Nations And Religions; whom we shall wellcome to a participation of all our rights and previleges, if by decency and propriety of conduct they appear to merit the enjoyment." | |
val wikiQuotes = Map( | |
".*open.*" -> quote | |
) | |
def scan(): Future[Any] = { | |
for { | |
channels <- client.listChannels() | |
general = channels.filter(_.name == "general") | |
channelId = general.head.id | |
history <- client.getChannelHistory(channelId, count = Some(1)) | |
messages = history.messages | |
parsedMsgs = messages.map(Json.fromJson(_)) | |
chatMessages <- Future.successful { | |
parsedMsgs collect { | |
case JsSuccess(SlackMessage(_, _, text, _), _) => text | |
} | |
} | |
matchingMsg <- Future.successful { | |
wikiQuotes.find(entry => chatMessages.exists(_.matches(entry._1))) | |
} | |
_ <- Future.successful(matchingMsg.map { matchingTxt => | |
client.postChatMessage(channelId, quote) | |
}) | |
zzzz <- Future { Thread.sleep(1000) } | |
} yield scan() | |
} | |
scan() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment