pt13
pt13

Reputation: 15

telegram bot python specific day automated messages

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:)

Upvotes: 0

Views: 897

Answers (2)

Pēteris Caune
Pēteris Caune

Reputation: 45112

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.

Upvotes: 1

user16545584
user16545584

Reputation:

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/

Upvotes: 0

Related Questions