Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CronJob to call a function #707

Closed
apritykin opened this issue May 20, 2014 · 6 comments
Closed

CronJob to call a function #707

apritykin opened this issue May 20, 2014 · 6 comments

Comments

@apritykin
Copy link

Trying to get Hubot to call a function (could be in the same script or from a different script) periodically every few minutes.

Currently I am able to say "hubot totalviewers" in a hipchat room and hubot will respond with the results from a function.

I would like hubot to call function "totalviewers" every few minutes without having the user required to initiate the command.

I cant seem to find any solid documentation on this.

@technicalpickles
Copy link
Member

Check out https://github.com/github/hubot/blob/master/docs/scripting.md#timeouts-and-intervals for some examples of doing things on an interval.

@apritykin
Copy link
Author

I tried this

module.exports = (robot) ->
    loopIntervalId = null

    robot.respond /begintalking/, (msg) ->
        if loopIntervalId
            msg.send "I am still talking....and wont stop until you tell me"
            return

            msg.send "I will begin to talk every minute now!"
            loopIntervalId = setInterval () ->
                msg.send "I am still talking....and wontstop until you tell me"
                , 1000

    robot.respond /shutup/, (msg) ->
        if loopIntervalId
            msg.send "okay okay ill stop"
            clearInterval(loopIntervalId)
            loopIntervalId = null
        else
            msg.send "You have the power to stop me, just say it!"

But it does not seem to be doing anything

@technicalpickles
Copy link
Member

I can't tell if it's a bad copy paste or not, but it looks like the code in /begintalking to start talking is actually nested under the if loopIntervalId, so wouldn't ever be executed.

@apritykin
Copy link
Author

Even when I move the /begintalking/ above the loopintervalId it still is not working.

module.exports = (robot) ->
    loopIntervalId = null

    robot.respond /begintalking/, (msg) ->
            msg.send "I will begin to talk every minute now!"
            loopIntervalId = setInterval () ->
                msg.send "I am still talking....and wontstop until you tell me"
                , 1000
        if loopIntervalId
            msg.send "I am still talking....and wont stop until you tell me"
            return

    robot.respond /shutup/, (msg) ->
        if loopIntervalId
            msg.send "okay okay ill stop"
            clearInterval(loopIntervalId)
            loopIntervalId = null
        else
            msg.send "You have the power to stop me, just say it!"

@apritykin
Copy link
Author

nevermind. It works. It was indentation issue

@technicalpickles
Copy link
Member

Excellent! I think we can close this out, unless you have any suggestions on how to make it easier to find the documentation and whatnot? Maybe use the word 'cron' somewhere so it's searchable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants