Slack

This module connects the LNLDB with our Slack workspace using the Slack API. This integration relies on our own custom app (a bot) which has already been installed into the workspace.

Models

class slack.models.ReportedMessage(*args, **kwargs)[source]

Used when users report problematic Slack messages

Parameters:
  • id (AutoField) – Id (required)
  • message (ForeignKey to SlackMessage) – Message (required)
  • comments (TextField) – Comments
  • reported_by (CharField) – Reported by (required)
  • reported_on (DateTimeField) – Reported on (required)
  • resolved (BooleanField) – Resolved (default=False)
exception DoesNotExist
exception MultipleObjectsReturned
comments

TextField(blank=True, null=True)

get_next_by_reported_on(*, field=<django.db.models.fields.DateTimeField: reported_on>, is_next=True, **kwargs)
get_previous_by_reported_on(*, field=<django.db.models.fields.DateTimeField: reported_on>, is_next=False, **kwargs)
id

AutoField(verbose_name=”ID”, primary_key=True, serialize=False, auto_created=True)

message

ForeignKey(related_name=”reports”, on_delete=CASCADE(), to= SlackMessage)

message_id

Raw (integer) FK for message

objects = <django.db.models.manager.Manager object>
reported_by

CharField(max_length=12)

reported_on

DateTimeField(auto_now_add=True)

resolved

BooleanField(default=False)

class slack.models.SlackMessage(*args, **kwargs)[source]

Used for archiving slack messages

Parameters:
  • id (AutoField) – Id (required)
  • posted_to (CharField) – Posted to (required)
  • posted_by (CharField) – Posted by (required)
  • content (TextField) – Content (required)
  • blocks (BooleanField) – Blocks (default=False)
  • ts (CharField) – Timestamp (required)
  • public (BooleanField) – Public (default=True)
  • parent (ForeignKey to SlackMessage) – Parent
  • deleted (BooleanField) – Remove from the Slack workspace? This action cannot be undone. (default=False)
exception DoesNotExist
exception MultipleObjectsReturned
blocks

BooleanField(default=False)

content

TextField()

deleted

BooleanField(verbose_name=”Remove from workspace”, default=False, help_text=”Remove from the Slack workspace? This action cannot be undone.”)

Remove from workspace: Remove from the Slack workspace? This action cannot be undone.

id

AutoField(verbose_name=”ID”, primary_key=True, serialize=False, auto_created=True)

objects = <django.db.models.manager.Manager object>
parent

ForeignKey(blank=True, null=True, on_delete=CASCADE(), to= SlackMessage)

parent_id

Raw (integer) FK for parent

posted_by

CharField(max_length=24)

posted_to

CharField(max_length=128)

public

BooleanField(default=True)

reports

Reverse Manager for slack.ReportedMessage’s message

slackmessage_set

Reverse Manager for slack.SlackMessage’s parent

ts

CharField(verbose_name=”Timestamp”, max_length=24)


API Methods

slack.api.channel_info(channel_id)[source]

Retrieves all the information about a channel

Parameters:channel_id – The ID of the channel
Returns:Channel details (Dictionary)
slack.api.check_presence(user)[source]

Gets user presence information from Slack (“active” or “away”)

Parameters:user – The identifier of the specified user
Returns:True if user is currently active, False if user is away
slack.api.delete_file(id)[source]
slack.api.handle_event(request)[source]

Event endpoint for the Slack API. Slack will send POST requests here whenever certain events have been triggered.

slack.api.handle_interaction(request)[source]

Interaction endpoint for the Slack API. Slack will send POST requests here when users interact with a shortcut or interactive component.

slack.api.join_channel(channel)[source]

If the app gets the ‘not_in_channel’ error when accessing a public channel, call this method

Parameters:channel – The channel to join
Returns:Response object (Dictionary)
slack.api.load_channels(archived=False)[source]

Get a list of all the public channels in Slack

Parameters:archived – Boolean - Include archived channels
Returns:Response object (Dictionary)
slack.api.lookup_user(email)[source]

Will search for a user in the Slack workspace using their email address

Parameters:email – The email address for the user
Returns:The identifier for the user in Slack (None if the search returns nothing)

Get a permalink for a specific message in Slack.

Parameters:
  • channel – The channel the message was posted in
  • message_id – The timestamp of the message
Returns:

Permalink URL

slack.api.message_react(channel, message, reaction)[source]

React to a Slack message

Parameters:
  • channel – The channel the message was posted to
  • message – The timestamp of the message
  • reaction – The name of the emoji to react to the message with
Returns:

Response object (Dictionary)

slack.api.message_unreact(channel, message, reaction)[source]

Remove a reaction from a Slack message

Parameters:
  • channel – The channel the message was posted to
  • message – The timestamp of the message
  • reaction – The name of the emoji to remove from the message
Returns:

Response object (Dictionary)

slack.api.open_modal(trigger_id, blocks)[source]

Opens a modal view (in Slack) in response to user action

Parameters:
Returns:

View ID if successful; None otherwise

slack.api.post_ephemeral(channel, text, user, username=None)[source]

Send an ephemeral message to a user in a channel. This message will only be visible to the target user.

Parameters:
Returns:

Response object (Dictionary)

slack.api.refresh_ticket_message(channel, message)[source]

Update a TFed ticket message with the latest information

