~idmyn/bubbin_bot

0d8589eeef26d39eefb87c0adabf03c472dbd7c8 — David Mynors 18 days ago 727f467
Rename proj to bubbin_bot; tidy some things up
12 files changed, 39 insertions(+), 30 deletions(-)

M .dockerignore
A .env.example
M .gitignore
M Dockerfile
M README.rst
R transparency_bot/{__init__.py => __.py}
R transparency_bot/{__main__.py => __.py}
R transparency_bot/{bot.py => }
R transparency_bot/{img.py => }
M pyproject.toml
R tests/{test_transparency_bot.py => test_bubbin_bot.py}
D token.txt
M .dockerignore => .dockerignore +3 -4
@@ 1,10 1,9 @@
.git
.gitignore
.git-crypt
.gitattributes
.pytest_cache
pyproject.toml
poetry.lock
**/__pycache__/
**/__pycache__
tests
token.txt
.env
.envrc

A .env.example => .env.example +1 -0
@@ 0,0 1,1 @@
TOKEN=

M .gitignore => .gitignore +3 -0
@@ 0,0 1,3 @@
.env
.envrc
**/__pycache__

M Dockerfile => Dockerfile +1 -2
@@ 7,5 7,4 @@ RUN pip install -r requirements.txt

COPY . .

ENV DEPLOY=docker
CMD [ "python", "-m", "transparency_bot" ]
CMD [ "python", "-m", "bubbin_bot" ]

M README.rst => README.rst +21 -6
@@ 1,9 1,14 @@
transparency_bot
================
bubbin_bot
==========

This is a Telegram bot that listens for messages containing images, resizes them
if necessary (to fit the Telegram sticker restriction of 512 pixels), replaces
white pixels with transparency, and returns the modified image to the sender.
This is a Telegram bot that serves as a handy interface for a couple of
utilities. If sent an image, it'll resize it if necessary (to fit the Telegram
sticker restriction of 512 pixels), replace white pixels with transparency, and
return the modified image to the sender. If sent a pdf, it'll process the pages
with Python-tesseract_ and return a pdf that can be searched and highlighted
(WIP).

.. _Python-tesseract: https://github.com/madmaze/pytesseract

commands
--------


@@ 12,4 17,14 @@ commands

  poetry run bot
  poetry run pytest
  poetry run export  # writes dependencies to requirements.txt for pip in Dockerfile

deployment
----------

A dockerfile is provided in this repo, but you'll want to make sure that you
have an up-to-date requirements file before you build the image! To refresh the
requirements.txt file, run this command:

.. code:: bash

  poetry export -f requirements.txt -o requirements.txt

R transparency_bot/__init__.py => bubbin_bot/__init__.py +0 -0

R transparency_bot/__main__.py => bubbin_bot/__main__.py +0 -0

R transparency_bot/bot.py => bubbin_bot/bot.py +6 -13
@@ 7,15 7,8 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s
                    level=logging.INFO)


def get_token():
    if os.getenv('DEPLOY') == 'docker':
        return os.getenv('TOKEN')
    else:
        return open('token.txt', 'r').read().rstrip()


token = get_token()
whitelist = ['idmyn', 'bubbin']
TOKEN = os.getenv('TOKEN')
ALLOWLIST = ['idmyn', 'bubbin']


def start(update, context):


@@ 27,17 20,17 @@ def image(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="nice pic")

    if update.effective_chat.username not in whitelist:
    if update.effective_chat.username not in ALLOWLIST:
        return  # I don't want to process images from strangers

    biggestImage = max(update.message.photo, key=lambda x:x['file_size'])
    # ^ https://stackoverflow.com/a/5326622/10314380
    file_id = biggestImage.file_id

    file_path_url = 'https://api.telegram.org/bot{0}/getFile?file_id={1}'.format(token, file_id)
    file_path_url = 'https://api.telegram.org/bot{0}/getFile?file_id={1}'.format(TOKEN, file_id)
    r = requests.get(file_path_url)
    file_path = r.json()['result']['file_path']
    file_url = 'https://api.telegram.org/file/bot{0}/{1}'.format(token, file_path)
    file_url = 'https://api.telegram.org/file/bot{0}/{1}'.format(TOKEN, file_path)
    image = requests.get(file_url)

    bytes = img.white_to_transparent(image.content)


@@ 50,7 43,7 @@ def image(update, context):
def run():
    print("running...")

    updater = Updater(token, use_context=True)
    updater = Updater(TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    start_handler = CommandHandler('start', start)

R transparency_bot/img.py => bubbin_bot/img.py +0 -0

M pyproject.toml => pyproject.toml +3 -4
@@ 1,12 1,11 @@
[tool.poetry]
name = "transparency-bot"
name = "bubbin_bot"
version = "0.1.0"
description = ""
authors = ["idmyn <hello@davidmyno.rs>"]
authors = ["David Mynors <hello@davidmyno.rs>"]

[tool.poetry.scripts]
bot = "transparency_bot.bot:run"
export = "poetry export -f requirements.txt -o requirements.txt"
bot = "bubbin_bot.bot:run"

[tool.poetry.dependencies]
python = "^3.8"

R tests/test_transparency_bot.py => tests/test_bubbin_bot.py +1 -1
@@ 1,4 1,4 @@
from transparency_bot import img
from bubbin_bot import img


def test_calc_dimensions():

D token.txt => token.txt +0 -0