-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jerimumhs/feature/commands_module
Bot commands as python modules
- Loading branch information
Showing
13 changed files
with
197 additions
and
196 deletions.
There are no files selected for viewing
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
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) |
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
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 | ||
] |
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
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))) |
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
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)) |
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
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) |
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
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)))) |
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
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))) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.