예제 #1
0
파일: views.py 프로젝트: ymping/hue
def handle_query_bank(channel_id, user_id):
    data = get_all_queries()

    message_block = [
        {
            "type": "section",
            "text": {
                "type":
                "mrkdwn",
                "text":
                "Hi <@{user}>, here is the list of all saved queries!".format(
                    user=user_id)
            }
        },
        {
            "type": "divider"
        },
    ]

    for query in data:
        statement = query['data']['query']['statement']
        query_element = {
            "type": "section",
            "text": {
                "type":
                "mrkdwn",
                "text":
                "*Name:* {name} \n *Statement:*\n ```{statement}```".format(
                    name=query['name'], statement=statement)
            },
        }
        message_block.append(query_element)

    _send_message(channel_id, block_element=message_block)
예제 #2
0
파일: views.py 프로젝트: jkillian/hue
def get_user(channel_id, slack_user):
  try:
    return User.objects.get(username=slack_user.get('user_email_prefix'))
  except User.DoesNotExist:
    bot_message = 'Corresponding Hue user not found or does not have access to the query'
    _send_message(channel_id, bot_message)
    raise PopupException(_("Slack user does not have access to the query"), error_code=200)
예제 #3
0
파일: views.py 프로젝트: ymping/hue
def get_user(channel_id, slack_user, message_ts):
    try:
        return User.objects.get(username=slack_user.get('user_email_prefix'))
    except User.DoesNotExist:
        err_msg = 'Corresponding Hue user not found or does not have access.'
        _send_message(channel_id, message=err_msg, message_ts=message_ts)
        raise SlackBotException(
            _("Slack user does not have access to the query"))
예제 #4
0
def handle_on_message(channel_id, bot_id, text, user_id):
    # Ignore bot's own message since that will cause an infinite loop of messages if we respond.
    if bot_id is not None:
        return HttpResponse(status=200)

    if slack_client is not None:
        if text and 'hello hue' in text.lower():
            bot_message = 'Hi <@{user}> :wave:'.format(user=user_id)
            _send_message(channel_id, bot_message)
예제 #5
0
파일: views.py 프로젝트: ymping/hue
def _make_select_statement_gist(host_domain, is_http_secure, user, channel_id,
                                statement):
    default_dialect = get_cluster_config(
        rewrite_user(user))['main_button_action']['dialect']
    gist_response = _gist_create(host_domain, is_http_secure, user, statement,
                                 default_dialect)

    msg = 'Here is the gist link\n {gist_link}'.format(
        gist_link=gist_response['link'])
    _send_message(channel_id, message=msg)
예제 #6
0
파일: views.py 프로젝트: ymping/hue
def handle_on_app_mention(host_domain, channel_id, user_id, text, message_ts):
    if text and 'help' in text:
        help_msg_block = help_message(user_id)
        _send_message(channel_id, block_element=help_msg_block)

    if text and 'queries' in text:
        slack_user = check_slack_user_permission(host_domain, user_id)
        user = get_user(channel_id, slack_user, message_ts)

        handle_query_bank(channel_id, user_id)
예제 #7
0
파일: views.py 프로젝트: ymping/hue
def handle_select_statement(host_domain, is_http_secure, channel_id, user_id,
                            statement, message_ts):
    msg = 'Hi <@{user}> \n Looks like you are copy/pasting SQL, instead now you can send Editor links which unfurls in a rich preview!'.format(
        user=user_id)
    _send_message(channel_id, message=msg)

    # Check Slack user perms to send gist link
    slack_user = check_slack_user_permission(host_domain, user_id)
    user = get_user(channel_id, slack_user, message_ts)

    _make_select_statement_gist(host_domain, is_http_secure, user, channel_id,
                                statement)
예제 #8
0
파일: views.py 프로젝트: jkillian/hue
def handle_on_message(channel_id, bot_id, text, user_id):
  # Ignore bot's own message since that will cause an infinite loop of messages if we respond.
  if bot_id is not None:
    return HttpResponse(status=200)
  
  if slack_client is not None:
    if text and 'hello hue' in text.lower():
      bot_message = 'Hi <@{user}> :wave:'.format(user=user_id)
      _send_message(channel_id, bot_message)

    if text and 'select 1' in text.lower():
      raw_sql_message = 'Hi <@{user}>\nInstead of copy/pasting SQL, now you can send Editor links which unfurls in a rich preview!'.format(user=user_id)
      _send_ephemeral_message(channel_id, user_id, raw_sql_message)
