Skip to content

Make it easier to handle webhooks manually, by providing a convience setup method for dispatcher #1258

@jsmnbom

Description

@jsmnbom

Sometimes you need not just webhooks from telegram, but from other services too. It would be nice if we could integrate this nicely into the library, without forcing users to not use Updater (which means handling threading, jobqueue starting, and of cause webhooks themselves).

As this is still very much an advanced feature, I think a good way of doing this could be to allow passing a custom WebhookAppClass to Updater._start_webhook somehow.

def _start_webhook(self, listen, port, url_path, cert, key, bootstrap_retries, clean,
webhook_url, allowed_updates):
self.logger.debug('Updater thread started (webhook)')
use_ssl = cert is not None and key is not None
if not url_path.startswith('/'):
url_path = '/{0}'.format(url_path)
# Create Tornado app instance
app = WebhookAppClass(url_path, self.bot, self.update_queue)

For example to also receive webhooks from github one could write a custom GithubWebhookHandler (instance of tornado.web.RequestHandler that perhaps turns incomming data into a GithubUpdate type that a TypeHandler can then handle) and then a WebhookAppClass like:

class WebhookAppClass(tornado.web.Application):
    def __init__(self, webhook_path, bot, update_queue):
        handlers = [
            (
                r'/github/?',
                GithubWebhookHandler,
                {'update_queue': update_queue}
            ),
            (
                r"{0}/?".format(webhook_path),
                telegram.utils.webhookhandler.WebhookHandler,
                {'bot': bot, 'update_queue': update_queue}
            )
        ]
        super().__init__(handlers)

The only way this is currently possible to do is something like

telegram.utils.webhookhandler.WebhookAppClass = WebhookAppClass

But as that line needs to come before any telegram imports, it makes it quite messy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions