slack.sansio - Sans-I/O helpers

Collection of functions for sending and decoding request to or from the slack API

slack.sansio.ITERMODE = ('cursor', 'page', 'timeline')

Supported pagination mode

slack.sansio.RECONNECT_EVENTS = ('team_migration_started', 'goodbye')

Events type preceding a disconnection

slack.sansio.SKIP_EVENTS = ('reconnect_url',)

Events that do not need to be dispatched

slack.sansio.decode_body(headers: MutableMapping[KT, VT], body: bytes) → dict[source]

Decode the response body

For ‘application/json’ content-type load the body as a dictionary

Parameters:
  • headers – Response headers
  • body – Response body
Returns:

decoded body

slack.sansio.decode_iter_request(data: dict) → Union[str, int, None][source]

Decode incoming response from an iteration request

Parameters:data – Response data
Returns:Next itervalue
slack.sansio.decode_response(status: int, headers: MutableMapping[KT, VT], body: bytes) → dict[source]

Decode incoming response

Parameters:
  • status – Response status
  • headers – Response headers
  • body – Response body
Returns:

Response data

slack.sansio.discard_event(event: slack.events.Event, bot_id: str = None) → bool[source]

Check if the incoming event needs to be discarded

Parameters:
Returns:

boolean

slack.sansio.find_iteration(url: Union[slack.methods.Methods, str], itermode: Optional[str] = None, iterkey: Optional[str] = None) → Tuple[str, str][source]

Find iteration mode and iteration key for a given slack.methods

Parameters:
  • urlslack.methods or string url
  • itermode – Custom iteration mode
  • iterkey – Custom iteration key
Returns:

tuple (itermode, iterkey)

slack.sansio.need_reconnect(event: slack.events.Event) → bool[source]

Check if RTM needs reconnecting

Parameters:event – Incoming slack.events.Event
Returns:boolean
slack.sansio.parse_content_type(headers: MutableMapping[KT, VT]) → Tuple[Optional[str], str][source]

Find content-type and encoding of the response

Parameters:headers – Response headers
Returns:tuple (content-type, encoding)
slack.sansio.prepare_iter_request(url: Union[slack.methods.Methods, str], data: MutableMapping[KT, VT], *, iterkey: Optional[str] = None, itermode: Optional[str] = None, limit: int = 200, itervalue: Union[str, int, None] = None) → Tuple[MutableMapping[KT, VT], str, str][source]

Prepare outgoing iteration request

Parameters:
  • urlslack.methods item or string of url
  • data – Outgoing data
  • limit – Maximum number of results to return per call.
  • iterkey – Key in response data to iterate over (required for url string).
  • itermode – Iteration mode (required for url string) (one of cursor, page or timeline)
  • itervalue – Value for current iteration (cursor hash, page or timestamp depending on the itermode)
Returns:

tuple (data, iterkey, itermode)

slack.sansio.prepare_request(url: Union[str, slack.methods.Methods], data: Optional[MutableMapping[KT, VT]], headers: Optional[MutableMapping[KT, VT]], global_headers: MutableMapping[KT, VT], token: str, as_json: Optional[bool] = None) → Tuple[str, Union[str, MutableMapping[KT, VT]], MutableMapping[KT, VT]][source]

Prepare outgoing request

Create url, headers, add token to the body and if needed json encode it

Parameters:
  • urlslack.methods item or string of url
  • data – Outgoing data
  • headers – Custom headers
  • global_headers – Global headers
  • token – Slack API token
  • as_json – Post JSON to the slack API
Returns:

tuple (url, body, headers)

slack.sansio.raise_for_api_error(headers: MutableMapping[KT, VT], data: MutableMapping[KT, VT]) → None[source]

Check request response for Slack API error

Parameters:
  • headers – Response headers
  • data – Response data
Raises:

slack.exceptions.SlackAPIError

slack.sansio.raise_for_status(status: int, headers: MutableMapping[KT, VT], data: MutableMapping[KT, VT]) → None[source]

Check request response status

Parameters:
  • status – Response status
  • headers – Response headers
  • data – Response data
Raises:
slack.sansio.validate_request_signature(body: str, headers: MutableMapping[KT, VT], signing_secret: str) → None[source]

Validate incoming request signature using the application signing secret.

Contrary to the team_id and verification_token verification this method is not called by slack-sansio when creating object from incoming HTTP request. Because the body of the request needs to be provided as text and not decoded as json beforehand.

Parameters:
  • body – Raw request body
  • headers – Request headers
  • signing_secret – Application signing_secret
Raise:
slack.exceptions.InvalidSlackSignature: when provided and calculated signature do not match slack.exceptions.InvalidTimestamp: when incoming request timestamp is more than 5 minutes old