2017-01-30 21:18:41 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.actions import check_send_stream_message, \
|
|
|
|
create_stream_if_needed
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_error, json_success
|
|
|
|
from zerver.lib.validator import check_int, check_string
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2017-01-30 21:18:41 +01:00
|
|
|
|
2017-07-20 03:30:54 +02:00
|
|
|
ZULIP_MESSAGE_TEMPLATE = u"**{message_sender}**: `{text}`"
|
2017-01-30 21:18:41 +01:00
|
|
|
VALID_OPTIONS = {'SHOULD_NOT_BE_MAPPED': '0', 'SHOULD_BE_MAPPED': '1'}
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Slack')
|
|
|
|
@has_request_variables
|
2017-12-24 15:47:06 +01:00
|
|
|
def api_slack_webhook(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_name: str=REQ(),
|
|
|
|
text: str=REQ(),
|
|
|
|
channel_name: str=REQ(),
|
|
|
|
stream: str=REQ(default='slack'),
|
|
|
|
channels_map_to_topics: str=REQ(default='1')) -> HttpRequest:
|
2017-01-30 21:18:41 +01:00
|
|
|
|
|
|
|
if channels_map_to_topics not in list(VALID_OPTIONS.values()):
|
|
|
|
return json_error(_('Error: channels_map_to_topics parameter other than 0 or 1'))
|
|
|
|
|
|
|
|
if channels_map_to_topics == VALID_OPTIONS['SHOULD_BE_MAPPED']:
|
|
|
|
subject = "channel: {}".format(channel_name)
|
|
|
|
else:
|
|
|
|
stream = channel_name
|
|
|
|
subject = _("Message from Slack")
|
|
|
|
|
|
|
|
content = ZULIP_MESSAGE_TEMPLATE.format(message_sender=user_name, text=text)
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client, stream, subject, content)
|
2017-01-30 21:18:41 +01:00
|
|
|
return json_success()
|