Jobs
A job runs on a schedule. There are two kinds:
-
With the shared data. The script runs with no visitor: the bot runs it on its own, even when nobody is writing. For example, load the list of doctors from another service into
app.*every night, and show it at once during the day. - With each visitor. For each person in the chosen audiences the bot runs the script with their variables and then, when a flow is set, starts that flow for them. For example, every morning remind those whose visit is today.
How to add one
- Open "Jobs" in the menu on the left.
- Press "New job".
- Give it a name.
- In "When to run" choose how often: every few minutes or hours, every day, on chosen weekdays or every month, and the time. Under the field you see how it reads: "On Mo, We, Fr at 09:00".
- In "The script works with" choose "Shared data" or "Each visitor".
- For visitors, choose the audiences.
- Write the script.
- For visitors, if you like, pick a flow or create a one-off flow right there. Its editor opens under the field.
- Press "Save". A job for visitors may have only the flow.
The time is in the bot's time zone. The zone is changed in "Bot settings".
What the script sees
The script is written as in the "Script" step. The language is described on the Scripts page.
-
app.*is the bot's real data. Changes toapp.*are kept. -
fetch,page_metaandsave_imagework with the same limits as in a conversation. -
platform.nowis the moment of the run.
In a job with the shared data, user.* and session.* are empty: there is no visitor. What is written there is gone after the run.
In a job for visitors the script runs for each person on their own: user.* are their variables, and changes to them are kept. What the script writes to session.* the flow they get after it sees.
This script refreshes the list of doctors. If the service did not answer, the old list stays:
temp.resp = fetch("https://clinic.example.com/api/doctors", {timeout: 20})
app.doctors = temp.resp.error ? app.doctors : temp.resp.body
This script drops orders older than 30 days:
app.orders = filter(app.orders, item.date >= add_days(platform.now, -30))
Runs and errors
- The switch by the job turns the scheduled runs on and off.
- "Run now" runs the job at once, even when it is off.
- Under the name you see the time of the last run. For the shared data, the
app.*keys it changed. For visitors, how many people it ran for and how many failed. - If the job stopped with an error, its text is on the card. The next run happens on schedule.
- The job's page has a "Run history": the last 50 runs with what they changed and their errors. For visitors it shows whose run failed.
- When a scheduled job stops working, the bot sends you a message in Telegram. While it keeps failing, it does not repeat it.
A bot may have up to 10 jobs. A job for visitors reaches up to 1,000 people per run and sends at the pace Telegram allows.