Skip to content

Commit

Permalink
Merge pull request #16 from jerimumhs/feature/commands_module
Browse files Browse the repository at this point in the history
Bot commands as python modules
  • Loading branch information
rodrigondec authored Oct 5, 2019
2 parents 5acb6a4 + 82c0b11 commit 163a651
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 196 deletions.
11 changes: 4 additions & 7 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from core import BotTelegramCore
from mixins import (BaseCommandsBotMixin, CallbackBotMixin, ErrorBotMixin,
MessageBotMixin, StickerBotMixin)
from commands import handlers


class JerimumBot(BaseCommandsBotMixin, CallbackBotMixin, ErrorBotMixin,
MessageBotMixin, StickerBotMixin):
class JerimumBot(BotTelegramCore):
"""Bot Controller"""

def config_handlers(self):
for BaseClass in self.__class__.__bases__:
assert issubclass(BaseClass, BotTelegramCore)
BaseClass.config_handlers(self)
for config_handler in handlers:
config_handler(self)
14 changes: 14 additions & 0 deletions commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from commands.base import config_handlers as base_handler
from commands.callback import config_handlers as callback_handler
from commands.error import config_handlers as error_handler
from commands.message import config_handlers as message_handler
from commands.sticker import config_handlers as sticker_handler


handlers = [
base_handler,
callback_handler,
error_handler,
message_handler,
sticker_handler
]
43 changes: 43 additions & 0 deletions commands/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging

from telegram.ext import CommandHandler

from core import BotTelegramCore, adm_verify
from messages import START, HELP, DESCRIPTION, RULES_COMPLETE


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def description(bot, update):
"""Send a message with the group description."""
if adm_verify(update):
update.message.reply_text(DESCRIPTION)
else:
bot.sendMessage(
chat_id=update.message.from_user.id,
text=DESCRIPTION)


def rules(bot, update):
"""Send a message with the group rules."""
if adm_verify(update):
update.message.reply_text(RULES_COMPLETE)
else:
bot.sendMessage(
chat_id=update.message.from_user.id,
text=RULES_COMPLETE)


def config_handlers(instance: BotTelegramCore):
logging.info('Configurando comandos base do bot...')
dp = instance.updater.dispatcher

dp.add_handler(CommandHandler("regras", rules))
dp.add_handler(CommandHandler("descricao", description))

dp.add_handler(CommandHandler("start", lambda bot, update: update.message.reply_text(START)))
dp.add_handler(CommandHandler("ajuda", lambda bot, update: update.message.reply_text(HELP)))
32 changes: 32 additions & 0 deletions commands/callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging

from telegram.ext import CallbackQueryHandler

from core import BotTelegramCore
from messages import RULES_BIT


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def handle_callback(bot, update):
query = update.callback_query

if query.data == "rules":
bot.answer_callback_query(
callback_query_id=query.id,
text=RULES_BIT,
show_alert=True
)
elif query.data == "site":
bot.answer_callback_query(
callback_query_id=query.id
)


def config_handlers(instance: BotTelegramCore):
logging.info('Configurando callback handler do bot...')
instance.updater.dispatcher.add_handler(CallbackQueryHandler(handle_callback))
25 changes: 25 additions & 0 deletions commands/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging

from core import BotTelegramCore
from messages import ERROR_BLOCKED, ERROR_INITIATE


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def error(bot, update, err):
"""Log Errors caused by Updates."""
if err.message == "Forbidden: bot can't initiate conversation with a user":
update.message.reply_text(ERROR_INITIATE)
elif err.message == "Forbidden: bot was blocked by the user":
update.message.reply_text(ERROR_BLOCKED)
else:
logger.warning('Update "%s" caused error "%s"', update, err)


def config_handlers(instance: BotTelegramCore):
logging.info('Configurando error handler do bot...')
instance.updater.dispatcher.add_error_handler(error)
60 changes: 60 additions & 0 deletions commands/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import MessageHandler, Filters

from core import BotTelegramCore
from messages import BYE, WELCOME


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def welcome(bot, update):
"""Send a message when a new user join the group."""
welcome_message = WELCOME.format(full_name=update.message.new_chat_members[0].full_name)

keyboard = [
[
InlineKeyboardButton(
"Resumo das regras!",
callback_data='rules')
],

[
InlineKeyboardButton(
"Nosso site!",
callback_data='site',
url="http://jerimumhacker.space/"),

InlineKeyboardButton(
"Nosso Facebook!",
callback_data='site',
url="https://www.facebook.com/JerimumHS/")
],

[
InlineKeyboardButton(
"Nosso GitHub!",
callback_data='site',
url="https://github.com/jerimumhs/")
]
]

reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(welcome_message, reply_markup=reply_markup)


def config_handlers(instance: BotTelegramCore):
logging.info('Configurando message handlers do bot...')

instance.updater.dispatcher.add_handler(MessageHandler(
Filters.status_update.new_chat_members, welcome))

instance.updater.dispatcher.add_handler(MessageHandler(
Filters.status_update.left_chat_member,
lambda bot, update: update.message.reply_text(
BYE.format(full_name=update.message.left_chat_member.full_name))))
19 changes: 19 additions & 0 deletions commands/sticker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

from telegram.ext import (CommandHandler)

from core import BotTelegramCore


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


def config_handlers(instance: BotTelegramCore):
logging.info('Configurando comandos de sticker do bot...')

instance.updater.dispatcher.add_handler(CommandHandler("xinga", lambda bot, update: bot.send_sticker(
sticker="CAADAQADCgEAAmOWFQq4zU4TMS08AwI",
chat_id=update.message.chat_id)))
5 changes: 0 additions & 5 deletions mixins/__init__.py

This file was deleted.

44 changes: 0 additions & 44 deletions mixins/base_commands.py

This file was deleted.

33 changes: 0 additions & 33 deletions mixins/callback.py

This file was deleted.

26 changes: 0 additions & 26 deletions mixins/error.py

This file was deleted.

61 changes: 0 additions & 61 deletions mixins/message.py

This file was deleted.

Loading

0 comments on commit 163a651

Please sign in to comment.