import logging from telegram import ForceReply, Update, InputFile from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters from pydub import AudioSegment import io from io import BytesIO from voice_manager import VoiceManager from photo_manager import PhotoManager # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) # set higher logging level for httpx to avoid all GET and POST requests being logged logging.getLogger("httpx").setLevel(logging.WARNING) logger = logging.getLogger(__name__) # Define a few command handlers. These usually take the two arguments update and # context. async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Send a message when the command /start is issued.""" user = update.effective_user await update.message.reply_html( rf"Hi {user.mention_html()}!", reply_markup=ForceReply(selective=True), ) async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Send a message when the command /help is issued.""" await update.message.reply_text("Help!") async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: print("Ricevuto messaggio testuale") """Echo the user message.""" await update.message.reply_text(update.message.text) async def voice_oracle(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: audio_file = await update.message.voice.get_file() audio_bytes = await audio_file.download_as_bytearray() await context.bot.send_voice(update.message.chat_id, BytesIO(voice_oracle.vocal_manager.oracle(audio_bytes))) voice_oracle.vocal_manager = VoiceManager() async def get_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: print(get_photo.photo_mgr.get_random_photo()) await update.message.reply_photo(open(get_photo.photo_mgr.get_random_photo(), 'rb')) get_photo.photo_mgr = PhotoManager() async def replace_faces(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: # user = update.message.from_user photo_file = await update.message.photo[-1].get_file() photo_bytes = await photo_file.download_as_bytearray() out_img_bytes = replace_faces.photo_mgr.replace_faces(photo_bytes) await update.message.reply_photo(InputFile(io.BytesIO(out_img_bytes))) # await photo_file.download_to_drive("user_photo.jpg") # logger.info("Photo of %s: %s", user.first_name, "user_photo.jpg") replace_faces.photo_mgr = PhotoManager() def main() -> None: """Start the bot.""" # Create the Application and pass it your bot's token. application = Application.builder().token("6583917096:AAG2SifgcHoQEqzprVp57ZVbxVgwc8Mygpo").build() # on different commands - answer in Telegram application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("help", help_command)) application.add_handler(CommandHandler("photo", get_photo)) # on non command i.e message - echo the message on Telegram application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) application.add_handler(MessageHandler(filters.VOICE, voice_oracle)) application.add_handler(MessageHandler(filters.PHOTO, replace_faces)) # Run the bot until the user presses Ctrl-C application.run_polling(allowed_updates=Update.ALL_TYPES) if __name__ == "__main__": main()