40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
import telegram
|
|
from telegram.ext import CommandHandler, Updater
|
|
import picamera
|
|
import time
|
|
import os
|
|
import secrets
|
|
|
|
bot = telegram.Bot(token=secrets.API_TOKEN)
|
|
|
|
def send_photo(update, context):
|
|
# Take a photo with the Raspberry Pi camera
|
|
|
|
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)
|
|
|
|
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.')
|
|
|
|
updater.start_polling()
|