skip to Main Content

I have telegram bot which is made by python. The file must run at 06:00 P.M everyday.
My PC is Windows 10.
How can I achieve this , please guide me.

3

Answers


    1. Find out your Python exe path, e.g., C:python-pathpython.exe
    2. Save your Python script somewhere and note the path
    3. Then, open the Task Scheduler
    4. Go to Actions > Create Task
    5. Give it a name and then Go to Actions > New
    6. Ensure the action is ‘Start a program’
    7. In the "Program/script" you will add the path of the Python exe (C:python-pathpython.exe).
    8. In the "Add arguments (optional)” box, you will add the name of your python file (e.g., myScript.py).
    9. In the "Start in (optional)" box, you will add the location of your Python file (the whole path of the file, C:path-to-the-python-script).
    Login or Signup to reply.
  1. If you are using python-telegram-bot you should take a look at JobQueue.
    It has a run_daily which does what you want:

    run_daily(callback, time, days=(0, 1, 2, 3, 4, 5, 6), context=None, name=None)
    

    Here is an example with it:

        def daily_function(context: telegram.ext.CallbackContext):
          bot.send_message(chat_id=id, text='Daily message')
    
        def call_daily_message(update,context):
           context.job_queue.run_daily(daily_function, context=update.message.chat_id,days=(0, 1, 2, 3, 4, 5, 6),time = time(hour = 18, minute = 0, second = 0))
    

    This example executes the function daily_function everyday at 18:00.

    Login or Signup to reply.
  2. You can use Windows scheduled tasks:

    1. Save your python file. For example: my_python.py

    2. Create a bat file that runs my_python.py. Assuming Python is installed in the path below:

      "C:UsersMyUserAppDataLocalProgramsPythonPython37-32python.exe"

    And my_python is saved in:

    "C:UsersMyUserDesktopmy_python.py"
    

    Create new text file and save it as run_my_python.bat with the following content:

    "C:UsersMyUserAppDataLocalProgramsPythonPython37-32python.exe" "C:UsersMyUserDesktopmy_python.py"
    
    1. Create a scheduled task to run run_my_python.bat.
      Go to Windows Task Scheduler, set your trigger, and choose "Start a program" action. Select run_my_python and save the scheduled task.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search