Bot.scala 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. This file is part of karl-marx.
  3. karl-marx is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. karl-marx is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with karl-marx. If not, see <https://www.gnu.org/licenses/>.
  13. */
  14. package org.congressodeiradicali.karlmarx
  15. import cats.instances.future._
  16. import cats.syntax.functor._
  17. import com.bot4s.telegram.api.RequestHandler
  18. import com.bot4s.telegram.api.declarative.{Commands, whenOrElse}
  19. import com.bot4s.telegram.clients.ScalajHttpClient
  20. import com.bot4s.telegram.future.{Polling, TelegramBot}
  21. import com.bot4s.telegram.models.{ChatType, Message}
  22. import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
  23. import scala.concurrent.Future
  24. class Bot(val token: String) extends TelegramBot
  25. with Polling
  26. with Commands[Future] {
  27. LoggerConfig.factory = PrintLoggerFactory()
  28. // set log level, e.g. to TRACE
  29. LoggerConfig.level = LogLevel.TRACE
  30. val localizer = new Localizer("it_IT")
  31. override val client: RequestHandler[Future] = new ScalajHttpClient(token)
  32. def getLogger() = logger
  33. /**
  34. * Ensures a message is sent from a user and not from the chat
  35. * @param msg the message
  36. * @return a boolean that answers the question: is it an actual user?
  37. */
  38. def fromActualUser(msg: Message): Boolean =
  39. msg.from.isDefined
  40. /**
  41. * Ensures a message is from a group or super group
  42. * @param msg the message
  43. * @return answer to the question: is it a group?
  44. */
  45. def fromGroup(msg: Message): Boolean =
  46. msg.chat.`type` == ChatType.Group || msg.chat.`type` == ChatType.Supergroup
  47. /**
  48. * Ensures a message is from a chat administrator
  49. * @param msg the message
  50. * @return a boolean that answers the question: is it an administrator?
  51. */
  52. def fromAdmin(msg: Message): Boolean =
  53. if (fromActualUser(msg) && fromGroup(msg)) {
  54. val user = new BotUser(msg.from.get, msg.chat, this)
  55. user.isAdmin || user.isCreator
  56. } else {
  57. false
  58. }
  59. def canBan(msg: Message): Boolean =
  60. if (fromActualUser(msg) && fromGroup(msg)) {
  61. val user = new BotUser(msg.from.get, msg.chat, this)
  62. user.canBanUsers
  63. } else {
  64. false
  65. }
  66. onCommand("license") { implicit msg =>
  67. reply(" karl-marx is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n karl-marx is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with karl-marx. If not, see <https://www.gnu.org/licenses/>.").void
  68. }
  69. whenOrElse(onCommand("ban"), canBan) { implicit msg =>
  70. msg.replyToMessage match {
  71. case Some(message) => {
  72. message.from match {
  73. case Some(u) => {
  74. val user = new BotUser(u, message.chat, this)
  75. if (user.initSuccessful()) {
  76. if (!user.isAdmin && !user.isCreator) {
  77. user.ban
  78. reply(localizer.getString("ban.successful")).void
  79. } else reply(localizer.getString("ban.failed_ban_admin")).void
  80. } else reply(localizer.getString("ban.failed")).void
  81. }
  82. }
  83. }
  84. }
  85. } /*else*/ { implicit msg =>
  86. reply(localizer.getString("ban.unauthorized")).void
  87. }
  88. }