From 9b982a611510d4f1bf79f17acfe18c3ecc45fe2d Mon Sep 17 00:00:00 2001 From: Pedro Rey Anca Date: Fri, 23 Aug 2024 01:49:51 +0200 Subject: [PATCH 1/5] Remove as it is unnecessary --- setup.py | 116 ------------------------------------------------------- 1 file changed, 116 deletions(-) delete mode 100644 setup.py diff --git a/setup.py b/setup.py deleted file mode 100644 index 9507d43..0000000 --- a/setup.py +++ /dev/null @@ -1,116 +0,0 @@ -import json -import os - -approved = False -avaliable_languages = ["es", "eng"] - -selection = input("Are you sure you are running this in the root of the repository? [Y/n] ") -if selection == "" or selection == "Y" or selection == "y": - print("[i] Perfect!") -else: - print("[!] Exiting...") - exit() - -try: - while not approved: - token = input("Enter the token @BotFather gave you: ") - bot_name = input("Enter a name for your bot: ") - log_channel_id = input("Id of the channel where all the events will be logged: ") - key_channel_id = input("Id of the channel in which it's members should be able to use the bot: ") - lang = input("Enter the language code you want [es/eng]: ") - while not lang in avaliable_languages: - print("Language not avaliable") - lang = input("Enter the language code you want [es/eng]: ") - gpio_pin = input("Enter the number of the GPIO pin you have your relay connected to. 17, for example. Be careful: ") - btn_press_time = input("Enter the time(in seconds) the relay should be open when simulating a button pressing on your remote.(Recommended and default: 0.5): ") - if btn_press_time == "": - btn_press_time = 0.5 - else: - try: - btn_press_time = int(btn_press_time) - except: - try: - btn_press_time = float(btn_press_time) - except: - pass - while not isinstance(btn_press_time, int) and not isinstance(btn_press_time, float): - print("Incorrect input. Be sure to use no spaces and for decimal numbers use \".\" instead of \",\"") - btn_press_time = input("Enter the time(in seconds) the relay should be open when simulating a button pressing on your remote.(Recommended and default: 0.5): ") - if btn_press_time == "": - btn_press_time = 0.5 - try: - btn_press_time = int(btn_press_time) - except: - try: - btn_press_time = float(btn_press_time) - except: - pass - btn_press_time = str(btn_press_time) - wait_to_close_time = input("Enter the time(in seconds) the door should wait before closing itself when using /open command.(Recommended and default: 60): ") - if wait_to_close_time == "": - wait_to_close_time = 60 - else: - try: - wait_to_close_time = int(wait_to_close_time) - except: - try: - wait_to_close_time = float(wait_to_close_time) - except: - pass - while not isinstance(wait_to_close_time, int) and not isinstance(wait_to_close_time, float): - print("Incorrect input. Be sure to use no spaces and for decimal numbers use \".\" instead of \",\"") - wait_to_close_time = input("Enter the time(in seconds) the door should wait before closing itself when using /open command.(Recommended and default: 60): ") - if wait_to_close_time == "": - wait_to_close_time = 60 - try: - wait_to_close_time = int(wait_to_close_time) - except: - try: - wait_to_close_time = float(wait_to_close_time) - except: - pass - wait_to_close_time = str(wait_to_close_time) - lock_file_path = input("Enter the path where the lockFile should be created and checked for existance. Be sure to have the neccesary permissions: ") - - print("Ok. Check if this information is ok:") - print("Token: [" + token + "]") - print("Bot name: [" + bot_name + "]") - print("Log channel id: [" + log_channel_id + "]") - print("Key channel id: [" + key_channel_id + "]") - print("Language: [" + lang + "]") - print("GPIO pin: [" + gpio_pin + "]") - print("Button press time: [" + btn_press_time + "]") - print("Wait to close time: [" + wait_to_close_time + "]") - print("Lock file path: [" + lock_file_path + "]") - selection = input("Is this correct [S/n] ") - if selection == "" or selection == "S" or selection == "s": - approved = True - else: - approved = False - - print("[i] Creating config file...") - data = { - "telegram_bot_token": token, - "telegram_log_channel_id": int(log_channel_id), - "telegram_key_channel_id": int(key_channel_id), - "telegram_bot_name": bot_name, - "language": lang, - "relay_gpio_pin": int(gpio_pin), - "button_press_time": float(btn_press_time), - "wait_to_close_time": float(wait_to_close_time), - "lock_file_path": lock_file_path - } - with open("config.json", "w") as f: - json.dump(data, f) - - print("[i] Creating audios dir if it doesn't exist...") - if not os.path.isdir('./audios'): - os.mkdir("audios") - - print("[i] Succesfull!") - print("[i] Keep in mind that all this variables can be changed whenever you want at config.json.") - print("[i] Bye!") - -except Exception as e: - print("[E] There was an error! vvv\n") - print(e) From 4ba16e43f546d78c1ae0b891f3fc2f3c5de73e06 Mon Sep 17 00:00:00 2001 From: Pedro Rey Anca Date: Fri, 23 Aug 2024 01:49:58 +0200 Subject: [PATCH 2/5] Add example config --- config.example.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 config.example.json diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..d960445 --- /dev/null +++ b/config.example.json @@ -0,0 +1,17 @@ +{ + "config": { + "telegram": { + "bot_token": "123456:ABCDEFGHIverysecuretoken", + "log_channel_id": -123456, + "key_channel_id": -654321, + "bot_name": "Garage Door", + "language": "es" + }, + "door-dumb-api": { + "base_url": "http://arespi.local:8080", + "token": "thisisaverysecuretoken", + "door_id": "garageDoor", + "wait_to_close_time": 60 + } + } +} \ No newline at end of file From a91c26bcc09e553077eca1cd0e91b61381ff09c8 Mon Sep 17 00:00:00 2001 From: Pedro Rey Anca Date: Fri, 23 Aug 2024 01:50:09 +0200 Subject: [PATCH 3/5] Clean Dockerfile --- Dockerfile | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index afd6d39..bc94991 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,12 @@ -FROM balenalib/rpi-raspbian:latest +FROM python:3.11 -RUN apt-get -q update && \ - apt-get -qy install \ - python3 python3-pip python3-dev \ - gcc make \ - python3-pygame \ - openssl +COPY requirements.txt / +RUN pip3 install -r /requirements.txt -COPY ./requirements.txt /requirements.txt +RUN rm /requirements.txt -WORKDIR / - -RUN pip3 install setuptools - -RUN pip3 install -r requirements.txt - -COPY . / +COPY . /app +WORKDIR /app ENTRYPOINT [ "python3" ] CMD [ "main.py" ] \ No newline at end of file From 114d099df6c87985f8ecd4f188bc913ded7bd79f Mon Sep 17 00:00:00 2001 From: Pedro Rey Anca Date: Fri, 23 Aug 2024 01:50:26 +0200 Subject: [PATCH 4/5] Fix typos --- config.py | 8 ++++---- door.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index a3aff45..5066fba 100644 --- a/config.py +++ b/config.py @@ -26,7 +26,7 @@ def _load_config(env_var: str, default: str): key_channel_id = _config["telegram"]["key_channel_id"] bot_name = _config["telegram"]["bot_name"] language = _config["telegram"]["language"] -dumbdoorapi_base_url = _config["dumb-door-api"]["base_url"] -dumbdoorapi_token = _config["dumb-door-api"]["token"] -door_id = _config["dumb-door-api"]["door_id"] -wait_to_close_time = _config["dumb-door-api"].get("wait_to_close_time") +doordumbapi_base_url = _config["door-dumb-api"]["base_url"] +doordumbapi_token = _config["door-dumb-api"]["token"] +door_id = _config["door-dumb-api"]["door_id"] +wait_to_close_time = _config["door-dumb-api"].get("wait_to_close_time") diff --git a/door.py b/door.py index 1dfe977..9ade9e7 100644 --- a/door.py +++ b/door.py @@ -1,6 +1,6 @@ import requests -from config import dumbdoorapi_base_url, dumbdoorapi_token, door_id, wait_to_close_time +from config import doordumbapi_base_url, doordumbapi_token, door_id, wait_to_close_time class DoorException(Exception): From 0303c44d9e672b9f98b97a5f1bf724593dbe82c3 Mon Sep 17 00:00:00 2001 From: Pedro Rey Anca Date: Fri, 23 Aug 2024 01:50:36 +0200 Subject: [PATCH 5/5] Improve README --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 34c600a..d2256f8 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,52 @@ # dooropener-telegram -Open your door with a telegram bot. This is programmed in python3. And it's avaliable in english and spanish. +A client for [door-dumb-api](https://codeberg.org/peprolinbot/door-dumb-api) that allows to use a Telegram bot to simply open the door. The bot used to do a few more things (check the `old` branch if interested), but if you want anything more advanced, you should check [Home assistant](https://www.home-assistant.io/) as this is meant to be extremly simple (I run it at my grandparents' country house, where there are limited resources). -## HW Setup -You need a Raspberry pi with a Picamera, a speaker, and a relay connected to the button in the remote of yor door. Just that. Write down the number of the GPIO pin where you put the relay. +## 🔧 Deploy it -## SW Setup -You need three things from telgram: A bot and two channels, one is used for logging the events and the other for adding the persons that should be able to use the bot, you need the chatId from both channels. Now clone this repo. Then run the setup.py script and answer it's questions. For executing the bot run main.py +### 🐳 With Docker (Recommended) -## Using -You can change whatever you want if you have a little knowdeledge on python, but as default this is very simple: `/open` will open the door for 60s and close it, and the opposite if it's closed; `/toggle` will open or close it, depending on it's actual state, but just that, no automagically closing. +This is quick, easy and simple (if everything is going to be on a Pi, you should make yourself a `docker-compose.yml`): -### This short documentation will be better in future. You can help if you want ;) \ No newline at end of file +1. First, you need a [door-dumb-api](https://codeberg.org/peprolinbot/door-dumb-api) instance. Check that README to learn how to spin one up + +2. Then, you can setup the basic configuration (check `config.example.json`). + +3. Then you can use this docker command: +```bash +docker run -d --name dooropener-telegram -v /tmp/config:/app/config ghcr.io/peprolinbot/dooropener-telegram +``` + +#### Environment Variables + +| Name | Description | +|-------------------|-----------------------------------------------------------------------------| +| `CONFIG_FILE` | Path to the json file with the basic config _(Default: "config/config.json")| + +#### Configuration values +- `telegram` + - `bot_token`: The token @BotFather gave you + - `log_channel_id`: Id of the channel where all the events will be logged + - `key_channel_id`: Id of the channel whose members should be able to use the bot + - `bot_name`: A name for your bot + - `language`: The language code you want the messages to be sent in (es/eng) +- `door-dumb-api` + - `base_url`: The base url of the door-dumb-api instance + - `token`: A token for door-dumb-api(check that README) + - `door_id`: The door_id that will be controlled by the bot + - `wait_to_close_time`: This is **optional**, if specified it will override the value specified for the door in door-dumb-api + +#### Build the image +```bash +git clone https://github.com/peprolinbot/dooropener-telegram +cd dooropener-telegram +docker build -t dooropener-telegram . +``` + +### 💪🏻 Without Docker +Only use this for development unless you know what you're doing. + +```bash +git clone https://github.com/peprolinbot/dooropener-telegram +cd dooropener-telegram +python3 main.py +``` \ No newline at end of file