Skip to content

Commit

Permalink
buitin handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorubin committed Jul 6, 2021
1 parent 048b501 commit 21504de
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[report]
omit =
setup.py
tests/fixtures/*
*/site-packages/*
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# lifeguard-telegram



[![SourceLevel](https://app.sourcelevel.io/github/LifeguardSystem/-/lifeguard-telegram.svg)](https://app.sourcelevel.io/github/LifeguardSystem/-/lifeguard-telegram)


Lifeguard Telegram Integration

4 changes: 4 additions & 0 deletions lifeguard_telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@


class LifeguardTelegramPlugin:
"""
Telegram Plugin
"""

def __init__(self, lifeguard_context):
self.lifeguard_context = lifeguard_context
init_updater()
Expand Down
36 changes: 27 additions & 9 deletions lifeguard_telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,50 @@


def init_updater():
"""
Init start polling
"""
CONTEXT["updater"] = Updater(LIFEGUARD_TELEGRAM_BOT_TOKEN, use_context=True)

load_bot_handlers()

CONTEXT["updater"].start_polling()
CONTEXT["updater"].idle()


def load_directory_with_handlers(module_prefix, bot_handler_file):
if bot_handler_file.endswith("_bot_handler.py"):
bot_handler_module_name = bot_handler_file.replace(".py", "")
logger.info("loading bot handler %s", bot_handler_module_name)

module = "{}.{}".format(module_prefix, bot_handler_module_name)
if module not in sys.modules:
__import__(module)


def load_bot_handlers():
"""
Load bot handlers from application path
"""
sys.path.append(LIFEGUARD_DIRECTORY)

for bot_handler_file in os.listdir(
os.path.join(os.path.dirname(os.path.realpath(__file__)), "bot_handlers")
):
load_directory_with_handlers(
"lifeguard_telegram.bot_handlers", bot_handler_file
)

if not os.path.exists(os.path.join(LIFEGUARD_DIRECTORY, "bot_handlers")):
return

for bot_handler_file in os.listdir(
os.path.join(LIFEGUARD_DIRECTORY, "bot_handlers")
):
if bot_handler_file.endswith("_bot_handler.py"):
bot_handler_module_name = bot_handler_file.replace(".py", "")
logger.info("loading bot handler %s", bot_handler_module_name)

module = "bot_handlers.%s" % (bot_handler_module_name)
if module not in sys.modules:
__import__(module)
load_directory_with_handlers("bot_handlers", bot_handler_file)


def bot_handler(command):
def bot_handler(command, load=True):
"""
Decorator to configure a bot handler
"""
Expand All @@ -50,7 +65,10 @@ def function_reference(decorated):
def wrapped(*args, **kwargs):
return decorated(*args, **kwargs)

CONTEXT["updater"].dispatcher.add_handler(CommandHandler(command, decorated))
if load:
CONTEXT["updater"].dispatcher.add_handler(
CommandHandler(command, decorated)
)
return wrapped

return function_reference
Empty file.
18 changes: 18 additions & 0 deletions lifeguard_telegram/bot_handlers/builtin_bot_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Builtin Handlers
"""
from lifeguard.repositories import ValidationRepository

from lifeguard_telegram.bot import bot_handler
from lifeguard_telegram.settings import LIFEGUARD_TELEGRAM_VALIDATIONS_HANDLER_ENABLED


@bot_handler("validations", load=LIFEGUARD_TELEGRAM_VALIDATIONS_HANDLER_ENABLED)
def recover_validations(update, _context):
"""
send all validations status
"""
for validation in ValidationRepository().fetch_all_validation_results():
update.message.reply_text(
"{}: {}".format(validation.validation_name, validation.status)
)
12 changes: 11 additions & 1 deletion lifeguard_telegram/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
"default": "",
"description": "Telegram bot token",
},
"LIFEGUARD_TELEGRAM_VALIDATIONS_HANDLER": {
"default": "true",
"description": "Enable telegram validations handler",
},
}
)

LIFEGUARD_TELEGRAM_BOT_TOKEN = SETTINGS_MANAGER.read_value("LIFEGUARD_TELEGRAM_BOT_TOKEN")
LIFEGUARD_TELEGRAM_BOT_TOKEN = SETTINGS_MANAGER.read_value(
"LIFEGUARD_TELEGRAM_BOT_TOKEN"
)
LIFEGUARD_TELEGRAM_VALIDATIONS_HANDLER_ENABLED = (
SETTINGS_MANAGER.read_value("LIFEGUARD_TELEGRAM_VALIDATIONS_HANDLER_ENABLED")
== "true"
)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lifeguard==0.0.7
lifeguard==0.0.27
python-telegram-bot==13.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="lifeguard-telegram",
version="0.0.2",
version="0.0.4",
url="https://github.com/LifeguardSystem/lifeguard-telegram",
author="Diego Rubin",
author_email="[email protected]",
Expand Down
20 changes: 20 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest

from unittest.mock import patch, MagicMock
from lifeguard_telegram.bot import init_updater

MOCK_CONTEXT = {}


class BotTest(unittest.TestCase):
@patch("lifeguard_telegram.bot.CONTEXT", MOCK_CONTEXT)
@patch("lifeguard_telegram.bot.Updater")
def test_init_updater(self, mock_updater):
instance_updater = MagicMock(name="updater")
mock_updater.return_value = instance_updater

init_updater()

mock_updater.assert_called_with("", use_context=True)
MOCK_CONTEXT["updater"].start_polling.assert_called()
MOCK_CONTEXT["updater"].idle.assert_called()

0 comments on commit 21504de

Please sign in to comment.