Skip to content

Webhooks and cron jobs

Daniel Kontšek edited this page Aug 7, 2015 · 9 revisions

Webhooks

The internal web server can be enabled by configuring the host and port options in the [webserver] section of Ludolph's configuration file.

Warning: Web hooks in default plugins do not care about authentication or any credentials verification.

Webhooks in base plugin

URL Methods Description
/ GET Default web page - the about message
/ping GET pong
/message POST Send private message (msg parameter) to user (jid parameter)
/broadcast POST Send private message (msg parameter) to every user in Ludolph's roster

Webhooks in muc plugin

URL Methods Description
/room POST Send message (msg parameter) to MUC room

@webhook

The @webhook can be used to decorate plugin methods which will transform them into web server views. The webhooks are using Bottle Web Framework - have a look at bottle documentation for more info.

from ludolph.web import webhook, request, abort

class MyPlugin(LudolphPlugin):
    ...
    @webhook('/inform_user', methods=('POST',))               # Register web hook at URL /inform_user. Allow only POST requests
    def inform_user(self):
        user = request.forms.get('user', None)                # Fetch parameter from request form

        if not user:
            abort(400, 'Missing user parameter in request')   # Return 400 HTTP error response

        self.xmpp.msg_send(user, 'Message text')              # Send XMPP message to user

        return 'Message sent'                                 # Return 200 HTTP response

Cron jobs

For running cron jobs the internal cron thread must be enabled by setting enabled = True in [cron] section in your ludolph configuration file.

@cronjob

Use the @cronjob(minute='*', hour='*', day='*', month='*', dow='*') decorator on your methods to schedule periodic jobs.

from ludolph.cron import cronjob

class MyPlugin(LudolphPlugin):
    ...
    @cronjob(minute=range(0, 60, 5))
    def periodic_job(self):
        # do something every 5 minutes
        ...