diff --git a/README.md b/README.md index f18470a..7e74041 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,17 @@ directly from other Python programs: `import telegram_send`. Look at the [documentation]: https://www.rahielkasim.com/telegram-send/docs/api/ +```python +from telegram_send import send + +config = { + 'token': 'your_bot_token_here', + 'chat_id': 'chat_id_here', +} + +send(messages=['Some text here'], conf=config) +``` + ## Cron job output Cron has a built-in feature to send the output of jobs via mail. In this example diff --git a/setup.py b/setup.py index 551da72..2502b17 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 from setuptools import setup -from version import __version__ - +from telegram_send.version import __version__ try: import pypandoc @@ -23,9 +22,8 @@ license="GPLv3+", python_requires=">=3.5", - py_modules=["telegram_send", "version"], + packages=['telegram_send'], install_requires=["python-telegram-bot>=12.1.1", "colorama", "appdirs"], - entry_points={"console_scripts": ["telegram-send=telegram_send:main"]}, author="Rahiel Kasim", author_email="rahielkasim@gmail.com", diff --git a/telegram_send/__init__.py b/telegram_send/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/telegram_send.py b/telegram_send/telegram_send.py similarity index 97% rename from telegram_send.py rename to telegram_send/telegram_send.py index a64a88c..81848c1 100644 --- a/telegram_send.py +++ b/telegram_send/telegram_send.py @@ -31,7 +31,7 @@ from telegram.constants import MAX_MESSAGE_LENGTH from appdirs import AppDirs -from version import __version__ +from .version import __version__ try: import readline @@ -143,6 +143,31 @@ def main(): raise(e) +def get_config(conf): + """Get config from config file or from dict.""" + if isinstance(conf, dict): + try: + return conf['token'], conf['chat_id'] + except KeyError as e: + raise ConfigError("Missing options in config dict: {}".format(e)) + elif isinstance(conf, str): + conf = expanduser(conf) + else: + conf = get_config_path() + + config = configparser.ConfigParser() + if not config.read(conf) or not config.has_section("telegram"): + raise ConfigError("Config not found") + missing_options = set(["token", "chat_id"]) - set(config.options("telegram")) + if len(missing_options) > 0: + raise ConfigError("Missing options in config: {}".format(", ".join(missing_options))) + config = config["telegram"] + token = config["token"] + chat_id = int(config["chat_id"]) if config["chat_id"].isdigit() else config["chat_id"] + + return token, chat_id + + def send(*, messages=None, files=None, images=None, stickers=None, animations=None, videos=None, audios=None, captions=None, locations=None, conf=None, parse_mode=None, silent=False, disable_web_page_preview=False, @@ -182,16 +207,7 @@ def send(*, disable_web_page_preview (bool): Disables web page previews for all links in the messages. timeout (int|float): The read timeout for network connections in seconds. """ - conf = expanduser(conf) if conf else get_config_path() - config = configparser.ConfigParser() - if not config.read(conf) or not config.has_section("telegram"): - raise ConfigError("Config not found") - missing_options = set(["token", "chat_id"]) - set(config.options("telegram")) - if len(missing_options) > 0: - raise ConfigError("Missing options in config: {}".format(", ".join(missing_options))) - config = config["telegram"] - token = config["token"] - chat_id = int(config["chat_id"]) if config["chat_id"].isdigit() else config["chat_id"] + token, chat_id = get_config(conf) request = telegram.utils.request.Request(read_timeout=timeout) bot = telegram.Bot(token, request=request) diff --git a/telegram_send/version.py b/telegram_send/version.py new file mode 100644 index 0000000..4e32d0c --- /dev/null +++ b/telegram_send/version.py @@ -0,0 +1 @@ +__version__ = "0.25.4" diff --git a/version.py b/version.py deleted file mode 100644 index f50820e..0000000 --- a/version.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "0.25"