skip to Main Content

Context:
I want to add an automated message in my telegram bot i did with python that will send a message every time a specific holiday is eligible. For example, international mans day, independance day etc.
Is this possible without using a json file? Preferably with a simple if function that will compare the current date with a specified one from a list and return a message according to date? I am new in Python if you can direct me to some documentation with an example i would appreciate it:)

2

Answers


  1. You could make multiple cron jobs, for each date. Each cron job would run your script and send your message. There is probably a better way to do it. But I personally use cron for all my scripts that execute off a schedule.

    Take a look at this for using cron for particular dates:
    Cron expression for particular date

    You can also use this to easily configure cron schedules:
    https://crontab.guru/

    Login or Signup to reply.
  2. If the number of the special holidays is not too high, you could put the dates and the messages inline in the script like so:

    from datetime import date
    
    messages = {
        "03-20": "A message for March Equinox here",
        "09-22": "A message for September Equinox here"
    }
    
    
    def get_message():
        mm_dd = date.today().strftime("%m-%d")
        return messages.get(mm_dd)
    
    
    print(get_message())
    

    If get_message returns something other than None, pass it to the Telegram bot. Run this from cron once a day.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search