Parameters:
  • channel – The channel the ticket was posted to
  • message – The original message object
Returns:

Response from Slack API after attempting to update the message

slack.api.replace_message(channel, message_id, text=None, content=None)[source]

Replace an existing message in Slack. The message will need to have been published by the bot.

The text parameter is not required when the content parameter is provided, however including it is still highly recommended.

Parameters:
Returns:

Response object (Dictionary)

slack.api.retrieve_message(channel, message_id)[source]

Retrieve a single message from Slack

Parameters:
  • channel – The channel the message was posted to
  • message_id – The timestamp of the message
Returns:

The message details

slack.api.slack_post(channel, thread=None, text=None, content=None, username=None, icon_url=None, attachment=None)[source]

Post a message on Slack

The text parameter is not required when the content parameter is provided, however including it is still highly recommended.

Parameters:
  • channel – The identifier of the Slack conversation to post to
  • thread – The timestamp of another message to post this message as a reply to
  • text – Message text (Formatting: https://api.slack.com/reference/surfaces/formatting)
  • content – List of valid blocks data (https://api.slack.com/block-kit)
  • username – Name displayed by the bot
  • icon_url – The URL to an image / icon to display next to the message (profile picture)
  • attachment – Dictionary with file details - {‘name’: ‘Example File’, ‘filepath’: ‘/media/slack/example.pdf’}
Returns:

Response object (Dictionary)

slack.api.upload(attachment, filename, title=None, message=None, channels=None)[source]

Upload a new file to Slack

Parameters:
  • attachment – File path to the file
  • filename – Filename with file extension (i.e. example.pdf)
  • title – Title of the file to display in Slack
  • message – The message text introducing the file in the specified channels
  • channels – Comma-separated list of channel names or ids where the file should be posted (i.e. C1234567890)
Returns:

Response object (Dictionary)

slack.api.user_add(channel, users)[source]

Invite users to join a slack channel. The bot must be a member of the channel.

Parameters:
  • channel – The identifier of the Slack channel to invite the users to
  • users – The identifiers of the specified users (List of up to 1000)
Returns:

Response object (Dictionary)

slack.api.user_kick(channel, user)[source]

Remove a user from a slack channel. The bot must be a member of the channel.

Parameters:
  • channel – The identifier of the Slack channel to remove users from
  • user – The identifier of the specified user
Returns:

Response object (Dictionary)

slack.api.user_profile(user_id)[source]

Get basic user profile information

Parameters:user_id – The identifier for the user in Slack (i.e. U123456789)
Returns:Slack user info (Dictionary)

Views

slack.views.app_home(tickets)[source]

Blocks for the App’s Home tab. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:tickets – A list of ticket ids
slack.views.cc_add_notification(cci)[source]

Blocks for a Crew Chief add notification. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:cci – EventCCInstance object
slack.views.cc_report_reminder(cci)[source]

Blocks for a missing crew chief report reminder Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:cci – EventCCInstance object
slack.views.event_edited_notification(event, triggered_by, fields_changed)[source]

Blocks for an event edited Slack notification. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:
  • event – The event object
  • triggered_by – The user that edited the event
  • fields_changed – A list of fields that have changed
slack.views.generate_button(text, value, style='default', emoji=False, action_suffix='action')[source]

Generate a Block Kit button

Parameters:
  • text – The button text
  • style – Style of button (Must be “default”, “primary”, or “danger”)
  • value – Button value
  • emoji – Boolean indicating whether or not to permit emoji in the button text
  • action_suffix – Defaults to “action”. Will be appended to value to create action_id
Returns:

Button block dictionary

slack.views.generate_modal(title, callback_id, blocks)[source]

Generate a modal view object using Slack’s BlockKit

Parameters:
  • title – Title to display at the top of the modal view
  • callback_id – Identifier used to help determine the type of modal view in future responses
  • blocks – Blocks to add to the modal view
Returns:

View object (Dictionary)

slack.views.report_list(request)[source]

View Slack messages reported for moderation

slack.views.report_message_modal(message)[source]

Blocks for the modal view that is displayed when a user reports a problematic Slack message. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:message – A SlackMessage object representing the message
slack.views.reported_message_notification(sender, report)[source]

Blocks for the notification sent to the Webmaster whenever a new report is submitted. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:
  • sender – The Slack ID for the user that sent the report
  • report – The corresponding ReportedMessage object
slack.views.tfed_modal()[source]

Blocks for the TFed ticket submission form. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

slack.views.tfed_ticket(ticket)[source]

Generate blocks for TFed ticket response message. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

slack.views.ticket_comment_modal(ticket_id)[source]

Blocks for the TFed ticket comment modal. Can be launched in the App Home tab. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

slack.views.ticket_update_modal(ticket_id, channel, timestamp, action)[source]

Blocks for the TFed ticket update form. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)

Parameters:
  • ticket_id – The ticket # for the ticket being updated
  • channel – The channel the ticket is being updated from
  • timestamp – The timestamp of the message that triggered this action
  • action – The type of update operation (Options: ‘open’, ‘update’, ‘close’)
slack.views.view_report(request, pk)[source]

View full report for a Slack message that has been flagged for moderation

slack.views.welcome_message()[source]

Blocks for the Welcome Message. This message will be displayed to new users joining the Slack workspace. Generated using the Block Kit Builder (https://app.slack.com/block-kit-builder)