snowflakedb / SnowAlert

Security Analytics Using The Snowflake Data Warehouse
Apache License 2.0
182 stars 57 forks source link

Slack group messages #545

Closed sfc-gh-rmahajan closed 3 years ago

sfc-gh-rmahajan commented 3 years ago

Slack Group Messages

case 1: When single recipient_email is passed i.e direct message:

Screenshot 2021-06-25 at 1 02 41 PM

Output:

Screenshot 2021-06-25 at 1 11 59 PM

Explaination: Here, for Single Email, string will be be passed as usual.

    if isinstance(recipient_email, str):
        if recipient_email is not None:
            result = sc.api_call("users.lookupByEmail", email=recipient_email)

        # log.info(f'Slack user info for {email}', result)

        if result['ok'] is True and 'error' not in result:
            user = result['user']
            channel_id = user['id']
        else:
            log.error(f'Cannot identify  Slack user for email {recipient_email}')
            return None

case 2: When more than one recipient_email is passed i.e group slack creation:

Screenshot 2021-06-25 at 1 08 42 PM

Output:

Screenshot 2021-06-25 at 1 09 09 PM

Explaination: If we get a list of emails, we need to iterate and get userId for each email and store it in as a list and then send all the ids as string by calling an API.

  for email in recipient_email:
      response = sc.api_call("users.lookupByEmail", email=email)
      if not response['ok']:
          log.error(f'Cannot identify Slack user for email {email}')
          continue
      users.append(response['user']['id'])
  user_ids = ",".join(users)                  
  result = sc.api_call("conversations.open", users=user_ids)