scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 396 forks source link

Add a job queue #157

Open Tahvok opened 6 years ago

Tahvok commented 6 years ago

Coming from telegram bots, there's a nice wrapper that can queue jobs: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue

Basically it's a scheduler that can run specific tasks as scheduled.

It would be nice to have this feature here as well. There are multiple ways we are using this in telegram, and I have not found a good way to have this with slack.

DonDebonair commented 6 years ago

Slackbot is already inspired by Will, and I think Will has a nice API for handling this kind of situation: http://skoczen.github.io/will/plugins/notice/#take-an-action-on-a-schedule. I personally think it's nicer than the (somewhat overly verbose) way python-telegram-bot is approaching this.

I'd suggest mimicking the way Will handles scheduling. What do you think @lins05 ?

jtatum commented 6 years ago

I like that API, @DandyDev. Maybe I can put some time into it after #158.

DannyHinshaw commented 6 years ago

Though not a traditional task queue, I've been working on a bot where I just integrated a task scheduling functionality. It's implementation is basically a decorator wrapped around APScheduler, running tasks in a background thread. Once the feature is more established it shouldn't be to difficult to piece into slackbot.

Like I said, it's still being developed, but here's an example of it's usage. If there's interest then I'll look into making a PR when it's finished.

# quick overview of decorator args
    """
    Function decorator to designate function as a task.
    :param interval: Interval
        if cron=False:
            seconds to run the task.
        else:
            dict containing cron day/time params
    :param cron: Boolean indicating whether to run task as cron job or interval in seconds.
    :param delay: Boolean indicating whether to run task immediately or offset by interval.
    :param run_once: Boolean indicating whether to run task once or as a routine.
    :return decorator:
    """

@bot_routine({'day_of_week': 'mon'}, cron=True)
def test_cron():
    print('this function will run every Monday at 12am')

@bot_routine({'day_of_week': 'mon-fri', 'hour': '5', 'minute': '30'}, cron=True)
def test_cron():
    print('this function will run every day Monday-Friday at 5:30am')

@bot_routine(60, delay=False)
def test_cron():
    print('this function will run immediately on load, then once every 60 seconds')

@bot_routine(60, delay=True)
def test_cron():
    print('this function will run after 60 seconds pass, then once every 60 seconds')

@bot_routine(60, delay=True, run_once=True)
def test_cron():
    print('this function will run once after 60 seconds')
DonDebonair commented 6 years ago

I don't want to steal anyone's thunder, but after looking for a proper Slack bot framework that had everything I need (which includes scheduling), and not finding a proper solution, I built my own. Slack Machine takes inspiration from both slackbot and Will, and it also includes scheduling. If you need something like that, you can try out Slack Machine. Also, feel free to use any code from Slack Machine to enhance slackbot itself. It's MIT licensed.