Redis async client

This commit is contained in:
ekardnam 2019-07-12 12:05:01 +02:00
parent 55c179bc49
commit 41e83b5445
8 changed files with 154 additions and 12 deletions

View file

@ -24,7 +24,6 @@ import com.bot4s.telegram.future.{Polling, TelegramBot}
import com.bot4s.telegram.models.{Message, User}
import org.congressodeiradicali.karlmarx.coreplugins.{BanPlugin, EchoPlugin, PluginManagerPlugin, SetPlugin}
import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
import com.redis._
import scala.concurrent.Future
@ -41,15 +40,17 @@ class Bot(val token: String) extends TelegramBot
override val client: RequestHandler[Future] = new ScalajHttpClient(token)
val redis = new RedisClient(sys.env("REDIS_HOST"), 6379)
val redis = new RedisAsyncClient(sys.env("REDIS_HOST"), 6379)
def getLogger() = logger
def plugin(plugin: Plugin) = plugin.identifier -> plugin
var plugins: Map[String, Plugin] = Map(
"pluginmanager" -> new PluginManagerPlugin(this),
"ban" -> new BanPlugin(this),
"echo" -> new EchoPlugin(this),
"set" -> new SetPlugin(this)
plugin(new PluginManagerPlugin(this)),
plugin(new BanPlugin(this)),
plugin(new EchoPlugin(this)),
plugin(new SetPlugin(this))
)
// Future[Option[T]] is just two wrappers; remove the inner, useless one

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx
import com.bot4s.telegram.api.declarative.CommandFilterMagnet

View file

@ -0,0 +1,38 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx
import com.redis._
import scala.concurrent.{ExecutionContext, Future}
class RedisAsyncClient(private val host: String, private val port: Int) {
implicit val ec: ExecutionContext = ExecutionContext.global
private val redisClient = new RedisClient(host, port)
def set(key: Any, value: Any) = Future[Unit] {
redisClient.set(key, value)
}
def get(key: Any) = Future[Option[String]] {
redisClient.get(key)
}
}

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx.coreplugins
import org.congressodeiradicali.karlmarx.Plugin

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}

View file

@ -1,3 +1,20 @@
/*
This file is part of karl-marx.
karl-marx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
karl-marx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
*/
package org.congressodeiradicali.karlmarx.coreplugins
import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
@ -16,16 +33,17 @@ class SetPlugin(bot: Bot) extends CorePlugin with CommandImplicits {
stringToCommandFilter("set") -> { (_, argv) =>
val trigger = argv(1)
val response = argv.drop(2).mkString(" ") // Drop command + trigger
bot.redis.set("PLUGIN_SET_" + trigger, response)
Future { Some(bot.localize(LocalizableString.DONE)) }(ExecutionContext.global)
bot.redis.set("PLUGIN_SET_" + trigger, response).map { _ =>
Some(bot.localize(LocalizableString.DONE))
}(ExecutionContext.global)
}
)
override var messageHandlers: List[MessageHandler] = List(
{ msg: Message => Future {
msg.text.flatMap { text =>
{ msg: Message =>
msg.text.map { text =>
val firstWord = text.takeWhile(_ != ' ')
bot.redis.get("PLUGIN_SET_" + firstWord)
}
}(ExecutionContext.global) }
}.getOrElse(Future { None }(ExecutionContext.global))
}
)
}