mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
Add WordPress webhook.
Adds a new webhook integration for WordPress blogs. Both WordPress.com and self-installed blogs are supported, with minor differences that are described in the documentation. It creates a new message for each action, the stream and topic may be specified or use default values. WordPress actions supported: publish_post: a new blog post was published publish_page: a new page was published user_register: a new user account was created wp_login: a user logged in Notes: comment_post only provides the id of the parent post, not title or link, so was not included. On further testing, I found edit_post is not very practical, it also fires while a new post is being written, and when posts are deleted. (I think it tracks drafts too.) I've removed it, as it seems more confusing than useful. Fixes #3245
This commit is contained in:
BIN
static/images/integrations/logos/wordpress.png
Normal file
BIN
static/images/integrations/logos/wordpress.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
static/images/integrations/wordpress/wordpress_config_done.png
Normal file
BIN
static/images/integrations/wordpress/wordpress_config_done.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
BIN
static/images/integrations/wordpress/wordpress_configure_url.png
Normal file
BIN
static/images/integrations/wordpress/wordpress_configure_url.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 36 KiB |
BIN
static/images/integrations/wordpress/wordpress_hookpress.png
Normal file
BIN
static/images/integrations/wordpress/wordpress_hookpress.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
BIN
static/images/integrations/wordpress/wordpress_post_created.png
Normal file
BIN
static/images/integrations/wordpress/wordpress_post_created.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
@@ -1208,7 +1208,6 @@ access_token_secret =</pre>
|
||||
in <code>/etc/zulip/settings.py</code> on the Zulip server.</p>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
1
zerver/fixtures/wordpress/wordpress_publish_page.txt
Normal file
1
zerver/fixtures/wordpress/wordpress_publish_page.txt
Normal file
@@ -0,0 +1 @@
|
||||
stream=wordpress&topic=WordPress%20Page&hook=publish_page&post_type=page&post_title=New%20Blog%20Page&post_url=http%3A%2F%2Fexample.com
|
||||
1
zerver/fixtures/wordpress/wordpress_publish_post.txt
Normal file
1
zerver/fixtures/wordpress/wordpress_publish_post.txt
Normal file
@@ -0,0 +1 @@
|
||||
stream=wordpress&topic=WordPress%20Post&hook=publish_post&post_type=post&post_title=New%20Blog%20Post&post_url=http%3A%2F%2Fexample.com
|
||||
@@ -0,0 +1 @@
|
||||
hook=publish_post
|
||||
@@ -0,0 +1 @@
|
||||
stream=wordpress&topic=WordPress%20Post&hook=publish_post&post_title=New%20Blog%20Post&post_url=http%3A%2F%2Fexample.com
|
||||
@@ -0,0 +1 @@
|
||||
stream=wordpress&topic=WordPress%20Post&post_type=post&post_title=New%20Blog%20Post&post_url=http%3A%2F%2Fexample.com
|
||||
1
zerver/fixtures/wordpress/wordpress_user_register.txt
Normal file
1
zerver/fixtures/wordpress/wordpress_user_register.txt
Normal file
@@ -0,0 +1 @@
|
||||
stream=wordpress&topic=New%20Blog%20Users&post_type=post&hook=user_register&display_name=test_user&user_email=test_user@example.com
|
||||
1
zerver/fixtures/wordpress/wordpress_wp_login.txt
Normal file
1
zerver/fixtures/wordpress/wordpress_wp_login.txt
Normal file
@@ -0,0 +1 @@
|
||||
stream=wordpress&user_login=testuser&hook=wp_login&topic=New%20Login
|
||||
@@ -162,6 +162,7 @@ WEBHOOK_INTEGRATIONS = [
|
||||
logo='static/images/integrations/logos/yo-app.png',
|
||||
display_name='Yo App'
|
||||
),
|
||||
WebhookIntegration('wordpress', display_name='WordPress'),
|
||||
WebhookIntegration('zapier'),
|
||||
WebhookIntegration('zendesk')
|
||||
] # type: List[WebhookIntegration]
|
||||
|
||||
0
zerver/webhooks/wordpress/__init__.py
Normal file
0
zerver/webhooks/wordpress/__init__.py
Normal file
132
zerver/webhooks/wordpress/doc.html
Normal file
132
zerver/webhooks/wordpress/doc.html
Normal file
@@ -0,0 +1,132 @@
|
||||
<p>
|
||||
See WordPress blog notifications in Zulip!
|
||||
</p>
|
||||
|
||||
<p>
|
||||
This integration works with both <a href="http://wordpress.com">WordPress.com</a>
|
||||
blogs and self-installed blogs using software from <a href="http://wordpress.org">
|
||||
WordPress.org</a> and the optional <a href="https://wordpress.org/plugins/hookpress/">
|
||||
HookPress plugin</a>. For more details on the two, please see the WordPress.com
|
||||
support page about <a href="https://en.support.wordpress.com/com-vs-org/"> the
|
||||
difference between WordPress.com and WordPress.org.</a>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The destination stream for your WordPress notifications must already exist. The
|
||||
default stream is <code>wordpress</code>. Some actions are only available for self-
|
||||
installed blogs. For a complete list of supported action types, please see the table
|
||||
at the bottom of this page.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Configuration</b>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<em>Note: screen capture images in this tutorial use a self-installed blog.</em>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
To configure a new webhook from WordPress, go to the <b>Webhooks</b> page in the
|
||||
<b>Settings</b> section of your blog dashboard and click <b>Add webhook</b>.
|
||||
</p>
|
||||
|
||||
<p><img class="screenshot" src="/static/images/integrations/wordpress/wordpress_hookpress.png" /></p>
|
||||
|
||||
<p>
|
||||
If you use WordPress.com and have trouble locating the correct page, you can reach
|
||||
it by manually typing the url in your browser address bar as in this example:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<code>https://<em><b>yourblogname</b></em>.wordpress.com/wp-admin/options-general.php?page=webhooks</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Select the hook type <b>action</b> and the specific action that should trigger
|
||||
this webhook notification. This example uses <b>publish_post</b>, which is triggered
|
||||
when a new blog post is created.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The Zulip WordPress integration uses the fields <b>post_title</b>, <b>post_type</b>,
|
||||
and <b>post_url</b> for a <b>publish_post</b> action, so select those three fields in
|
||||
the list.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Next, enter the URL for the Zulip endpoint, specifying your desired destination
|
||||
stream and topic. Construct your URL like this for a WordPress.com blog:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<code>{{ external_api_uri_subdomain }}/v1/external/wordpress?api_key=abcdefgh&stream=wordpress&topic=New%20Posts</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
and like this for a self-installed blog:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<code>{{ external_api_uri_subdomain }}/v1/external/wordpress?api_key=abcdefgh;stream=wordpress;topic=New%20Posts</code>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The parameters are as follows:
|
||||
<ul>
|
||||
<li><code>api_key</code>: the API key for your Zulip bot</li>
|
||||
<li><code>stream</code>: the stream your WordPress notifications should be posted in</li>
|
||||
<li><code>topic</code>: the name of the topic in that stream</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<em>
|
||||
<b>Important:</b> the HookPress plugin requires parameters delimited by
|
||||
semicolons. If you have a self-installed blog, separate parameters with <code>;</code>
|
||||
instead of <code>&</code>.
|
||||
</em>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<img class="screenshot" src="/static/images/integrations/wordpress/wordpress_configure_url.png" />
|
||||
</p>
|
||||
|
||||
<p>
|
||||
When you are done, your configured webhook should look like this:
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<img class="screenshot" src="/static/images/integrations/wordpress/wordpress_config_done.png" />
|
||||
</p>
|
||||
|
||||
<p>
|
||||
When a new post is published, a message is created in the specified Zulip stream
|
||||
and topic.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<img class="screenshot" src="/static/images/integrations/wordpress/wordpress_post_created.png" />
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Congratulations! You're done!</b><br />
|
||||
</p>
|
||||
|
||||
<h3>Types of Actions</h3>
|
||||
|
||||
<p>
|
||||
To configure other actions, choose a supported action from the dropdown list and
|
||||
select the appropriate fields.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<table style="width: 100%" >
|
||||
<tr><td><b>Action</b></td><td><b>Required Fields</b></td><td><b>Blog Type</b></td></tr>
|
||||
<tr><td>publish_post</td><td>post_title, post_type, post_url</td><td>Both</td></tr>
|
||||
<tr><td>publish_page</td><td>post_title, post_type, post_url</td><td>Both</td></tr>
|
||||
<tr><td>user_register</td><td>display_name, user_email</td><td>Self-installed</td></tr>
|
||||
<tr><td>wp_login</td><td>user_login</td><td>Self-installed</td></tr>
|
||||
</table>
|
||||
</p>
|
||||
|
||||
101
zerver/webhooks/wordpress/tests.py
Normal file
101
zerver/webhooks/wordpress/tests.py
Normal file
@@ -0,0 +1,101 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from six import text_type
|
||||
from zerver.lib.test_classes import WebhookTestCase
|
||||
|
||||
class WordPressHookTests(WebhookTestCase):
|
||||
STREAM_NAME = 'wordpress'
|
||||
URL_TEMPLATE = "/api/v1/external/wordpress?api_key={api_key}"
|
||||
FIXTURE_DIR_NAME = 'wordpress'
|
||||
|
||||
def test_publish_post(self):
|
||||
# type: () -> None
|
||||
|
||||
expected_topic = u"WordPress Post"
|
||||
expected_message = u"New post published.\n[New Blog Post](http://example.com\n)"
|
||||
|
||||
self.send_and_test_stream_message('publish_post', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_publish_post_type_not_provided(self):
|
||||
# type: () -> None
|
||||
|
||||
expected_topic = u"WordPress Post"
|
||||
expected_message = u"New post published.\n[New Blog Post](http://example.com\n)"
|
||||
|
||||
self.send_and_test_stream_message('publish_post_type_not_provided',
|
||||
expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_publish_post_no_data_provided(self):
|
||||
# type: () -> None
|
||||
|
||||
# Note: the fixture includes 'hook=publish_post' because it's always added by HookPress
|
||||
expected_topic = u"WordPress Notification"
|
||||
expected_message = u"New post published.\n" + "[New WordPress Post](WordPress Post URL)"
|
||||
|
||||
self.send_and_test_stream_message('publish_post_no_data_provided',
|
||||
expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_publish_page(self):
|
||||
# type: () -> None
|
||||
|
||||
expected_topic = u"WordPress Page"
|
||||
expected_message = u"New page published.\n" + "[New Blog Page](http://example.com\n)"
|
||||
|
||||
self.send_and_test_stream_message('publish_page', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_user_register(self):
|
||||
# type: () -> None
|
||||
|
||||
expected_topic = u"New Blog Users"
|
||||
expected_message = u"New blog user registered.\nName: test_user\nemail: test_user@example.com\n"
|
||||
|
||||
self.send_and_test_stream_message('user_register', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_wp_login(self):
|
||||
# type: () -> None
|
||||
|
||||
expected_topic = u"New Login"
|
||||
expected_message = u"User testuser logged in."
|
||||
|
||||
self.send_and_test_stream_message('wp_login', expected_topic, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_unknown_action_no_data(self):
|
||||
# type: () -> None
|
||||
|
||||
# Mimic send_and_test_stream_message() to manually execute a negative test.
|
||||
# Otherwise its call to send_json_payload() would assert on the non-success
|
||||
# we are testing. The value of result is the error message the webhook should
|
||||
# return if no params are sent. The fixture for this test is an empty file.
|
||||
|
||||
# subscribe to the target stream
|
||||
self.subscribe_to_stream(self.TEST_USER_EMAIL, self.STREAM_NAME)
|
||||
|
||||
# post to the webhook url
|
||||
post_params = {'stream_name': self.STREAM_NAME,
|
||||
'content_type': 'application/x-www-form-urlencoded'}
|
||||
result = self.client_post(self.url, 'unknown_action', **post_params)
|
||||
|
||||
# check that we got the expected error message
|
||||
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress Action")
|
||||
|
||||
def test_unknown_action_no_hook_provided(self):
|
||||
# type: () -> None
|
||||
|
||||
# Similar to unknown_action_no_data, except the fixture contains valid blog post
|
||||
# params but without the hook parameter. This should also return an error.
|
||||
|
||||
self.subscribe_to_stream(self.TEST_USER_EMAIL, self.STREAM_NAME)
|
||||
post_params = {'stream_name': self.STREAM_NAME,
|
||||
'content_type': 'application/x-www-form-urlencoded'}
|
||||
result = self.client_post(self.url, 'unknown_action', **post_params)
|
||||
|
||||
self.assert_json_error(result, "Unknown WordPress webhook action: WordPress Action")
|
||||
|
||||
def get_body(self, fixture_name):
|
||||
# type: (text_type) -> text_type
|
||||
return self.fixture_data("wordpress", fixture_name, file_type="txt")
|
||||
47
zerver/webhooks/wordpress/view.py
Normal file
47
zerver/webhooks/wordpress/view.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Webhooks for external integrations.
|
||||
from __future__ import absolute_import
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from zerver.models import Client, get_client, UserProfile
|
||||
from zerver.lib.actions import check_send_message
|
||||
from zerver.lib.response import json_success, json_error
|
||||
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
||||
|
||||
from six import text_type
|
||||
|
||||
PUBLISH_POST_OR_PAGE_TEMPLATE = 'New {type} published.\n[{title}]({url})'
|
||||
USER_REGISTER_TEMPLATE = 'New blog user registered.\nName: {name}\nemail: {email}'
|
||||
WP_LOGIN_TEMPLATE = 'User {name} logged in.'
|
||||
|
||||
@api_key_only_webhook_view("Wordpress")
|
||||
@has_request_variables
|
||||
def api_wordpress_webhook(request, user_profile, client,
|
||||
stream=REQ(default="wordpress"),
|
||||
topic=REQ(default="WordPress Notification"),
|
||||
hook=REQ(default="WordPress Action"),
|
||||
post_title=REQ(default="New WordPress Post"),
|
||||
post_type=REQ(default="post"),
|
||||
post_url=REQ(default="WordPress Post URL"),
|
||||
display_name=REQ(default="New User Name"),
|
||||
user_email=REQ(default="New User Email"),
|
||||
user_login=REQ(default="Logged in User")):
|
||||
# type: (HttpRequest, UserProfile, Client, text_type, text_type, text_type, text_type, text_type, text_type, text_type, text_type, text_type) -> HttpResponse
|
||||
|
||||
# remove trailing whitespace (issue for some test fixtures)
|
||||
hook = hook.rstrip()
|
||||
|
||||
if hook == 'publish_post' or hook == 'publish_page':
|
||||
data = PUBLISH_POST_OR_PAGE_TEMPLATE.format(type=post_type, title=post_title, url=post_url)
|
||||
|
||||
elif hook == 'user_register':
|
||||
data = USER_REGISTER_TEMPLATE.format(name=display_name, email=user_email)
|
||||
|
||||
elif hook == 'wp_login':
|
||||
data = WP_LOGIN_TEMPLATE.format(name=user_login)
|
||||
|
||||
else:
|
||||
return json_error(_("Unknown WordPress webhook action: " + hook))
|
||||
|
||||
check_send_message(user_profile, get_client("ZulipWordPressWebhook"), "stream",
|
||||
[stream], topic, data)
|
||||
return json_success()
|
||||
Reference in New Issue
Block a user