Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass token and chat_id through params on send #90

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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="[email protected]",
Expand Down
Empty file added telegram_send/__init__.py
Empty file.
38 changes: 27 additions & 11 deletions telegram_send.py → telegram_send/telegram_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions telegram_send/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.25.4"
1 change: 0 additions & 1 deletion version.py

This file was deleted.