<commit_before>"""
`notipyserver` - User-Notification-Framework server

Provides functions to send messages
to Telegram clients.

:copyright: (c) by <NAME>
:license: MIT, see LICENSE for details
"""
import logging

from telegram import Bot, TelegramError, ParseMode

from notipyserver.config import Config

from .usermanager import get_user_chat_id, get_group_chat_id
from .errors import TelegramConfigurationError


def get_telegram_bot():
    """
    Creates a telegram bot instance.

    Returns:
        telegram.Bot: The bot instance.

    Raises:
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    logger = logging.getLogger()
    try:
        return Bot(Config().telegram_token)
    except (FileNotFoundError, ValueError, TelegramError) as exc:
        logger.error("Telegram token not present or invalid: '%s'", str(exc))
        raise TelegramConfigurationError("Telegram token not "
                                         "present or invalid.")


def send(recipient, message, **_):
    """
    Sends the given message to the given
    recipient over the telegram api.

    Args:
        recipient (str): The recipient of the message.
        message (str): The message to be sent.
    Raises:
        UserNotRegisteredError: If the recipient is not registered.
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    bot = get_telegram_bot()
    chat_id = get_user_chat_id(recipient)
    bot.sendMessage(chat_id=chat_id, text=message,
                    parse_mode=ParseMode.MARKDOWN)


def send_to_group(recipient, message, **_):
    """
    Sends the given message to the given
    group recipient over the telegram api.

    Args:
        recipient (str): The recipient of the message.
        message (str): The message to be sent.
    Raises:
        GroupNotRegisteredError: If the recipient is not registered.
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    bot = get_telegram_bot()

    chat_id = get_group_chat_id(recipient)
    bot.sendMessage(chat_id=chat_id, text=message)<commit_msg>Support markdown in telegram group messages<commit_after>"""
`notipyserver` - User-Notification-Framework server

Provides functions to send messages
to Telegram clients.

:copyright: (c) by <NAME>
:license: MIT, see LICENSE for details
"""
import logging

from telegram import Bot, TelegramError, ParseMode

from notipyserver.config import Config

from .usermanager import get_user_chat_id, get_group_chat_id
from .errors import TelegramConfigurationError


def get_telegram_bot():
    """
    Creates a telegram bot instance.

    Returns:
        telegram.Bot: The bot instance.

    Raises:
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    logger = logging.getLogger()
    try:
        return Bot(Config().telegram_token)
    except (FileNotFoundError, ValueError, TelegramError) as exc:
        logger.error("Telegram token not present or invalid: '%s'", str(exc))
        raise TelegramConfigurationError("Telegram token not "
                                         "present or invalid.")


def send(recipient, message, **_):
    """
    Sends the given message to the given
    recipient over the telegram api.

    Args:
        recipient (str): The recipient of the message.
        message (str): The message to be sent.
    Raises:
        UserNotRegisteredError: If the recipient is not registered.
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    bot = get_telegram_bot()
    chat_id = get_user_chat_id(recipient)
    bot.sendMessage(chat_id=chat_id, text=message,
                    parse_mode=ParseMode.MARKDOWN)


def send_to_group(recipient, message, **_):
    """
    Sends the given message to the given
    group recipient over the telegram api.

    Args:
        recipient (str): The recipient of the message.
        message (str): The message to be sent.
    Raises:
        GroupNotRegisteredError: If the recipient is not registered.
        TelegramConfigurationError: If the telegram bot was not properly
            configured.
    """
    bot = get_telegram_bot()

    chat_id = get_group_chat_id(recipient)
    bot.sendMessage(chat_id=chat_id, text=message,
                    parse_mode=ParseMode.MARKDOWN)
{'fraction_non_alphanumeric': 0.07449921224397929, 'fraction_numerical': 0.0, 'mean_word_length': 2.850953206239168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '971517', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1208, 'n_words': 403}