aslack package¶
Submodules¶
aslack.core module¶
Core API wrapper functionality, adapted from Flash Services.
-
class
aslack.core.
Service
(*_, **kwargs)[source]¶ Bases:
object
Abstract base class for API wrapper services.
-
class
aslack.core.
TokenAuthMixin
(*, api_token, **kwargs)[source]¶ Bases:
object
Mix-in class for implementing token authentication.
Parameters: api_token ( str
) – A valid API token.
-
class
aslack.core.
UrlParamMixin
(*, api_token, **kwargs)[source]¶ Bases:
aslack.core.TokenAuthMixin
Mix-in class for implementing URL parameter authentication.
aslack.slack_api module¶
Access to the base Slack Web API.
-
aslack.slack_api.
ALL
¶ object
– Marker for cases where all child methods should be deleted byapi_subclass_factory()
.
-
class
aslack.slack_api.
SlackApi
(*, api_token, **kwargs)[source]¶ Bases:
aslack.core.UrlParamMixin
,aslack.core.Service
Class to handle interaction with Slack’s API.
-
execute_method
(method, **params)[source]¶ Execute a specified Slack Web API method.
Parameters: Returns: The JSON data from the response.
Return type: Raises: aiohttp.web_exceptions.HTTPException
– If the HTTP request returns a code other than 200 (OK).SlackApiError
– If the Slack API is reached but the response contains an error message.
-
-
exception
aslack.slack_api.
SlackApiError
(err_msg, *args)[source]¶ Bases:
aslack.utils.FriendlyError
Wrapper exception for error messages in the response JSON.
-
EXPECTED_ERRORS
= {'migration_in_progress': 'Team is being migrated between servers.', 'account_inactive': 'Authentication token is for a deleted user or team.', 'invalid_auth': 'Invalid authentication token.', 'not_authed': 'No authentication token provided.'}¶ Friendly messages for expected Slack API errors.
-
-
aslack.slack_api.
api_subclass_factory
(name, docstring, remove_methods, base=<class 'aslack.slack_api.SlackApi'>)[source]¶ Create an API subclass with fewer methods than its base class.
Parameters: - name (
str
) – The name of the new class. - docstring (
str
) – The docstring for the new class. - remove_methods (
dict
) – The methods to remove from the base class’sAPI_METHODS
for the subclass. The key is the name of the root method (e.g.'auth'
for'auth.test'
, the value is either a tuple of child method names (e.g.('test',)
) or, if all children should be removed, the special valueALL
. - base (
type
, optional) – The base class (defaults toSlackApi
).
Returns: The new subclass.
Return type: Raises: KeyError
– If the method wasn’t in the superclass.- name (
aslack.slack_bot module¶
A Slack bot using the real-time messaging API.
-
class
aslack.slack_bot.bot.
SlackBot
(id_, user, api)[source]¶ Bases:
object
Base class Slack bot.
Parameters: -
address_as
¶ str
– The text that appears at the start of messages addressed to this bot (e.g.'<@user>: '
).
-
socket
¶ aiohttp.web.WebSocketResponse
– The web socket to respond on.
-
VERSION
¶ str
– Version string to show to the user (if not overridden, will show the aSlack version).
-
classmethod
from_api_token
(token=None, api_cls=<class 'abc.SlackBotApi'>)[source]¶ Create a new instance from the API token.
Parameters: Returns: The new instance.
Return type:
-
handle_message
(message, filters)[source]¶ Handle an incoming message appropriately.
Parameters: - message (
aiohttp.websocket.Message
) – The incoming message to handle. - filters (
list
) – The filters to apply to incoming messages.
- message (
-
join_rtm
(filters=None)[source]¶ Join the real-time messaging service.
Parameters: filters ( dict
, optional) – Dictionary mapping message filters to the functions they should dispatch to. Use acollections.OrderedDict
if precedence is important; only one filter, the first match, will be applied to each message.
-
Generic message handling functionality.
-
class
aslack.slack_bot.handler.
BotMessageHandler
(bot)[source]¶ Bases:
aslack.slack_bot.handler.MessageHandler
Base class for handlers of messages about the current bot.
Parameters: bot ( SlackBot
) – The current bot.
aslack.utils module¶
Utility functionality.
-
exception
aslack.utils.
FriendlyError
(err_msg, *args)[source]¶ Bases:
Exception
Exception with friendlier error messages.
Notes
The
err_msg
is resolved inEXPECTED_ERRORS
, or passed through as-is if not found there.Parameters: -
EXPECTED_ERRORS
= {}¶ Friendly messages for expected errors.
-
-
aslack.utils.
raise_for_status
(response)[source]¶ Raise an appropriate error for a given response.
Parameters: response ( aiohttp.ClientResponse
) – The API response.Raises: aiohttp.web_exceptions.HTTPException
– The appropriate error for the response’s status.
Examples¶
halliwell¶
Example aSlack bot to give you information on movies.
-
class
examples.halliwell.
ActorOverlapQueryHandler
(bot)[source]¶ Bases:
aslack.slack_bot.handler.BotMessageHandler
Handles queries about movies with the same group of actors.
If you send me a message asking ‘actors in’ with a quoted list of movies I will find movies featuring all of those actors.
-
class
examples.halliwell.
Halliwell
(id_, user, api, tmdb_client=None)[source]¶ Bases:
aslack.slack_bot.bot.SlackBot
The filmgoer’s companion.
Parameters: - id (
str
) – The BOT’s Slack ID. - user (
str
) – The BOT’s friendly name. - api (
SlackApi
) – The Slack API wrapper. - tmdb_client (
atmdb.client.TMDbClient
) – The TMDb client.
- id (
-
class
examples.halliwell.
MovieOverlapQueryHandler
(bot)[source]¶ Bases:
aslack.slack_bot.handler.BotMessageHandler
Handles queries about actors in the same group of movies.
If you send me a message asking ‘movies with’ with a quoted list of actors I will find movies featuring all of those actors.
-
class
examples.halliwell.
MovieQueryHandler
(bot)[source]¶ Bases:
aslack.slack_bot.handler.BotMessageHandler
Handles queries about specific movies.
If you send me a message starting with the word ‘movie’ I will tell you about that movie.
-
class
examples.halliwell.
PersonQueryHandler
(bot)[source]¶ Bases:
aslack.slack_bot.handler.BotMessageHandler
Handles queries about specific people.
If you send me a message starting with the word ‘person’ I will tell you about that person.
-
examples.halliwell.
QUOTED
= re.compile('"([^"]+)"')¶ Regular expression to extract quoted text.