Skip to content

Commit

Permalink
production checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
sp3rtah authored and drui9 committed Jun 1, 2024
0 parents commit 9609efe
Show file tree
Hide file tree
Showing 15 changed files with 814 additions and 0 deletions.
142 changes: 142 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.vscode
# C extensions
*.so

# config files
*.json
.env

# temp dir
Downloads/

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
__pycache__/
.env
venv/
files/
*.log
.venv
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 drui9

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions Makefile
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)

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<p style="text-align: center;">
<img src="https://raw.githubusercontent.com/drui9/autogram/main/autogram.png" align="middle" alt="Autogram">
<p>

## Installation :: Python3
`pip install autogram`

## `An efficient asyncronous Telegram bot API wrapper!`
Autogram is a telegram BOT API wrapper with focus on simplicity and performance.

## `Why AutoGram?`
I need a bot framework that makes it easy to administer control remotely.

## `Project TODOs`
- Plans to cover the entire telegram API methods.

### `footnotes`
- `Polling` can be implemented by the user, while feeding data to the bot through `bot.parseUpdate(...)`
- Don't run multiple bots with the same `TOKEN` as this will cause update problems
- Sending unescaped special characters when using MarkdownV2 will return HTTP400
- Have `fun` with whatever you're building `;)`

Binary file added autogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions autogram/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from collections import namedtuple
#
ChatActionTypes = namedtuple(
'ChatActions', [
'typing',
'photo',
'video',
'audio',
'document'
])
#
chat_actions = ChatActionTypes(
'typing',
'upload_photo',
'upload_video',
'upload_voice',
'upload_document'
)
#
from autogram.config import Start # noqa: E402
from autogram.app import Autogram # noqa: E402

__all__ = [
'Start', 'Autogram', 'chat_actions'
]
74 changes: 74 additions & 0 deletions autogram/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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

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()

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()
Loading

0 comments on commit 9609efe

Please sign in to comment.