123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- 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 cats.instances.future._
- import cats.syntax.functor._
- import com.bot4s.telegram.api.RequestHandler
- import com.bot4s.telegram.api.declarative.{Commands, whenOrElse}
- import com.bot4s.telegram.clients.ScalajHttpClient
- import com.bot4s.telegram.future.{Polling, TelegramBot}
- import com.bot4s.telegram.models.{ChatType, Message}
- import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory}
- import scala.concurrent.Future
- class Bot(val token: String) extends TelegramBot
- with Polling
- with Commands[Future] {
- LoggerConfig.factory = PrintLoggerFactory()
- // set log level, e.g. to TRACE
- LoggerConfig.level = LogLevel.TRACE
- val localizer = new Localizer("it_IT")
- override val client: RequestHandler[Future] = new ScalajHttpClient(token)
- def getLogger() = logger
- /**
- * Ensures a message is sent from a user and not from the chat
- * @param msg the message
- * @return a boolean that answers the question: is it an actual user?
- */
- def fromActualUser(msg: Message): Boolean =
- msg.from.isDefined
- /**
- * Ensures a message is from a group or super group
- * @param msg the message
- * @return answer to the question: is it a group?
- */
- def fromGroup(msg: Message): Boolean =
- msg.chat.`type` == ChatType.Group || msg.chat.`type` == ChatType.Supergroup
- /**
- * Ensures a message is from a chat administrator
- * @param msg the message
- * @return a boolean that answers the question: is it an administrator?
- */
- def fromAdmin(msg: Message): Boolean =
- if (fromActualUser(msg) && fromGroup(msg)) {
- val user = new BotUser(msg.from.get, msg.chat, this)
- user.isAdmin || user.isCreator
- } else {
- false
- }
- def canBan(msg: Message): Boolean =
- if (fromActualUser(msg) && fromGroup(msg)) {
- val user = new BotUser(msg.from.get, msg.chat, this)
- user.canBanUsers
- } else {
- false
- }
- onCommand("license") { implicit msg =>
- 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
- }
- whenOrElse(onCommand("ban"), canBan) { implicit msg =>
- msg.replyToMessage match {
- case Some(message) => {
- message.from match {
- case Some(u) => {
- val user = new BotUser(u, message.chat, this)
- if (user.initSuccessful()) {
- if (!user.isAdmin && !user.isCreator) {
- user.ban
- reply(localizer.getString("ban.successful")).void
- } else reply(localizer.getString("ban.failed_ban_admin")).void
- } else reply(localizer.getString("ban.failed")).void
- }
- }
- }
- }
- } /*else*/ { implicit msg =>
- reply(localizer.getString("ban.unauthorized")).void
- }
- }
|