From b6f9687e4dd0fa92b476ea2618c5bec7f325d0ed Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 18 May 2024 04:21:06 +0100 Subject: [PATCH] Update telegram library, add docs --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++-- campcam.py | 47 +++++++++++++++++++++++++++++----------------- requirements.txt | 17 +++++++++++++++++ secrets.py.example | 1 + 4 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 requirements.txt create mode 100644 secrets.py.example diff --git a/README.md b/README.md index 104c32d..25aa128 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,50 @@ This lets my deranged friend monitor his drunken campground shenanigans from a Telegram bot. +## Usage + +Add @robbcampbot to your Telegram group. + +Send the `/campcam` command. + ## Installation ``` -$ pip install --force-reinstall 'python-telegram-bot==v13.15' 'picamera' +$ sudo raspi-config # enable the camera +$ sudo apt install python3 python3-pip python3-virtualenv $ git clone https://git.tannercollin.com/tanner/campcam.git -$ python campcam.py +$ cd campcam/ +$ virtualenv -p python3 env +$ . env/bin/activate +(env) $ pip install -r requirements.txt +# edit secrets.py.example, save as secrets.py +(env) $ python campcam.py +``` + +## Process Control + +``` +$ sudo apt install supervisor +``` + +Append to `/etc/supervisor/supervisor.conf`, replace `tanner` with your Linux username: + +``` +[program:campcam] +user=tanner +directory=/home/tanner/campcam +command=/home/tanner/campcam/env/bin/python -u campcam.py +stopsignal=INT +stopasgroup=true +killasgroup=true +autostart=true +autorestart=true +stderr_logfile=/var/log/campcam.log +stderr_logfile_maxbytes=10MB +stdout_logfile=/var/log/campcam.log +stdout_logfile_maxbytes=10MB +``` + +``` +$ sudo supervisorctl reread; sudo supervisorctl update ``` diff --git a/campcam.py b/campcam.py index 87a5f06..39371fd 100644 --- a/campcam.py +++ b/campcam.py @@ -1,39 +1,52 @@ -import telegram -from telegram.ext import CommandHandler, Updater +import logging import picamera import time -import os + +from telegram import ForceReply, Update +from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters + import secrets -bot = telegram.Bot(token=secrets.API_TOKEN) +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO +) +logging.getLogger('httpx').setLevel(logging.WARNING) -def send_photo(update, context): - # Take a photo with the Raspberry Pi camera +logger = logging.getLogger(__name__) + +async def start(update, context): + user = update.effective_user + await update.message.reply_html( + rf'Hi {user.mention_html()}!', + reply_markup=ForceReply(selective=True), + ) + +async def campcam(update, context): print('Got campcam command') with picamera.PiCamera() as camera: camera.resolution = (2592, 1944) camera.start_preview() - # Camera warm-up time time.sleep(5) - # Save the photo to a file filename = 'data/' + str(int(time.time())) + '.jpg' camera.capture(filename) print('Saved file to', filename) - chat_id = update.message.chat_id - message_id = update.message.message_id - bot.send_photo(chat_id=chat_id, photo=open(filename, 'rb'), reply_to_message_id=message_id) + await update.message.reply_photo(photo=open(filename, 'rb'), connect_timeout=300, write_timeout=300, read_timeout=300) print('Sent to chat') -send_photo_handler = CommandHandler('campcam', send_photo) -updater = Updater(bot=bot, workers=1) -dispatcher = updater.dispatcher -dispatcher.add_handler(send_photo_handler) -print('Loaded.') +def main(): + application = Application.builder().token(secrets.API_TOKEN).build() -updater.start_polling() + application.add_handler(CommandHandler('start', start)) + application.add_handler(CommandHandler('campcam', campcam)) + + application.run_polling(allowed_updates=Update.ALL_TYPES) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7f1df05 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +anyio==4.3.0 +APScheduler==3.6.3 +cachetools==4.2.2 +certifi==2024.2.2 +exceptiongroup==1.2.1 +h11==0.14.0 +httpcore==1.0.5 +httpx==0.27.0 +idna==3.7 +picamera==1.13 +python-telegram-bot==21.1.1 +pytz==2024.1 +six==1.16.0 +sniffio==1.3.1 +tornado==6.1 +typing-extensions==4.11.0 +tzlocal==5.2 diff --git a/secrets.py.example b/secrets.py.example new file mode 100644 index 0000000..5e774d1 --- /dev/null +++ b/secrets.py.example @@ -0,0 +1 @@ +API_TOKEN = ''