question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing context from the node-cron function

See original GitHub issue

Hello!

I have difficulties with accessing telegram context from the node-cron function. What I want to achieve:

var cron = require('node-cron');
 
cron.schedule('* * * * *', () => {
  ctx.session.variable = 'something';
  ctx.telegram.sendMessage(chatID, message);
  <..>
});

You got a general idea. Is it possible or I should look in another direction? The main goal is to periodically execute something. Right now it looks like this:

bot.on('text', (ctx) => {
    if ((ctx.message.date - ctx.session.variableWithTimestamp) > 3600) {
    ctx.session.variableWithTimestamp += 3600;
    //do something
  }
})

and I understand that there should be much better way to perform it

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dotcypresscommented, Oct 29, 2019

@Raingerlina context is a short-lived and valid only during update processing.

For sending messages from any kind of a queues/schedulers use Telegram instance. And you can access to session directly, without context.

0reactions
Raingerlinacommented, Dec 28, 2019

As far as I understand you still need to clarify the session key manually in order to access it outside the context. Because otherwise how would the redis itself know where to retrieve and put the information

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing context info in onTick function · Issue #47 - GitHub
We need to pass context info in onTick function the way this is implemented in setInterval, any support for this.
Read more >
Task scheduling with node-cron - ITNEXT
The node-cron module is a tiny task scheduler in pure JavaScript for Node.js based on GNU crontab. We are going to build a...
Read more >
How to manage Cron Jobs in Node.js - Stack Overflow
You can use cron module to do this. Install cron using npm i cron . As an example, var CronJob = require('cron').CronJob; new...
Read more >
How To Use node-cron to Run Scheduled Jobs in Node.js
Use Node.js and the node-cron library to schedule jobs to manage logs, back up databases, and send emails.
Read more >
cron - npm
node -cron ... Cron is a tool that allows you to execute something on a schedule. This is typically done using the cron...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found