예제 #9
0
def handle_on_link_shared(channel_id, message_ts, links, user_id):
    for item in links:
        path = urlsplit(item['url'])[2]
        id_type, qid = urlsplit(item['url'])[3].split('=')
        query_id = {'id': qid} if qid.isdigit() else {'uuid': qid}

        try:
            if path == '/hue/editor' and id_type == 'editor':
                doc = Document2.objects.get(**query_id)
                doc_type = 'query'
            elif path == '/hue/gist' and id_type == 'uuid':
                doc = _get_gist_document(**query_id)
                doc_type = 'gist'
            else:
                raise PopupException(_("Cannot unfurl link"))
        except Document2.DoesNotExist:
            msg = "Document with {key} does not exist".format(key=query_id)
            raise PopupException(_(msg))

        # Permission check for Slack user to be Hue user
        try:
            slack_user = slack_user_check(user_id)
            user = User.objects.get(
                username=slack_user['user_email_prefix']
            ) if not slack_user['is_bot'] else doc.owner
        except User.DoesNotExist:
            bot_message = 'Corresponding Hue user not found or does not have access to the query'
            _send_message(channel_id, bot_message)
            raise PopupException(
                _("Slack user does not have access to the query"))

        doc.can_read_or_exception(user)

        # Mock request for query execution and fetch result
        user = rewrite_user(user)
        request = MockRequest(user=user)

        payload = _make_unfurl_payload(request, item['url'], id_type, doc,
                                       doc_type)
        try:
            slack_client.chat_unfurl(channel=channel_id,
                                     ts=message_ts,
                                     unfurls=payload['payload'])
        except Exception as e:
            raise PopupException(_("Cannot unfurl link"), detail=e)

        # Generate and upload result xlsx file only if result available
        if payload['file_status']:
            send_result_file(request, channel_id, message_ts, doc, 'xls')
예제 #10
0
def handle_on_message(host_domain, is_http_secure, channel_id, bot_id,
                      elements, user_id):
    # Ignore bot's own message since that will cause an infinite loop of messages if we respond.
    if bot_id is not None:
        return HttpResponse(status=200)

    for element_block in elements:
        text = element_block['elements'][0].get(
            'text', '') if element_block.get('elements') else ''

        if 'hello hue' in text.lower():
            bot_message = 'Hi <@{user}> :wave:'.format(user=user_id)
            _send_message(channel_id, bot_message)

        if text.lower().startswith('select'):
            detect_select_statement(host_domain, is_http_secure, channel_id,
                                    user_id, text.lower())
예제 #11
0
def detect_select_statement(host_domain, is_http_secure, channel_id, user_id,
                            statement):
    slack_user = check_slack_user_permission(host_domain, user_id)
    user = get_user(channel_id, slack_user)

    default_dialect = get_cluster_config(
        rewrite_user(user))['default_sql_interpreter']

    gist_response = _gist_create(host_domain, is_http_secure, user, statement,
                                 default_dialect)

    bot_message = (
        'Hi <@{user}>\n'
        'Looks like you are copy/pasting SQL, instead now you can send Editor links which unfurls in a rich preview!\n'
        'Here is the gist link\n {gist_link}').format(
            user=user_id, gist_link=gist_response['link'])
    _send_message(channel_id, bot_message)
예제 #12
0
파일: views.py 프로젝트: ymping/hue
def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
    for item in links:
        path = urlsplit(item['url'])[2]
        id_type, qid = urlsplit(item['url'])[3].split('=')
        query_id = {'id': qid} if qid.isdigit() else {'uuid': qid}

        try:
            if path == '/hue/editor' and id_type == 'editor':
                doc = Document2.objects.get(**query_id)
                doc_type = 'query'
            elif path == '/hue/gist' and id_type == 'uuid':
                doc = _get_gist_document(**query_id)
                doc_type = 'gist'
            else:
                err_msg = 'Could not access the query, please check the link again.'
                _send_message(channel_id,
                              message=err_msg,
                              message_ts=message_ts)
                raise SlackBotException(_("Cannot unfurl link"))
        except Document2.DoesNotExist:
            err_msg = 'Query document not found or does not exist.'
            _send_message(channel_id, message=err_msg, message_ts=message_ts)

            msg = "Document with {key} does not exist".format(key=query_id)
            raise SlackBotException(_(msg))

        # Permission check for Slack user to be Hue user
        slack_user = check_slack_user_permission(host_domain, user_id)
        user = get_user(channel_id, slack_user,
                        message_ts) if not slack_user['is_bot'] else doc.owner
        doc.can_read_or_exception(user)

        request = MockRequest(user=rewrite_user(user))

        payload = _make_unfurl_payload(request, item['url'], id_type, doc,
                                       doc_type)
        try:
            slack_client.chat_unfurl(channel=channel_id,
                                     ts=message_ts,
                                     unfurls=payload['payload'])
        except Exception as e:
            raise SlackBotException(_("Cannot unfurl link"), detail=e)

        # Generate and upload result xlsx file only if result available
        if payload['file_status']:
            send_result_file(request, channel_id, message_ts, doc, 'xls')