all repos — gif-escarbot @ 21f3d3172d52d4c4f88ec37203d32cf5dfcbe2fc

Earthbound Café's custom delivery bot with other cool utilities built-in.

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 == int(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
17async def admin_forward(update: Update, _):
18    try:
19        await update.message.forward(ADMIN_ID)
20        return logger.info(f"Forwarded this message to admin: {update.message.text}")
21    except:
22        return logger.error(f"Couldn't forward this update to admin: {update}")
23
24def config_error():
25    logger.error("Please create and fill the .env file.")
26    exit(1)
27
28if __name__ == "__main__":
29    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
30    logger = logging.getLogger(__name__)
31    load_dotenv()
32    
33    try:
34        TOKEN = str(getenv("token"))
35        GROUP_ID = str(getenv("group_id"))
36        CHANNEL_ID = str(getenv("channel_id"))
37        ADMIN_ID = int(getenv("admin_id"))
38    except TypeError:
39        config_error()
40    
41    if '' in [TOKEN, GROUP_ID, CHANNEL_ID, ADMIN_ID]:
42        config_error()
43    
44    application = ApplicationBuilder().token(TOKEN).build()
45    application.add_handler(MessageHandler(filters.ChatType.CHANNEL, forward))
46    application.add_handler(MessageHandler(filters.ChatType.PRIVATE, admin_forward))
47    application.run_polling()