-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdfa560
Showing
6 changed files
with
900 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,38 @@ | ||
# telegramgetbotip | ||
*Python3 telegram bot to retrieve public IP of the Telegram bot's host machine.* | ||
|
||
## Rationale: | ||
Sometimes a DynDNS is an overkill. This bot is designed to cover the use case where | ||
you just need the IP of a network. | ||
|
||
## Installation | ||
### Install with pip | ||
``` | ||
python3.8 -m pip install --user -U telegramgetbotip | ||
``` | ||
|
||
## Usage | ||
From shell: | ||
|
||
``` | ||
$ python3.8 telegramgetbotip -h | ||
usage: telegramgetbotip [-h] [-v] [-t TOKENFILE] [-i IDSFILE] [-d DELTA] | ||
Telegram Bot to get the public IP where the bot is hosted on. | ||
optional arguments: | ||
-h, --help show this help message and exit | ||
-v, --verbose Verbosity. | ||
-t TOKENFILE, --tokenfile TOKENFILE | ||
Bot token file. | ||
-i IDSFILE, --idsfile IDSFILE | ||
Allowed Telegram IDs file. | ||
-d DELTA, --delta DELTA | ||
Time between queries. | ||
``` | ||
|
||
From the interpreter: | ||
|
||
``` | ||
help(telegramgetbotip) | ||
``` |
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,38 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from setuptools import setup | ||
from pathlib import Path | ||
|
||
readme = Path("README.md").read_text() | ||
|
||
|
||
setup( | ||
name="telegramgetbotip", | ||
packages=["telegramgetbotip"], | ||
entry_points={ | ||
"console_scripts": [ | ||
"telegramgetbotip = telegramgetbotip.__main__:main" | ||
] | ||
}, | ||
version="0.1.0", | ||
license="GPL3", | ||
description="Python3 telegram bot to retrieve public IP " | ||
"of the Telegram bot's host machine.", | ||
long_description=readme, | ||
long_description_content_type="text/markdown", | ||
author="Carlos A. Planchón", | ||
author_email="bubbledoloresuruguay2@gmail.com", | ||
url="https://github.com/carlosplanchon/telegramgetbotip", | ||
download_url="https://github.com/carlosplanchon/" | ||
"telegramgetbotip/archive/v0.1.0.tar.gz", | ||
keywords=["telegram", "bot", "ip", "networking"], | ||
install_requires=[ | ||
"telepot" | ||
], | ||
classifiers=[ | ||
"Intended Audience :: Developers", | ||
"Topic :: Software Development :: Build Tools", | ||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", | ||
"Programming Language :: Python :: 3.8", | ||
], | ||
) |
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,3 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from telegramgetbotip.telegramgetbotip import TelegramGetBotIp |
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,51 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from telegramgetbotip.telegramgetbotip import TelegramGetBotIp | ||
|
||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser( | ||
description="Telegram Bot to get the public IP where the bot " | ||
"is hosted on." | ||
) | ||
# Argparse logic. | ||
parser.add_argument( | ||
"-v", "--verbose", | ||
help="Verbosity.", | ||
action="store_true", | ||
dest="verbose", | ||
default=False | ||
) | ||
parser.add_argument( | ||
"-t", "--tokenfile", | ||
type=str, | ||
help=f"Bot token file.", | ||
dest="tokenfile" | ||
) | ||
parser.add_argument( | ||
"-i", "--idsfile", | ||
type=str, | ||
help=f"Allowed Telegram IDs file.", | ||
dest="idsfile" | ||
) | ||
parser.add_argument( | ||
"-d", "--delta", | ||
type=int, | ||
help=f"Time between queries.", | ||
dest="delta", | ||
default=-1 | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
bot = TelegramGetBotIp() | ||
if args.tokenfile and args.idsfile: | ||
bot.start_bot( | ||
bot_token_file=Path(args.tokenfile), | ||
allowed_telegram_ids_file=Path(args.idsfile), | ||
time_between_queries=args.delta, | ||
verbose=args.verbose | ||
) |
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,96 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from telepot.loop import MessageLoop | ||
|
||
from requests import get | ||
from telepot import Bot | ||
|
||
from pathlib import Path | ||
from pprint import pprint | ||
from time import sleep, time | ||
|
||
from typing import Any, Dict | ||
|
||
|
||
class TelegramGetBotIp: | ||
""" | ||
Telegram bot to retrieve public IP of | ||
the Telegram bot's host machine. | ||
""" | ||
def __init__(self): | ||
self.queries_log = {} | ||
|
||
def message_handler(self, msg: Dict[Any, Any]) -> None: | ||
""" | ||
This message handler get the public IP of the host if | ||
the ID of the message's sender is in allowed_telegram_ids. | ||
:param msg: Dict[Any, Any]: Message to be processed. | ||
""" | ||
resp = None | ||
query_enabled = False | ||
chat_id = msg["chat"]["id"] | ||
if self.verbose: | ||
pprint(msg) | ||
if chat_id in self.allowed_telegram_ids: | ||
if self.time_between_queries == -1: | ||
query_enabled = True | ||
else: | ||
if chat_id not in self.queries_log: | ||
query_enabled = True | ||
else: | ||
if time() - self.queries_log[ | ||
chat_id] > self.time_between_queries: | ||
query_enabled = True | ||
else: | ||
query_enabled = False | ||
last_query = self.queries_log[chat_id] | ||
time_since_last_query = time() - last_query | ||
needs_to_wait = self.time_between_queries\ | ||
- time_since_last_query | ||
resp = f"Wait {int(needs_to_wait)} to query again." | ||
|
||
if query_enabled: | ||
try: | ||
resp = get("http://ip.42.pl/short").text | ||
self.queries_log[chat_id] = time() | ||
except Exception: | ||
resp = "ERR" | ||
|
||
if resp: | ||
if self.verbose: | ||
pprint(f"Resp: {resp}") | ||
|
||
if msg["text"].lower() == "ip": | ||
self.bot.sendMessage(chat_id, resp) | ||
return None | ||
|
||
def start_bot( | ||
self, | ||
bot_token_file: Path, | ||
allowed_telegram_ids_file: Path, | ||
time_between_queries: int = -1, | ||
verbose: bool = False | ||
): | ||
""" | ||
Start bot and keep it running in its own thread. | ||
:param bot_token_file: Path: File where the bot token is located. | ||
:param allowed_telegram_ids_file: Path: Allowed IDs file. | ||
:param time_between_queries: int: Time between user queries | ||
to avoid spamming the service. -1 to disable it. (Default value = -1) | ||
:param verbose: bool: Verbosity. (Default value = False) | ||
""" | ||
self.bot_token = Path(bot_token_file).read_text() | ||
self.allowed_telegram_ids = [int(i) for i in Path( | ||
allowed_telegram_ids_file).read_text().split("\n")] | ||
|
||
self.time_between_queries = time_between_queries | ||
self.verbose = verbose | ||
|
||
self.bot = Bot(self.bot_token) | ||
MessageLoop(self.bot, self.message_handler).run_as_thread() | ||
while True: | ||
sleep(100000000) |