'Customize welcome bot in telegram

I want to give different welcome messages to to new members who join the telegram group in the different time(morning / overnight), so I fixed a bit of code of telegram welcome bot but it didn't properly work

welcome_morning = 9am - 11pm
welcome_overnight = 11pm - 9am

I think main problem is the indentation,but I'm not sure.

Can you teach me what's wrong with the code and make this work?

Thank you

def welcome_morning(update, context, new_member):

    now = datetime.datetime.now()
    if 9 <= now.hour <= 23:

        message = update.message
        chat_id = message.chat.id
        logger.info(
            "%s joined to chat %d (%s)",
            escape(new_member.first_name),
            chat_id,
            escape(message.chat.title),
        )

        # Pull the custom message for this chat from the database
        text = db.get(str(chat_id))

    # Use default message if there's no custom one set

    if text is None:
        text = 'text for new member in the morning'

    # Replace placeholders and send message
    text = text.replace("$username", new_member.first_name)
    text = text.replace("$title", message.chat.title)
    send_async(context, chat_id=chat_id, text=text, parse_mode=ParseMode.HTML)
def welcome_overnight(update, context, new_member):

    now = datetime.datetime.now()
    if 23 <= now.hour <= 9:

        message = update.message
        chat_id = message.chat.id
        logger.info(
            "%s joined to chat %d (%s)",
            escape(new_member.first_name),
            chat_id,
            escape(message.chat.title),
        )

        # Pull the custom message for this chat from the database
        text = db.get(str(chat_id))

        # Use default message if there's no custom one set
        
    if text is None:
        text = text for new member in the night

    # Replace placeholders and send message
    text = text.replace("$username", new_member.first_name)
    text = text.replace("$title", message.chat.title)
    send_async(context, chat_id=chat_id, text=text, parse_mode=ParseMode.HTML)



Solution 1:[1]

try this code:

def welcome(update, context, new_member):

    message = update.message
    chat_id = message.chat.id
    now = datetime.datetime.now()

    logger.info(
        "%s joined to chat %d (%s)",
        escape(new_member.first_name),
        chat_id,
        escape(message.chat.title),
    )

    # Pull the custom message for this chat from the database
    text = db.get(str(chat_id))

    
    # Use default message if there's no custom one set

    if text is None:
        if 9 <= now.hour <= 23:
            text = 'text for new member in the morning'
        else:
            text = 'text for new member in the night'
    
    # Replace placeholders and send message
    text = text.replace("$username", new_member.first_name)
    text = text.replace("$title", message.chat.title)
    send_async(context, chat_id=chat_id, text=text, parse_mode=ParseMode.HTML)

In this way you have only a function that checks the time and set the correct custome welcome message (only if there is no custome one set).

Another suggestion could be replacing the now=datetime.datetime.now() with the date parameter inside the message object. But you should convert it to datetime object.

now = datetime.datetime.fromtimestamp(message.date)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 barbax7