campcam/campcam.py

53 lines
1.4 KiB
Python
Raw Normal View History

2024-05-18 03:21:06 +00:00
import logging
2023-05-10 05:18:56 +00:00
import picamera
import time
2024-05-18 03:21:06 +00:00
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
2023-05-10 05:18:56 +00:00
import secrets
2024-05-18 03:21:06 +00:00
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logging.getLogger('httpx').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
2023-05-10 05:18:56 +00:00
2024-05-18 03:21:06 +00:00
async def start(update, context):
user = update.effective_user
await update.message.reply_html(
rf'Hi {user.mention_html()}!',
reply_markup=ForceReply(selective=True),
)
2023-05-10 05:18:56 +00:00
2024-05-18 03:21:06 +00:00
async def campcam(update, context):
2023-05-10 05:18:56 +00:00
print('Got campcam command')
with picamera.PiCamera() as camera:
camera.resolution = (2592, 1944)
camera.start_preview()
time.sleep(5)
filename = 'data/' + str(int(time.time())) + '.jpg'
camera.capture(filename)
print('Saved file to', filename)
2024-05-18 03:21:06 +00:00
await update.message.reply_photo(photo=open(filename, 'rb'), connect_timeout=300, write_timeout=300, read_timeout=300)
2023-05-10 05:18:56 +00:00
print('Sent to chat')
2024-05-18 03:21:06 +00:00
def main():
application = Application.builder().token(secrets.API_TOKEN).build()
application.add_handler(CommandHandler('start', start))
application.add_handler(CommandHandler('campcam', campcam))
application.run_polling(allowed_updates=Update.ALL_TYPES)
2023-05-10 05:18:56 +00:00
2024-05-18 03:21:06 +00:00
if __name__ == '__main__':
main()