2019-03-28 21:32:28 +01:00
|
|
|
from html.parser import HTMLParser
|
2022-10-08 20:55:55 +02:00
|
|
|
from typing import Callable, Dict, List, Tuple
|
2018-01-06 12:11:44 +01:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2023-09-11 20:22:32 +02:00
|
|
|
from returns.curry import partial
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2018-01-06 12:11:44 +01:00
|
|
|
|
2023-01-06 12:27:53 +01:00
|
|
|
from zerver.decorator import return_success_on_head_request, webhook_view
|
2022-11-17 09:30:48 +01:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
|
2018-01-06 12:11:44 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_int, check_none_or, check_string
|
2020-08-19 22:14:40 +02:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2018-01-06 12:11:44 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
COMPANY_CREATED = """
|
|
|
|
New company **{name}** created:
|
|
|
|
* **User count**: {user_count}
|
|
|
|
* **Monthly spending**: {monthly_spend}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_EMAIL_ADDED = "New email {email} added to contact."
|
|
|
|
|
|
|
|
CONTACT_CREATED = """
|
|
|
|
New contact created:
|
|
|
|
* **Name (or pseudonym)**: {name}
|
|
|
|
* **Email**: {email}
|
|
|
|
* **Location**: {location_info}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_SIGNED_UP = """
|
|
|
|
Contact signed up:
|
|
|
|
* **Email**: {email}
|
|
|
|
* **Location**: {location_info}
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONTACT_TAG_CREATED = "Contact tagged with the `{name}` tag."
|
|
|
|
|
|
|
|
CONTACT_TAG_DELETED = "The tag `{name}` was removed from the contact."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_ASSIGNED = "{name} assigned to conversation."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_TEMPLATE = "{admin_name} {action} the conversation."
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_REPLY_TEMPLATE = """
|
|
|
|
{admin_name} {action} the conversation:
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
CONVERSATION_ADMIN_INITIATED_CONVERSATION = """
|
|
|
|
{admin_name} initiated a conversation:
|
|
|
|
|
|
|
|
``` quote
|
|
|
|
{content}
|
|
|
|
```
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
EVENT_CREATED = "New event **{event_name}** created."
|
|
|
|
|
|
|
|
USER_CREATED = """
|
|
|
|
New user created:
|
|
|
|
* **Name**: {name}
|
|
|
|
* **Email**: {email}
|
|
|
|
""".strip()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
class MLStripper(HTMLParser):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.reset()
|
|
|
|
self.strict = False
|
|
|
|
self.convert_charrefs = True
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.fed: List[str] = []
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2019-03-28 21:32:28 +01:00
|
|
|
def handle_data(self, d: str) -> None:
|
|
|
|
self.fed.append(d)
|
|
|
|
|
|
|
|
def get_data(self) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
return "".join(self.fed)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def strip_tags(html: str) -> str:
|
|
|
|
s = MLStripper()
|
|
|
|
s.feed(html)
|
|
|
|
return s.get_data()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_topic_for_contacts(user: WildValue) -> str:
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = "{type}: {name}".format(
|
2022-10-08 20:55:55 +02:00
|
|
|
type=user["type"].tame(check_string).capitalize(),
|
|
|
|
name=user.get("name").tame(check_none_or(check_string))
|
|
|
|
or user.get("pseudonym").tame(check_none_or(check_string))
|
|
|
|
or user.get("email").tame(check_none_or(check_string)),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
return topic_name
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_company_created_message(payload: WildValue) -> Tuple[str, str]:
|
|
|
|
body = COMPANY_CREATED.format(
|
|
|
|
name=payload["data"]["item"]["name"].tame(check_string),
|
|
|
|
user_count=payload["data"]["item"]["user_count"].tame(check_int),
|
|
|
|
monthly_spend=payload["data"]["item"]["monthly_spend"].tame(check_int),
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
return ("Companies", body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_contact_added_email_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2022-10-08 20:55:55 +02:00
|
|
|
body = CONTACT_EMAIL_ADDED.format(email=user["email"].tame(check_string))
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_contact_created_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONTACT_CREATED.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
name=contact.get("name").tame(check_none_or(check_string))
|
|
|
|
or contact.get("pseudonym").tame(check_none_or(check_string)),
|
|
|
|
email=contact["email"].tame(check_string),
|
2019-03-28 21:32:28 +01:00
|
|
|
location_info="{city_name}, {region_name}, {country_name}".format(
|
2022-10-08 20:55:55 +02:00
|
|
|
city_name=contact["location_data"]["city_name"].tame(check_string),
|
|
|
|
region_name=contact["location_data"]["region_name"].tame(check_string),
|
|
|
|
country_name=contact["location_data"]["country_name"].tame(check_string),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(contact)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_contact_signed_up_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONTACT_SIGNED_UP.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
email=contact["email"].tame(check_string),
|
2019-03-28 21:32:28 +01:00
|
|
|
location_info="{city_name}, {region_name}, {country_name}".format(
|
2022-10-08 20:55:55 +02:00
|
|
|
city_name=contact["location_data"]["city_name"].tame(check_string),
|
|
|
|
region_name=contact["location_data"]["region_name"].tame(check_string),
|
|
|
|
country_name=contact["location_data"]["country_name"].tame(check_string),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
),
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(contact)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_contact_tag_created_message(payload: WildValue) -> Tuple[str, str]:
|
|
|
|
body = CONTACT_TAG_CREATED.format(
|
|
|
|
name=payload["data"]["item"]["tag"]["name"].tame(check_string)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]["contact"]
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(contact)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_contact_tag_deleted_message(payload: WildValue) -> Tuple[str, str]:
|
|
|
|
body = CONTACT_TAG_DELETED.format(
|
|
|
|
name=payload["data"]["item"]["tag"]["name"].tame(check_string)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
contact = payload["data"]["item"]["contact"]
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(contact)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_conversation_admin_assigned_message(payload: WildValue) -> Tuple[str, str]:
|
|
|
|
body = CONVERSATION_ADMIN_ASSIGNED.format(
|
|
|
|
name=payload["data"]["item"]["assignee"]["name"].tame(check_string)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_admin_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
action: str,
|
2024-04-29 23:52:26 +02:00
|
|
|
payload: WildValue,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_TEMPLATE.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
admin_name=assignee.get("name").tame(check_none_or(check_string)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
action=action,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_conversation_admin_reply_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
action: str,
|
2024-04-29 23:52:26 +02:00
|
|
|
payload: WildValue,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
note = payload["data"]["item"]["conversation_parts"]["conversation_parts"][0]
|
2022-10-08 20:55:55 +02:00
|
|
|
content = strip_tags(note["body"].tame(check_string))
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_REPLY_TEMPLATE.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
admin_name=assignee.get("name").tame(check_none_or(check_string)),
|
2019-03-28 21:32:28 +01:00
|
|
|
action=action,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_conversation_admin_single_created_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
assignee = payload["data"]["item"]["assignee"]
|
|
|
|
user = payload["data"]["item"]["user"]
|
2022-10-08 20:55:55 +02:00
|
|
|
conversation_body = payload["data"]["item"]["conversation_message"]["body"].tame(check_string)
|
2019-03-28 21:32:28 +01:00
|
|
|
content = strip_tags(conversation_body)
|
|
|
|
body = CONVERSATION_ADMIN_INITIATED_CONVERSATION.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
admin_name=assignee.get("name").tame(check_none_or(check_string)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_conversation_user_created_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
2022-10-08 20:55:55 +02:00
|
|
|
conversation_body = payload["data"]["item"]["conversation_message"]["body"].tame(check_string)
|
2019-03-28 21:32:28 +01:00
|
|
|
content = strip_tags(conversation_body)
|
|
|
|
body = CONVERSATION_ADMIN_INITIATED_CONVERSATION.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
admin_name=user.get("name").tame(check_none_or(check_string)),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_conversation_user_replied_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
note = payload["data"]["item"]["conversation_parts"]["conversation_parts"][0]
|
2022-10-08 20:55:55 +02:00
|
|
|
content = strip_tags(note["body"].tame(check_string))
|
2019-03-28 21:32:28 +01:00
|
|
|
body = CONVERSATION_ADMIN_REPLY_TEMPLATE.format(
|
2022-10-08 20:55:55 +02:00
|
|
|
admin_name=user.get("name").tame(check_none_or(check_string)),
|
2021-02-12 08:20:45 +01:00
|
|
|
action="replied to",
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
content=content,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_event_created_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
event = payload["data"]["item"]
|
2022-10-08 20:55:55 +02:00
|
|
|
body = EVENT_CREATED.format(event_name=event["event_name"].tame(check_string))
|
2021-02-12 08:20:45 +01:00
|
|
|
return ("Events", body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_user_created_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2022-10-08 20:55:55 +02:00
|
|
|
body = USER_CREATED.format(
|
|
|
|
name=user["name"].tame(check_string), email=user["email"].tame(check_string)
|
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_user_deleted_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, "User deleted.")
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_user_email_updated_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
2022-10-08 20:55:55 +02:00
|
|
|
body = "User's email was updated to {}.".format(user["email"].tame(check_string))
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-28 21:32:28 +01:00
|
|
|
def get_user_tagged_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
action: str,
|
2024-04-29 23:52:26 +02:00
|
|
|
payload: WildValue,
|
2019-03-28 21:32:28 +01:00
|
|
|
) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]["user"]
|
|
|
|
tag = payload["data"]["item"]["tag"]
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
2021-02-12 08:20:45 +01:00
|
|
|
body = "The tag `{tag_name}` was {action} the user.".format(
|
2022-10-08 20:55:55 +02:00
|
|
|
tag_name=tag["name"].tame(check_string),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
action=action,
|
2019-03-28 21:32:28 +01:00
|
|
|
)
|
2024-01-17 15:53:30 +01:00
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
def get_user_unsubscribed_message(payload: WildValue) -> Tuple[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = payload["data"]["item"]
|
|
|
|
body = "User unsubscribed from emails."
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = get_topic_for_contacts(user)
|
|
|
|
return (topic_name, body)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 20:55:55 +02:00
|
|
|
EVENT_TO_FUNCTION_MAPPER: Dict[str, Callable[[WildValue], Tuple[str, str]]] = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"company.created": get_company_created_message,
|
|
|
|
"contact.added_email": get_contact_added_email_message,
|
|
|
|
"contact.created": get_contact_created_message,
|
|
|
|
"contact.signed_up": get_contact_signed_up_message,
|
|
|
|
"contact.tag.created": get_contact_tag_created_message,
|
|
|
|
"contact.tag.deleted": get_contact_tag_deleted_message,
|
|
|
|
"conversation.admin.assigned": get_conversation_admin_assigned_message,
|
2024-04-29 23:52:26 +02:00
|
|
|
"conversation.admin.closed": partial(get_conversation_admin_message, "closed"),
|
|
|
|
"conversation.admin.opened": partial(get_conversation_admin_message, "opened"),
|
|
|
|
"conversation.admin.snoozed": partial(get_conversation_admin_message, "snoozed"),
|
|
|
|
"conversation.admin.unsnoozed": partial(get_conversation_admin_message, "unsnoozed"),
|
|
|
|
"conversation.admin.replied": partial(get_conversation_admin_reply_message, "replied to"),
|
|
|
|
"conversation.admin.noted": partial(get_conversation_admin_reply_message, "added a note to"),
|
2021-02-12 08:20:45 +01:00
|
|
|
"conversation.admin.single.created": get_conversation_admin_single_created_message,
|
|
|
|
"conversation.user.created": get_conversation_user_created_message,
|
|
|
|
"conversation.user.replied": get_conversation_user_replied_message,
|
|
|
|
"event.created": get_event_created_message,
|
|
|
|
"user.created": get_user_created_message,
|
|
|
|
"user.deleted": get_user_deleted_message,
|
|
|
|
"user.email.updated": get_user_email_updated_message,
|
2024-04-29 23:52:26 +02:00
|
|
|
"user.tag.created": partial(get_user_tagged_message, "added to"),
|
|
|
|
"user.tag.deleted": partial(get_user_tagged_message, "removed from"),
|
2021-02-12 08:20:45 +01:00
|
|
|
"user.unsubscribed": get_user_unsubscribed_message,
|
2019-03-28 21:32:28 +01:00
|
|
|
# Note that we do not have a payload for visitor.signed_up
|
|
|
|
# but it should be identical to contact.signed_up
|
2021-02-12 08:20:45 +01:00
|
|
|
"visitor.signed_up": get_contact_signed_up_message,
|
2019-03-28 21:32:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = list(EVENT_TO_FUNCTION_MAPPER.keys())
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
|
|
|
|
@webhook_view("Intercom", all_event_types=ALL_EVENT_TYPES)
|
2023-01-06 12:27:53 +01:00
|
|
|
# Intercom sends a HEAD request to validate the webhook URL. In this case, we just assume success.
|
|
|
|
@return_success_on_head_request
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_intercom_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2022-10-08 20:55:55 +02:00
|
|
|
event_type = payload["topic"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
if event_type == "ping":
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2019-03-28 21:32:28 +01:00
|
|
|
|
2021-02-16 00:17:17 +01:00
|
|
|
handler = EVENT_TO_FUNCTION_MAPPER.get(event_type)
|
|
|
|
if handler is None:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise UnsupportedWebhookEventTypeError(event_type)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name, body = handler(payload)
|
2018-01-06 12:11:44 +01:00
|
|
|
|
2024-01-17 15:53:30 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body, event_type)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|