main.py (view raw)
1from telegram.ext import MessageHandler, ApplicationBuilder, filters
2from telegram import Update
3import logging, dotenv, os
4dotenv.load_dotenv()
5logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
6logger = logging.getLogger(__name__)
7
8async def forward(update: Update, _):
9 if update.effective_chat.id == CHANNEL_ID:
10 if update.channel_post is not None:
11 await update.channel_post.forward(GROUP_ID)
12 logger.info("Forwarded a message.")
13
14def err_missing_file(filename: str, exit_arg: int = 1):
15
16 exit(exit_arg)
17
18if __name__ == "__main__":
19
20 try:
21 TOKEN = str(os.getenv("token"))
22 GROUP_ID = int(os.getenv("group_id"))
23 CHANNEL_ID = int(os.getenv("channel_id"))
24 except TypeError:
25 logger.error(f"Please create and fill the following file: .env")
26 exit(1)
27
28 if '' in [TOKEN, GROUP_ID, CHANNEL_ID]:
29 logger.error(f"Please fill the following file: .env")
30 exit(1)
31
32 application = ApplicationBuilder().token(TOKEN).build()
33 application.add_handler(MessageHandler(filters.ChatType.CHANNEL, forward))
34 application.run_polling()