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