Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add Uptime Robot integration.
Adam Birds589a7799dcc9
diff --git a/static/images/integrations/bot_avatars/uptimerobot.png b/static/images/integrations/bot_avatars/uptimerobot.pngnew file mode 100644index 00000000000..9e8030e38b3Binary files /dev/null and b/static/images/integrations/bot_avatars/uptimerobot.png differdiff --git a/static/images/integrations/logos/uptimerobot.svg b/static/images/integrations/logos/uptimerobot.svgnew file mode 100644index 00000000000..87117972f84--- /dev/null+++ b/static/images/integrations/logos/uptimerobot.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512"><g fill="none" fill-rule="evenodd"><circle cx="256" cy="256" r="256" fill="#2A8C00"/><circle cx="256.5" cy="256.5" r="141.5" fill="#20201F" stroke="#FFF" stroke-width="20"/></g></svg>\ No newline at end of filediff --git a/static/images/integrations/uptimerobot/001.png b/static/images/integrations/uptimerobot/001.pngnew file mode 100644index 00000000000..1623b451d7eBinary files /dev/null and b/static/images/integrations/uptimerobot/001.png differdiff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.pyindex 451fff05548..389d2aead85 100644--- a/zerver/lib/integrations.py+++ b/zerver/lib/integrations.py@@ -445,6 +445,7 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [WebhookIntegration("travis", ["continuous-integration"], display_name="Travis CI"),WebhookIntegration("trello", ["project-management"]),WebhookIntegration("updown", ["monitoring"]),+ WebhookIntegration("uptimerobot", ["monitoring"], display_name="Uptime Robot"),WebhookIntegration("yo",["communication"],@@ -781,6 +782,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[BaseScreenshotConfig]] = {"travis": [ScreenshotConfig("build.json", payload_as_query_param=True)],"trello": [ScreenshotConfig("adding_comment_to_card.json")],"updown": [ScreenshotConfig("check_multiple_events.json")],+ "uptimerobot": [ScreenshotConfig("uptimerobot_monitor_up.json")],"wordpress": [ScreenshotConfig("publish_post.txt", "wordpress_post_created.png")],"yo": [ScreenshotConfig(diff --git a/zerver/webhooks/uptimerobot/__init__.py b/zerver/webhooks/uptimerobot/__init__.pynew file mode 100644index 00000000000..e69de29bb2ddiff --git a/zerver/webhooks/uptimerobot/doc.md b/zerver/webhooks/uptimerobot/doc.mdnew file mode 100644index 00000000000..e46df8a7736--- /dev/null+++ b/zerver/webhooks/uptimerobot/doc.md@@ -0,0 +1,41 @@+Receive Zulip notifications from Uptime Robot!++1. {!create-stream.md!}++1. {!create-a-bot-indented.md!}++ Construct the URL for the {{ integration_display_name }}+ bot using the bot's API key and the desired stream name:++ `{{ api_url }}{{ integration_url }}?api_key=abcdefgh&stream=stream%20name&`++ Modify the parameters of the URL above, where `api_key` is the API key+ of your Zulip bot, and `stream` is the URL-encoded stream name you want the+ notifications sent to. If you do not specify a `stream`, the bot will+ send notifications via PMs to the creator of the bot.++1. On Uptime Robot, go to **My Settings** and select **Add Alert Contact**.+ Set **Alert Contact Type** to **webhook**.++1. Set **Friendly Name** to a name of your choice, such as `Zulip`. Set **URL to notify** to the+ URL constructed above. Under **POST Value (JSON Format)**, select **Send as JSON (application/json)**.++1. Set **POST Value(JSON Format)** to:++ ```+ {+ "monitor_url":"*monitorURL*",+ "monitor_friendly_name":"*monitorFriendlyName*",+ "alert_type":"*alertType*",+ "alert_type_friendly_name":"*alertTypeFriendlyName*",+ "alert_details":"*alertDetails*",+ "alert_friendly_duration":"*alertFriendlyDuration*"+ }+ ```++1. Set **Enable Notifications For** to whichever events you want to notify on.+ Save the form.++{!congrats.md!}++diff --git a/zerver/webhooks/uptimerobot/fixtures/uptimerobot_invalid_payload_with_missing_data.json b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_invalid_payload_with_missing_data.jsonnew file mode 100644index 00000000000..26b70ffbabb--- /dev/null+++ b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_invalid_payload_with_missing_data.json@@ -0,0 +1,7 @@+{+ "monitor_url": "server1.example.com",+ "alert_type": "1",+ "alert_type_friendly_name": "Down",+ "alert_details": "Host Is Unreachable",+ "alert_friendly_duration": "*alertFriendlyDuration*"+ }diff --git a/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_down.json b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_down.jsonnew file mode 100644index 00000000000..5318c50fd0e--- /dev/null+++ b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_down.json@@ -0,0 +1,8 @@+{+ "monitor_url": "server1.example.com",+ "monitor_friendly_name": "Web Server",+ "alert_type": "1",+ "alert_type_friendly_name": "Down",+ "alert_details": "Host Is Unreachable",+ "alert_friendly_duration": "*alertFriendlyDuration*"+ }diff --git a/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_up.json b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_up.jsonnew file mode 100644index 00000000000..5f5721c86d2--- /dev/null+++ b/zerver/webhooks/uptimerobot/fixtures/uptimerobot_monitor_up.json@@ -0,0 +1,8 @@+{+ "monitor_url": "server2.example.com",+ "monitor_friendly_name": "Mail Server",+ "alert_type": "2",+ "alert_type_friendly_name": "Up",+ "alert_details": "Host Is Reachable",+ "alert_friendly_duration": "44 minutes and 37 seconds"+ }diff --git a/zerver/webhooks/uptimerobot/tests.py b/zerver/webhooks/uptimerobot/tests.pynew file mode 100644index 00000000000..30962b19bd1--- /dev/null+++ b/zerver/webhooks/uptimerobot/tests.py@@ -0,0 +1,47 @@+from zerver.lib.send_email import FromAddress+from zerver.lib.test_classes import WebhookTestCase+from zerver.models import Recipient+from zerver.webhooks.uptimerobot.view import MISCONFIGURED_PAYLOAD_ERROR_MESSAGE+++class UptimeRobotHookTests(WebhookTestCase):+ STREAM_NAME = "uptimerobot"+ URL_TEMPLATE = "/api/v1/external/uptimerobot?stream={stream}&api_key={api_key}"+ FIXTURE_DIR_NAME = "uptimerobot"++ def test_uptimerobot_monitor_down(self) -> None:+ """+ Tests if uptimerobot monitor down is handled correctly+ """+ expected_topic = "Web Server"+ expected_message = "Web Server (server1.example.com) is DOWN (Host Is Unreachable)."+ self.check_webhook("uptimerobot_monitor_down", expected_topic, expected_message)++ def test_uptimerobot_monitor_up(self) -> None:+ """+ Tests if uptimerobot monitor up is handled correctly+ """+ expected_topic = "Mail Server"+ expected_message = """+Mail Server (server2.example.com) is back UP (Host Is Reachable).+It was down for 44 minutes and 37 seconds.+""".strip()+ self.check_webhook("uptimerobot_monitor_up", expected_topic, expected_message)++ def test_uptimerobot_invalid_payload_with_missing_data(self) -> None:+ """+ Tests if invalid uptime robot payloads are handled correctly+ """+ self.url = self.build_webhook_url()+ payload = self.get_body("uptimerobot_invalid_payload_with_missing_data")+ result = self.client_post(self.url, payload, content_type="application/json")+ self.assert_json_error(result, "Invalid payload")++ expected_message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(+ bot_name=self.test_user.full_name,+ support_email=FromAddress.SUPPORT,+ ).strip()++ msg = self.get_last_message()+ self.assertEqual(msg.content, expected_message)+ self.assertEqual(msg.recipient.type, Recipient.PERSONAL)diff --git a/zerver/webhooks/uptimerobot/view.py b/zerver/webhooks/uptimerobot/view.pynew file mode 100644index 00000000000..2165d27f848--- /dev/null+++ b/zerver/webhooks/uptimerobot/view.py@@ -0,0 +1,65 @@+from typing import Any, Dict++from django.http import HttpRequest, HttpResponse+from django.utils.translation import ugettext as _++from zerver.decorator import REQ, has_request_variables, webhook_view+from zerver.lib.actions import send_rate_limited_pm_notification_to_bot_owner+from zerver.lib.response import json_error, json_success+from zerver.lib.send_email import FromAddress+from zerver.lib.webhooks.common import check_send_webhook_message+from zerver.models import UserProfile++MISCONFIGURED_PAYLOAD_ERROR_MESSAGE = """+Hi there! Your bot {bot_name} just received a Uptime Robot payload that is missing+some data that Zulip requires. This usually indicates a configuration issue+in your Uptime Robot webhook settings. Please make sure that you set the required parameters+when configuring the Uptime Robot webhook. Contact {support_email} if you+need further help!+"""++UPTIMEROBOT_TOPIC_TEMPLATE = "{monitor_friendly_name}"+UPTIMEROBOT_MESSAGE_UP_TEMPLATE = """+{monitor_friendly_name} ({monitor_url}) is back UP ({alert_details}).+It was down for {alert_friendly_duration}.+""".strip()+UPTIMEROBOT_MESSAGE_DOWN_TEMPLATE = (+ "{monitor_friendly_name} ({monitor_url}) is DOWN ({alert_details})."+)+++@webhook_view("UptimeRobot")+@has_request_variables+def api_uptimerobot_webhook(+ request: HttpRequest,+ user_profile: UserProfile,+ payload: Dict[str, Any] = REQ(argument_type="body"),+) -> HttpResponse:++ try:+ body = get_body_for_http_request(payload)+ subject = get_subject_for_http_request(payload)+ except KeyError:+ message = MISCONFIGURED_PAYLOAD_ERROR_MESSAGE.format(+ bot_name=user_profile.full_name,+ support_email=FromAddress.SUPPORT,+ ).strip()+ send_rate_limited_pm_notification_to_bot_owner(user_profile, user_profile.realm, message)++ return json_error(_("Invalid payload"))++ check_send_webhook_message(request, user_profile, subject, body)+ return json_success()+++def get_subject_for_http_request(payload: Dict[str, Any]) -> str:+ return UPTIMEROBOT_TOPIC_TEMPLATE.format(monitor_friendly_name=payload["monitor_friendly_name"])+++def get_body_for_http_request(payload: Dict[str, Any]) -> str:+ if payload["alert_type_friendly_name"] == "Up":+ body = UPTIMEROBOT_MESSAGE_UP_TEMPLATE.format(**payload)+ elif payload["alert_type_friendly_name"] == "Down":+ body = UPTIMEROBOT_MESSAGE_DOWN_TEMPLATE.format(**payload)++ return body