-
Notifications
You must be signed in to change notification settings - Fork 0
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
drui9
committed
Jun 11, 2024
1 parent
9609efe
commit 57db187
Showing
11 changed files
with
67 additions
and
228 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,21 +1,2 @@ | ||
env := .venv | ||
deps := requirements.txt | ||
|
||
run: | ||
@clear;./$(env)/bin/python start.py | ||
|
||
build: | ||
@./$(env)/bin/pip install build;./$(env)/bin/python -m build | ||
|
||
clean: | ||
@rm -rf dist build *.egg-info **/__pycache__/ | ||
|
||
stable: clean build | ||
git push;git checkout releases;git merge main;git push;twine upload dist/*;git checkout main; | ||
|
||
$(env): $(deps) | ||
python -m venv $@ | ||
|
||
install: $(env) | ||
@./$(env)/bin/pip install -r $(deps) | ||
|
||
.venv\Scripts\python start.py |
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,21 @@ | ||
env := .venv | ||
deps := requirements.txt | ||
|
||
run: | ||
@clear;./$(env)/bin/python start.py | ||
|
||
build: | ||
@./$(env)/bin/pip install build;./$(env)/bin/python -m build | ||
|
||
clean: | ||
@rm -rf dist build *.egg-info **/__pycache__/ | ||
|
||
stable: clean build | ||
git push;git checkout releases;git merge main;git push;twine upload dist/*;git checkout main; | ||
|
||
$(env): $(deps) | ||
python -m venv $@ | ||
|
||
install: $(env) | ||
@./$(env)/bin/pip install -r $(deps) | ||
|
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 |
---|---|---|
@@ -1,74 +1,18 @@ | ||
import os | ||
import time | ||
import queue | ||
from autogram.base import Bot | ||
from requests.exceptions import ConnectionError | ||
|
||
|
||
# -- | ||
class Autogram(Bot): | ||
def __init__(self, config) -> None: | ||
"""Initialize parent object""" | ||
self.update_handler = None | ||
super().__init__(config) | ||
return | ||
|
||
def addHandler(self, function): | ||
self.update_handler = function | ||
return function | ||
|
||
def prepare(self): | ||
"""Confirm auth through getMe(), then check update methods""" | ||
res = self.getMe() | ||
if not res.ok: | ||
self.do_err(msg=str(res.json())) | ||
self.webhook_addr = self.config.get('AUTOGRAM_ENDPOINT') or os.getenv('AUTOGRAM_ENDPOINT') # noqa: E501 | ||
if self.webhook_addr: | ||
res = self.setWebhook(self.webhook_addr) | ||
if not res.ok: | ||
self.do_err(msg='/setWebhook failed!') | ||
else: | ||
res = self.deleteWebhook() | ||
if not res.ok: | ||
self.do_err('/deleteWebhook failed!') | ||
else: | ||
self.short_poll() | ||
return | ||
return super().__init__(config) | ||
|
||
def start(self): | ||
"""Launch the bot""" | ||
try: | ||
self.prepare() | ||
while not self.terminate.is_set(): | ||
try: | ||
if not self.update_handler: | ||
time.sleep(5) | ||
continue | ||
self.update_handler(self.updates.get()) | ||
except queue.Empty: | ||
continue | ||
except ConnectionError: | ||
self.terminate.set() | ||
self.logger.critical('Connection Error!') | ||
finally: | ||
self.shutdown() | ||
if (bot := self.getMe()).ok: | ||
print(self.poll().json()) | ||
|
||
|
||
def shutdown(self): | ||
"""Gracefully terminate the bot""" | ||
if self.terminate.is_set(): | ||
try: | ||
res = self.getWebhookInfo() | ||
if not res.ok: | ||
return | ||
if not res.json()['result']['url']: | ||
return | ||
except Exception: | ||
return | ||
# delete webhook and exit | ||
try: | ||
res = self.deleteWebhook() | ||
if not res.ok: | ||
raise RuntimeError() | ||
except Exception: | ||
self.logger.critical('/deleteWebhook failed!') | ||
finally: | ||
self.terminate.set() | ||
self.terminate.set() |
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
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
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
Empty file.
Empty file.
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "autogram" | ||
version = "3.4.5" | ||
version = "3.5.0" | ||
description = "An easily extensible telegram API wrapper" | ||
readme = "README.md" | ||
authors = [{ name = "droi9", email = "[email protected]" }] | ||
|
@@ -18,10 +18,8 @@ classifiers = [ | |
] | ||
keywords = ["telegram", "API", "wrapper"] | ||
dependencies = [ | ||
"SQLAlchemy==2.0.19", | ||
"loguru==0.7.0", | ||
"bottle==0.12.25", | ||
"requests==2.31.0", | ||
"loguru==0.7.2", | ||
"requests==2.32.3", | ||
] | ||
requires-python = ">=3.6" | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,2 @@ | ||
arrow>=1.3.0 | ||
loguru>=0.7.2 | ||
requests>=2.32.2 | ||
python-dotenv>=1.0.1 | ||
requests>=2.32.3 |
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 |
---|---|---|
@@ -1,49 +1,8 @@ | ||
""" | ||
@author: drui9 | ||
@config values: | ||
- update-endpoint: optional | ||
- ngrok-token: optional | ||
- bot-token: required | ||
- cava-auth: optional | ||
""" | ||
import os | ||
import requests | ||
from unittest import mock | ||
from autogram import Autogram | ||
from dotenv import load_dotenv | ||
from autogram.config import load_config | ||
|
||
def get_update_endpoint(validator_fn, inject_key=None): | ||
"""Get updates endpoint, silently default to telegram servers""" | ||
if key := os.getenv('ngrok-api-key', inject_key): | ||
header = {'Authorization': f'Bearer {key}', 'Ngrok-Version': '2'} | ||
try: | ||
def getter(): | ||
if os.getenv('TESTING') == '1': | ||
return 'http://localhost:8000' | ||
rep = requests.get('https://api.ngrok.com/tunnels', headers=header, timeout=6) | ||
if rep.ok and (out := validator_fn(rep.json())): | ||
return out | ||
return getter | ||
except Exception: | ||
raise | ||
return Autogram.api_endpoint | ||
|
||
# modify to select one ngrok tunnel from list of tunnels | ||
def select_tunnel(tunnels): | ||
for tunnel in tunnels['tunnels']: | ||
if tunnel['forwards_to'] == 'http://api:8000': | ||
return tunnel['public_url'] | ||
|
||
# launcher | ||
if __name__ == '__main__': | ||
load_dotenv() | ||
config = Autogram.cfg_template() | ||
with load_config(config): | ||
if ngrok_token := config.get('ngrok-token'): | ||
if getter := get_update_endpoint(select_tunnel, ngrok_token): | ||
config['update-endpoint'] = getter() | ||
bot = Autogram(config) | ||
bot.getter = getter # fn to get updated endpoint | ||
bot.loop() | ||
from autogram.config import load_config, Start | ||
|
||
#-- | ||
@Start(config_file='web-auto.json') | ||
def main(config): | ||
bot = Autogram(config) | ||
bot.start() |