| 1 |
Webhooks |
| 2 |
======== |
| 3 |
|
| 4 |
Errbot has a small integrated webserver that is capable of hooking up |
| 5 |
endpoints to methods inside your plugins. |
| 6 |
|
| 7 |
You must configure the *Webserver* plugin before this functionality |
| 8 |
can be used. You can get the configuration template using `!plugin config |
| 9 |
Webserver`, from where it's just a simple matter of plugging in the |
| 10 |
desired settings. |
| 11 |
|
| 12 |
.. note:: |
| 13 |
There is a `!generate certificate` command to generate a |
| 14 |
self-signed certificate in case you want to enable SSL |
| 15 |
connections and do not have a certificate. |
| 16 |
|
| 17 |
.. warning:: |
| 18 |
It is not recommended to expose Errbot's webserver directly to the |
| 19 |
network. Instead, we recommend placing it behind a webserver |
| 20 |
such as `nginx <http://nginx.org/>`_ or `Apache <https://httpd.apache.org/>`_. |
| 21 |
|
| 22 |
|
| 23 |
Simple webhooks |
| 24 |
--------------- |
| 25 |
|
| 26 |
All you need to do for a plugin of yours to listen to a specific URI |
| 27 |
is to apply the :func:`~errbot.webhook` decorator to your method. |
| 28 |
Whatever it returns will be returned in response to the request: |
| 29 |
|
| 30 |
.. code-block:: python |
| 31 |
|
| 32 |
from errbot import BotPlugin, webhook |
| 33 |
|
| 34 |
class PluginExample(BotPlugin): |
| 35 |
@webhook |
| 36 |
def test(self, request): |
| 37 |
self.log.debug(repr(request)) |
| 38 |
return "OK" |
| 39 |
|
| 40 |
This will listen for POST requests on |
| 41 |
http://yourserver.tld:yourport/test/, and return *"OK"* as the |
| 42 |
response body. |
| 43 |
|
| 44 |
.. note:: |
| 45 |
If you return `None`, an empty 200 response will be sent. |
| 46 |
|
| 47 |
|
| 48 |
You can also set a custom URI pattern by providing the `uri_rule` |
| 49 |
parameter: |
| 50 |
|
| 51 |
.. code-block:: python |
| 52 |
|
| 53 |
from errbot import BotPlugin, webhook |
| 54 |
|
| 55 |
class PluginExample(BotPlugin): |
| 56 |
@webhook('/example/<name>/<action>/') |
| 57 |
def test(self, request, name, action): |
| 58 |
return "User %s is performing %s" % (name, action) |
| 59 |
|
| 60 |
Refer to the documentation on Flask's |
| 61 |
`route <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.route>`_ |
| 62 |
for details on the supported syntax |
| 63 |
(Errbot uses Flask internally). |
| 64 |
|
| 65 |
|
| 66 |
Handling JSON request |
| 67 |
--------------------- |
| 68 |
|
| 69 |
If an incoming request has the MIME media type set to `application/json` |
| 70 |
the request will automatically be decoded as JSON. |
| 71 |
You will receive the result of calling `json.loads()` on `request` automatically |
| 72 |
so that you won't have to do this yourself. |
| 73 |
|
| 74 |
|
| 75 |
Handling form-encoded requests |
| 76 |
------------------------------ |
| 77 |
|
| 78 |
Form-encoded requests (those with an |
| 79 |
*application/x-www-form-urlencoded* mimetype) are very simple to |
| 80 |
handle as well, you just need to specify the `form_param` parameter. |
| 81 |
|
| 82 |
A good example for this is the GitHub format which posts a form with |
| 83 |
a *payload* parameter: |
| 84 |
|
| 85 |
.. code-block:: python |
| 86 |
|
| 87 |
from errbot import BotPlugin, webhook |
| 88 |
|
| 89 |
class Github(BotPlugin): |
| 90 |
@webhook('/github/', form_param = 'payload') |
| 91 |
def notification(self, payload): |
| 92 |
for room in self.bot_config.CHATROOM_PRESENCE: |
| 93 |
self.send( |
| 94 |
self.build_identifier(room), |
| 95 |
'Commit on %s!' % payload['repository']['name'], |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
The raw request |
| 100 |
--------------- |
| 101 |
|
| 102 |
The above webhooks are convenient for simple tasks, but sometimes |
| 103 |
you might wish to have more power and have access to the actual |
| 104 |
request itself. By setting the `raw` parameter of the |
| 105 |
:func:`~errbot.decorators.webhook` decorator to `True`, you will |
| 106 |
be able to get the |
| 107 |
`flask.Request <http://flask.pocoo.org/docs/1.0/api/#flask.Request>`_ |
| 108 |
which contains all the details about the actual request: |
| 109 |
|
| 110 |
.. code-block:: python |
| 111 |
|
| 112 |
from errbot import BotPlugin, webhook |
| 113 |
|
| 114 |
class PluginExample(BotPlugin): |
| 115 |
@webhook(raw=True) |
| 116 |
def test(self, request): |
| 117 |
user_agent = request.headers.get("user-agent", "Unknown") |
| 118 |
return f"Your user-agent is {user_agent}" |
| 119 |
|
| 120 |
|
| 121 |
Returning custom headers and status codes |
| 122 |
----------------------------------------- |
| 123 |
|
| 124 |
Adjusting the response headers, setting cookies or returning a |
| 125 |
different status code can all be done by manipulating the |
| 126 |
`flask response <http://flask.pocoo.org/docs/1.0/patterns/deferredcallbacks/>`_ |
| 127 |
object. The Flask docs on `the response object |
| 128 |
<http://flask.pocoo.org/docs/1.0/api/#response-objects>`_ |
| 129 |
explain this in more detail. Here's an example of setting a |
| 130 |
custom header: |
| 131 |
|
| 132 |
.. code-block:: python |
| 133 |
|
| 134 |
from errbot import BotPlugin, webhook |
| 135 |
from flask import after_this_request |
| 136 |
|
| 137 |
class PluginExample(BotPlugin): |
| 138 |
@webhook |
| 139 |
def example(self, incoming_request): |
| 140 |
@after_this_request |
| 141 |
def add_header(response): |
| 142 |
response.headers['X-Powered-By'] = 'Errbot' |
| 143 |
return "OK" |
| 144 |
|
| 145 |
Flask also has various helpers such as the `abort()` method. |
| 146 |
Using this method we could, for example, return a 403 forbidden |
| 147 |
response like so: |
| 148 |
|
| 149 |
.. code-block:: python |
| 150 |
|
| 151 |
from errbot import BotPlugin, webhook |
| 152 |
from flask import abort |
| 153 |
|
| 154 |
class PluginExample(BotPlugin): |
| 155 |
@webhook |
| 156 |
def example(self, incoming_request): |
| 157 |
abort(403, "Forbidden") |
| 158 |
|
| 159 |
|
| 160 |
Testing a webhook through chat |
| 161 |
------------------------------ |
| 162 |
|
| 163 |
You can use the `!webhook` command to test webhooks without making |
| 164 |
an actual HTTP request, using the following format:: |
| 165 |
|
| 166 |
!webhook test /[endpoint] [post_content] |
| 167 |
|
| 168 |
For example:: |
| 169 |
|
| 170 |
!webhook test /test |
| 171 |
|
| 172 |
!webhook test /github payload=%7B%22pusher%22%3A%7B%22name%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%2C%22repository%22%3A%7B%22name%22%3A%22test%22%2C%22created_at%22%3A%222012-08-12T16%3A09%3A43-07%3A00%22%2C%22has_wiki%22%3Atrue%2C%22size%22%3A128%2C%22private%22%3Afalse%2C%22watchers%22%3A0%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fgbin%2Ftest%22%2C%22fork%22%3Afalse%2C%22pushed_at%22%3A%222012-08-12T16%3A26%3A35-07%3A00%22%2C%22has_downloads%22%3Atrue%2C%22open_issues%22%3A0%2C%22has_issues%22%3Atrue%2C%22stargazers%22%3A0%2C%22forks%22%3A0%2C%22description%22%3A%22ignore%20this%2C%20this%20is%20for%20testing%20the%20new%20err%20github%20integration%22%2C%22owner%22%3A%7B%22name%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%7D%2C%22forced%22%3Afalse%2C%22after%22%3A%22b3cd9e66e52e4783c1a0b98fbaaad6258669275f%22%2C%22head_commit%22%3A%7B%22added%22%3A%5B%5D%2C%22modified%22%3A%5B%22README.md%22%5D%2C%22timestamp%22%3A%222012-08-12T16%3A24%3A25-07%3A00%22%2C%22removed%22%3A%5B%5D%2C%22author%22%3A%7B%22name%22%3A%22Guillaume%20BINET%22%2C%22username%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fgbin%2Ftest%2Fcommit%2Fb3cd9e66e52e4783c1a0b98fbaaad6258669275f%22%2C%22id%22%3A%22b3cd9e66e52e4783c1a0b98fbaaad6258669275f%22%2C%22distinct%22%3Atrue%2C%22message%22%3A%22voila%22%2C%22committer%22%3A%7B%22name%22%3A%22Guillaume%20BINET%22%2C%22username%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%7D%2C%22deleted%22%3Afalse%2C%22commits%22%3A%5B%7B%22added%22%3A%5B%5D%2C%22modified%22%3A%5B%22README.md%22%5D%2C%22timestamp%22%3A%222012-08-12T16%3A24%3A25-07%3A00%22%2C%22removed%22%3A%5B%5D%2C%22author%22%3A%7B%22name%22%3A%22Guillaume%20BINET%22%2C%22username%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fgbin%2Ftest%2Fcommit%2Fb3cd9e66e52e4783c1a0b98fbaaad6258669275f%22%2C%22id%22%3A%22b3cd9e66e52e4783c1a0b98fbaaad6258669275f%22%2C%22distinct%22%3Atrue%2C%22message%22%3A%22voila%22%2C%22committer%22%3A%7B%22name%22%3A%22Guillaume%20BINET%22%2C%22username%22%3A%22gbin%22%2C%22email%22%3A%22gbin%40gootz.net%22%7D%7D%5D%2C%22ref%22%3A%22refs%2Fheads%2Fmaster%22%2C%22before%22%3A%2229b1f5e59b7799073b6d792ce76076c200987265%22%2C%22compare%22%3A%22https%3A%2F%2Fgithub.com%2Fgbin%2Ftest%2Fcompare%2F29b1f5e59b77...b3cd9e66e52e%22%2C%22created%22%3Afalse%7D |
| 173 |
|
| 174 |
.. note:: |
| 175 |
You can get a list of all the endpoints with the `!webstatus` |
| 176 |
command. |