Update telegram library, add docs

This commit is contained in:
2024-05-18 04:21:06 +01:00
parent 31049541ba
commit b6f9687e4d
4 changed files with 90 additions and 19 deletions
+30 -17
View File
@@ -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()