glados package¶
-
class
glados.
Glados
(config_file=None, plugins_folder=None, bots_config_dir=None, plugins_config_dir=None)[source]¶ Bases:
object
Glados is the core of the GLaDOS package.
- Parameters
config_file (
Optional
[str
]) – path to config fileplugins_folder (
Optional
[str
]) – path to plugins folderbots_config_dir (
Optional
[str
]) – path to bots config folderplugins_config_dir (
Optional
[str
]) – path to plugin config folder.
Notes
If
config_file
is passed in and the file hasplugins_folder
,bots_config_dir
,plugins_config_dir
in it , then the other parameters are not required-
add_bot
(bot)[source]¶ Add a new bot to GLaDOS.
- Parameters
bot (
GladosBot
) – the bot to be added to GLaDOS- Return type
NoReturn
-
add_plugin
(plugin)[source]¶ Add a plugin to GLaDOS
- Parameters
plugin (
GladosPlugin
) – the plugin to be added to GLaDOS- Return type
NoReturn
-
import_plugins
(bot_name=None)[source]¶ Import all discovered plugins and add them to the plugin list.
- Parameters
bot_name (
Optional
[str
]) – If set GLaDOS will only import the bot name that is provided here.- Return type
NoReturn
-
read_config
(bot_name=None)[source]¶ Read the GLaDOS config file. If a bot name is provided it will only install that bot. Else it will install all bots.
- Parameters
bot_name (
Optional
[str
]) – If provided, install only the bot with this name.- Return type
NoReturn
-
request
(request)[source]¶ Send a request to GLaDOS. This returns whatever the plugin returns.
This function will also set the datastore session for the request, try to find the interaction in the datastore and fetch it. This info is available in the request.
- Parameters
request (
GladosRequest
) – the request to be sent to GLaDOS
-
class
glados.
GladosBot
(token, name, signing_secret=None, **kwargs)[source]¶ Bases:
object
GLaDOS Bot represents all the required data and functions for a Slack bot.
Notes
All Slack Web API functions can be called from MyBot.client.*
- Parameters
name (
str
) – The name of the bot (URL Safe)token (
Union
[str
,Dict
[str
,str
]]) – The bot tokensigning_secret (
Union
[str
,Dict
[str
,str
],None
]) – The bot signing secret.
-
name
¶ The name of the bot (URL Safe)
- Type
str
-
token
¶ The bot token
- Type
str
-
client
¶ A Slack client generated for that bot
- Type
WebClient
-
signing_secret
¶ The bots signing secret.
- Type
str
-
delete_message
(channel, ts)[source]¶ Deletes a message that was sent by a bot
- Parameters
channel (
str
) –ts (
str
) –
- Return type
SlackResponse
-
send_message
(channel, message)[source]¶ Send a message as the bot
- Parameters
channel (
str
) – channel to send the message tomessage (
Message
) – message object to send
- Return type
SlackResponse
-
update_message
(channel, ts, message)[source]¶ Updates a message that was sent by the bot
- Parameters
channel (
str
) –ts (
str
) –message (
Message
) –
- Return type
SlackResponse
-
validate_slack_signature
(request)[source]¶ - Parameters
request (
GladosRequest
) –
-
class
glados.
GladosRequest
(route_type, route=None, slack_verify=None, bot_name=None, json=None, data=None, **kwargs)[source]¶ Bases:
object
GLaDOS Request Object. This holds all the data required to process the request.
- Parameters
route_type (
RouteType
) – what type of route is thisroute (
Optional
[str
]) – what is the route to be calledslack_verify (
Optional
[SlackVerification
]) – slack data used for verifying the request came from Slackbot_name (
Optional
[str
]) – The name of the bot to send the request to. This is used for select RouteTypesjson (
Union
[str
,dict
,None
]) – the json paylod of the requestdata (
Optional
[dict
]) – data to send with the request. This should be from a databasekwargs –
Examples
>>> request = GladosRequest(RouteType.SendMessage, "send_mock", json={"message":"my message"}) >>> print(request.json.message) my message >>> try: ... print(request.json.other_param) ... except AttributeError: ... print("ERROR") ERROR
-
add_interaction_to_datastore
(interaction)[source]¶ Add an interaction to the datastore and return the updated interaction.
Notes
The interaction_id can be retrieved by doing interaction.interaction_id
- Parameters
interaction (
DataStoreInteraction
) – the interaction to be added- Return type
Optional
[DataStoreInteraction
]
-
property
data_blob
¶ Returns the raw dict of the data object
- Return type
dict
-
gen_new_interaction
(*, followup_action=None, followup_ts=None, ttl=None, data=None, auto_link=True, auto_set=True)[source]¶ Generate a new interaction object and set it as new_interaction.
- Parameters
followup_action –
followup_ts –
ttl –
data –
auto_link (
bool
) – set this request to auto-link using the return payload. The return payload must be the response from sending a slack message.auto_set (
bool
) – set this new interaction object as the request new_interaction
- Return type
-
property
interaction
¶ Returns the interaction for the request
- Return type
Optional
[DataStoreInteraction
]
-
property
interaction_id
¶ Returns the interaction_id of request.interaction
- Return type
Optional
[str
]
-
link_interaction_to_message
(interaction_id, channel, message_ts)[source]¶ Link interaction to message
- Parameters
interaction_id (
str
) – interaction ID to linkchannel (
str
) – channel to be linked tomessage_ts (
datetime
) – ts to be linked to
- Return type
NoReturn
-
link_interaction_to_message_response
(interaction_id, message_response)[source]¶ Link interaction to message response
- Parameters
interaction_id (
str
) – interaction ID to be linkedmessage_response (
dict
) – JSON payload response from sending message on slack.
- Return type
NoReturn
-
property
route
¶ the actual route
If the route automatically prefixed the route with the bot name, it will return the route with the prefix
- Return type
str
-
set_datastore
(datastore)[source]¶ Set the Datastore and session for the request.
- Parameters
datastore (
DataStore
) – Datastore to use. This datastore will be used to create the session.- Return type
NoReturn
-
class
glados.
RouteType
[source]¶ Bases:
enum.Enum
An enumeration.
-
Callback
= 3¶
-
Events
= 5¶
-
Interaction
= 6¶
-
Menu
= 7¶
-
Response
= 2¶
-
Slash
= 4¶
-
Webhook
= 1¶
-
-
class
glados.
EventRoutes
[source]¶ Bases:
enum.Enum
An enumeration.
-
app_home_opened
= 1¶
-
message
= 2¶
-
-
class
glados.
GladosPlugin
(config, bot, **kwargs)[source]¶ Bases:
object
Parent class for a GLaDOS Plugin
- Parameters
config (
PluginConfig
) – PluginConfig object for the plugin.bot (
GladosBot
) – the GLaDOS bot that this plugin will use
-
add_route
(route_type, route, function)[source]¶ Add a new route to the plugin
- Parameters
route_type (
RouteType
) – what type of route this is thisroute (
Union
[EventRoutes
,str
]) – what is the route to be addedfunction (
Callable
) – the function to be executed when this route runs
- Return type
NoReturn
-
has_route
(route)[source]¶ See if route exists.
- Parameters
route (route to check) –
- Returns
- Return type
True if route exists else false
-
respond_to_url
(request, text, **kwargs)[source]¶ When you click on a link that was sent via slack it sends a callback, This is to handle that
- Parameters
request (
GladosRequest
) –text (
str
) –
-
property
routes
¶ List all routes for the plugin.
- Return type
List
[GladosRoute
]
-
send_request
(request, **kwargs)[source]¶ This is the function to be called when sending a request to a plugin.
This function is responsible for validating the slack signature if needed. It also returns and empty string if the function called returns None.
- Parameters
request (
GladosRequest
) – the request object to be sentkwargs –
- Return type
Any
-
class
glados.
GladosConfig
(config_file)[source]¶ Bases:
object
- Parameters
config_file (
str
) –
-
property
sections
¶ what sections are there in the config file
- Returns
- Return type
sorted list of sections in the yaml file
-
glados.
set_logging
(level=None, format=None)[source]¶ Set the logging format
- Parameters
level (
Optional
[str
]) – Level to set logging toformat (
Optional
[str
]) – Logging format to set
-
glados.
check_for_env_vars
(value)[source]¶ Check an input value to see if it is an env_var or enc_env_var and get the value.
- Parameters
value (
Union
[str
,dict
]) – input to check.- Returns
Returns the value of the var from either the passed in value, or the env var value.
- Return type
Any
- Raises
KeyError if the env var is not set for what youre tying to get. –
Submodules¶
glados.bot module¶
-
class
glados.bot.
GladosBot
(token, name, signing_secret=None, **kwargs)[source]¶ Bases:
object
GLaDOS Bot represents all the required data and functions for a Slack bot.
Notes
All Slack Web API functions can be called from MyBot.client.*
- Parameters
name (
str
) – The name of the bot (URL Safe)token (
Union
[str
,Dict
[str
,str
]]) – The bot tokensigning_secret (
Union
[str
,Dict
[str
,str
],None
]) – The bot signing secret.
-
name
¶ The name of the bot (URL Safe)
- Type
str
-
token
¶ The bot token
- Type
str
-
client
¶ A Slack client generated for that bot
- Type
WebClient
-
signing_secret
¶ The bots signing secret.
- Type
str
-
delete_message
(channel, ts)[source]¶ Deletes a message that was sent by a bot
- Parameters
channel (
str
) –ts (
str
) –
- Return type
SlackResponse
-
send_message
(channel, message)[source]¶ Send a message as the bot
- Parameters
channel (
str
) – channel to send the message tomessage (
Message
) – message object to send
- Return type
SlackResponse
-
update_message
(channel, ts, message)[source]¶ Updates a message that was sent by the bot
- Parameters
channel (
str
) –ts (
str
) –message (
Message
) –
- Return type
SlackResponse
-
validate_slack_signature
(request)[source]¶ - Parameters
request (
GladosRequest
) –
glados.configs module¶
glados.core module¶
-
class
glados.core.
Glados
(config_file=None, plugins_folder=None, bots_config_dir=None, plugins_config_dir=None)[source]¶ Bases:
object
Glados is the core of the GLaDOS package.
- Parameters
config_file (
Optional
[str
]) – path to config fileplugins_folder (
Optional
[str
]) – path to plugins folderbots_config_dir (
Optional
[str
]) – path to bots config folderplugins_config_dir (
Optional
[str
]) – path to plugin config folder.
Notes
If
config_file
is passed in and the file hasplugins_folder
,bots_config_dir
,plugins_config_dir
in it , then the other parameters are not required-
add_bot
(bot)[source]¶ Add a new bot to GLaDOS.
- Parameters
bot (
GladosBot
) – the bot to be added to GLaDOS- Return type
NoReturn
-
add_plugin
(plugin)[source]¶ Add a plugin to GLaDOS
- Parameters
plugin (
GladosPlugin
) – the plugin to be added to GLaDOS- Return type
NoReturn
-
import_plugins
(bot_name=None)[source]¶ Import all discovered plugins and add them to the plugin list.
- Parameters
bot_name (
Optional
[str
]) – If set GLaDOS will only import the bot name that is provided here.- Return type
NoReturn
-
read_config
(bot_name=None)[source]¶ Read the GLaDOS config file. If a bot name is provided it will only install that bot. Else it will install all bots.
- Parameters
bot_name (
Optional
[str
]) – If provided, install only the bot with this name.- Return type
NoReturn
-
request
(request)[source]¶ Send a request to GLaDOS. This returns whatever the plugin returns.
This function will also set the datastore session for the request, try to find the interaction in the datastore and fetch it. This info is available in the request.
- Parameters
request (
GladosRequest
) – the request to be sent to GLaDOS
glados.datastore module¶
-
class
glados.datastore.
DataStore
(host, username, password, port=5432, database='glados')[source]¶ Bases:
object
DataStore is how GLaDOS stores async data.
- Parameters
host (
str
) – postgres host.username (
str
) – postgres username.password (
str
) – postgres password.port (
int
) – postgres port.database (
str
) – postgres database to use.
-
create_table
(tables=None, force=False)[source]¶ Create the table.
If you set force to True then it will drop the existing tables and then recreate them. ALL DATA WILL BE LOST
- Parameters
tables (
Optional
[List
[str
]]) – only take action on these tables. If None, then take action on all tablesforce (
bool
) – drop existing tables and rebuild. (default: False)
- Return type
NoReturn
-
drop_table
(table='interactions', force=False)[source]¶ Drop the GLaDOS table so that it can be re-created.
- Parameters
table (
str
) – table name to use.force (
bool
) – if True will fill force drop the table without checks.
- Return type
NoReturn
-
find_by_id
(interaction_id, session)[source]¶ Find an interaction by interaction_id.
- Parameters
interaction_id (
str
) – interaction ID to findsession (
Session
) – session to be used
- Return type
-
find_interaction_by_channel_ts
(channel, ts, session)[source]¶ Find the interaction in the datastore by channel and message ts.
- Parameters
channel (
str
) – channel of the interaction youre looking forts (
datetime
) – ts of the interaction you are looking forsession (
Session
) – session to be used
- Raises
ReferenceError – There were more than one interaction that matched the channel and message_ts
- Return type
Optional
[DataStoreInteraction
]
-
insert_interaction
(interaction, session)[source]¶ Insert an interaction object into the database.
- Parameters
interaction (
DataStoreInteraction
) – The row to be insertedsession (
Session
) – session to be used
- Return type
NoReturn
-
link_to_message
(interaction_id, channel, ts, session)[source]¶ Link to message by setting message ts and channel.
- Parameters
interaction_id (
str
) – interaction ID to linkchannel (
str
) – channel to link interaction tots (
datetime
) – ts to link interaction tosession (
Session
) – session to be used
- Return type
NoReturn
-
link_to_message_response
(interaction_id, message_response, session)[source]¶ Add info from the Slack message into the database for the interaction.
- Parameters
interaction_id (
str
) – The interaction ID that was returned on adding the message to the database.message_response (
dict
) – The raw message response from slack. The channel and ts will be pulled from this.session (
Session
) – session to be used
- Return type
NoReturn
-
table_exists
(table='interactions')[source]¶ Check to see if the GLaDOS table is found in postgres.
- Parameters
table (
str
) – table name to use.- Return type
bool
-
class
glados.datastore.
DataStoreInteraction
(**kwargs)[source]¶ Bases:
sqlalchemy.ext.declarative.api.Base
DataStoreInteraction represents a row in the datastore. This is used to update data in the datastore.
-
interaction_id
¶ This is the primary key of the datastore. This is the ID of the entry in the datastore.
- Type
str
-
ts
¶ This is the time the row was put into the database.
- Type
datetime
-
bot
¶ This is the name of the bot it should use when completing followup actions.
- Type
str
-
data
¶ Any extra data stored with the interaction. This is a JSON blob.
- Type
dict
-
message_channel
¶ The channel that this interaction was sent to.
- Type
str
-
message_ts
¶ The message timestamp when this interaction was sent.
- Type
datetime
-
ttl
¶ How long this interaction should live for.
- Type
int
-
followup_ts
¶ When should the follow up action happen.
- Type
datetime
-
followup_action
¶ The action name to execute when following up. If None then no action will happen.
- Type
str
-
cron_followup_action
¶ The action name to execute on a normal cron schedule like every 5 min. If None then no action will happen.
- Type
str
-
followed_up
¶ This is the time when the action was followed up at. If it has not happened yet this value will be None.
- Type
datetime
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and values in
kwargs
.Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.
-
bot
¶
-
cron_followup_action
¶
-
data
¶
-
followed_up
¶
-
followup_action
¶
-
followup_ts
¶
-
interaction_id
¶
-
message_channel
¶
-
message_ts
¶
-
ts
¶
-
ttl
¶
-
glados.errors module¶
-
exception
glados.errors.
GladosBotNotFoundError
[source]¶ Bases:
glados.errors.GladosError
Error raised when GladosBot is not found
-
exception
glados.errors.
GladosPathExistsError
[source]¶ Bases:
glados.errors.GladosError
Error raised when trying to add a path that already exists
-
exception
glados.errors.
GladosRouteNotFoundError
[source]¶ Bases:
glados.errors.GladosError
Error raised when the requested path is not found
glados.message_blocks module¶
glados.plugin module¶
-
class
glados.plugin.
GladosPlugin
(config, bot, **kwargs)[source]¶ Bases:
object
Parent class for a GLaDOS Plugin
- Parameters
config (
PluginConfig
) – PluginConfig object for the plugin.bot (
GladosBot
) – the GLaDOS bot that this plugin will use
-
add_route
(route_type, route, function)[source]¶ Add a new route to the plugin
- Parameters
route_type (
RouteType
) – what type of route this is thisroute (
Union
[EventRoutes
,str
]) – what is the route to be addedfunction (
Callable
) – the function to be executed when this route runs
- Return type
NoReturn
-
has_route
(route)[source]¶ See if route exists.
- Parameters
route (route to check) –
- Returns
- Return type
True if route exists else false
-
respond_to_url
(request, text, **kwargs)[source]¶ When you click on a link that was sent via slack it sends a callback, This is to handle that
- Parameters
request (
GladosRequest
) –text (
str
) –
-
property
routes
¶ List all routes for the plugin.
- Return type
List
[GladosRoute
]
-
send_request
(request, **kwargs)[source]¶ This is the function to be called when sending a request to a plugin.
This function is responsible for validating the slack signature if needed. It also returns and empty string if the function called returns None.
- Parameters
request (
GladosRequest
) – the request object to be sentkwargs –
- Return type
Any
-
class
glados.plugin.
PluginConfig
(name, config_file, module=None, enabled=False, bot=None, **kwargs)[source]¶ Bases:
object
Plugin Config Object.
- Parameters
name (
str
) – Plugin Nameconfig_file (
str
) – Path to config file for pluginmodule – plugin python module name
enabled – enable this plugin
bot – what bot does this plugin use
kwargs –
-
to_dict
(user_config_only=True)[source]¶ Return config as dict
- Parameters
user_config_only – if True only get get waht is in the config file and not the running config.
- Return type
dict
-
update
(config, use_base_module=True)[source]¶ Update a config object using the default values from the config object passed in.
- Parameters
config (PluginConfig) – the config object to use as the base. By default the module property will be set from the base config object only
use_base_module (bool) – if set true use the value of module and package from the base config object only.
- Return type
NoReturn
-
class
glados.plugin.
PluginImporter
(plugins_folder, plugins_config_folder)[source]¶ Bases:
object
Create the PluginImporter object.
- Parameters
plugins_folder (
str
) – plugin folderplugins_config_folder (
str
) – plugin config folder
-
discover_plugins
()[source]¶ Discover all plugin config files in the plugins folder
- Return type
NoReturn
glados.request module¶
-
class
glados.request.
GladosRequest
(route_type, route=None, slack_verify=None, bot_name=None, json=None, data=None, **kwargs)[source]¶ Bases:
object
GLaDOS Request Object. This holds all the data required to process the request.
- Parameters
route_type (
RouteType
) – what type of route is thisroute (
Optional
[str
]) – what is the route to be calledslack_verify (
Optional
[SlackVerification
]) – slack data used for verifying the request came from Slackbot_name (
Optional
[str
]) – The name of the bot to send the request to. This is used for select RouteTypesjson (
Union
[str
,dict
,None
]) – the json paylod of the requestdata (
Optional
[dict
]) – data to send with the request. This should be from a databasekwargs –
Examples
>>> request = GladosRequest(RouteType.SendMessage, "send_mock", json={"message":"my message"}) >>> print(request.json.message) my message >>> try: ... print(request.json.other_param) ... except AttributeError: ... print("ERROR") ERROR
-
add_interaction_to_datastore
(interaction)[source]¶ Add an interaction to the datastore and return the updated interaction.
Notes
The interaction_id can be retrieved by doing interaction.interaction_id
- Parameters
interaction (
DataStoreInteraction
) – the interaction to be added- Return type
Optional
[DataStoreInteraction
]
-
property
data_blob
¶ Returns the raw dict of the data object
- Return type
dict
-
gen_new_interaction
(*, followup_action=None, followup_ts=None, ttl=None, data=None, auto_link=True, auto_set=True)[source]¶ Generate a new interaction object and set it as new_interaction.
- Parameters
followup_action –
followup_ts –
ttl –
data –
auto_link (
bool
) – set this request to auto-link using the return payload. The return payload must be the response from sending a slack message.auto_set (
bool
) – set this new interaction object as the request new_interaction
- Return type
-
property
interaction
¶ Returns the interaction for the request
- Return type
Optional
[DataStoreInteraction
]
-
property
interaction_id
¶ Returns the interaction_id of request.interaction
- Return type
Optional
[str
]
-
link_interaction_to_message
(interaction_id, channel, message_ts)[source]¶ Link interaction to message
- Parameters
interaction_id (
str
) – interaction ID to linkchannel (
str
) – channel to be linked tomessage_ts (
datetime
) – ts to be linked to
- Return type
NoReturn
-
link_interaction_to_message_response
(interaction_id, message_response)[source]¶ Link interaction to message response
- Parameters
interaction_id (
str
) – interaction ID to be linkedmessage_response (
dict
) – JSON payload response from sending message on slack.
- Return type
NoReturn
-
property
route
¶ the actual route
If the route automatically prefixed the route with the bot name, it will return the route with the prefix
- Return type
str
-
set_datastore
(datastore)[source]¶ Set the Datastore and session for the request.
- Parameters
datastore (
DataStore
) – Datastore to use. This datastore will be used to create the session.- Return type
NoReturn
-
class
glados.request.
SlackVerification
(data, timestamp=None, signature=None)[source]¶ Bases:
object
An object to hold slack verification data
- Parameters
data (
str
) – raw request body. This is used to verify the message is from slack.timestamp (
Optional
[str
]) – The X-Slack-Request-Timestamp from the headers of the request. This is used to verify the message is from slack.signature (
Optional
[str
]) – The X-Slack-Signature from the headers of the request. This is used to verify the message is from slack.
-
property
json
¶ Returns the dict of the SlackVerification
- Return type
dict
glados.route_type module¶
glados.router module¶
-
class
glados.router.
GladosRoute
(route_type, route, function)[source]¶ Bases:
object
Represents a single route
- Parameters
route_type (
RouteType
) –route (
str
) –function (
Callable
) –
-
class
glados.router.
GladosRouter
(**kwargs)[source]¶ Bases:
object
-
add_route
(plugin, route)[source]¶ Add a route to the router
- Parameters
plugin – the plugin the route belongs to
route (
GladosRoute
) – the route to be added
- Raises
KeyError – a route with the same type and same name already exists
- Return type
NoReturn
-
add_routes
(plugin)[source]¶ Add multiple routes to the router.
- Parameters
plugin – the plugin to add routes from
- Return type
NoReturn
-
exec_route
(request)[source]¶ Execute a route function directly
- Parameters
request (
GladosRequest
) – the GLaDOS request- Returns
- Return type
the data returned by the plugin
Examples
>>> def mock_function(request: GladosRequest): ... print(f"Mock Function: {request.params.message}") ... return True >>> router = GladosRouter() >>> route = GladosRoute(RouteType.SendMessage, "send_mock", mock_function) >>> router.add_route(route) >>> request = GladosRequest(RouteType.SendMessage, "send_mock", message="Hello World!") >>> successful = router.exec_route(request) Mock Function: Hello World! >>> print(successful) True
>>> def mock_function(request: GladosRequest): ... print(f"Mock Function: {request.params.message}") ... return True >>> router = GladosRouter() >>> route = GladosRoute(RouteType.SendMessage, "send_mock", mock_function) >>> router.add_route(route) >>> request = GladosRequest(RouteType.SendMessage, "send_mock_fail", message="Hello World!") >>> successful = router.exec_route(request) >>> print(successful) False
-
get_route
(route_type, route)[source]¶ Get a GladosRoute object for the requested route.
- Parameters
route_type (
RouteType
) – the type of route to getroute (
str
) – the route to get
- Raises
GladosRouteNotFoundError – the requested route is not found
- Return type
Callable
-
glados.utils module¶
-
glados.utils.
check_for_env_vars
(value)[source]¶ Check an input value to see if it is an env_var or enc_env_var and get the value.
- Parameters
value (
Union
[str
,dict
]) – input to check.- Returns
Returns the value of the var from either the passed in value, or the env var value.
- Return type
Any
- Raises
KeyError if the env var is not set for what youre tying to get. –
-
glados.utils.
decode_kms
(ciphertext_blob)[source]¶ Decode a secret using the IAM role of the lambda function.
- Parameters
ciphertext_blob (
str
) – ciphertext_blob to decode- Returns
Decoded KMS data
- Return type
obj: str