I had a similar problem and solved it using a library call cron
.
this library allows you to schedule tasks very easily
here is an example of code i used to implement sending a message at a specific time
import {CronJob} from "cron";
sendMessageEveryMinute(message) {
let bot = this.bot;
let chatId = this.chatId;
let job = new CronJob('1 * * * * *',
function () {
bot.telegram.sendMessage(chatId, message).then(r => console.log(r));
}, null, true, 'Europe/Rome');
}
as you can see (minus the telegram methods) what we do is just declare an object called CronJob
which takes as input the so called cron schedule expressions (which is used to define the actual time when the function we want to execute will execute) and the second parameter is the function itself we want to execute at that specific time.
The true
param that you see is telling that the schedule will start at instantiation time. this means that the schedule will start as the object job
gets instantiated.
For the other params look at the documentation:
cron documentation