Slackbot¶
-
class
slackbot.
SlackBot
(token)¶ Bases:
object
Slack web client to build a python slack bot.
Slackbot
is a simple wrapper around basic API calls to slack API. Instantiation needs an environment variable storing the connexion token. When initiating aSlackbot
instance, the bot id is read from slack API and stored as attribute.Parameters: token (str) – name for environment variable storing slack application token
Variables: - client – the slack web client instance with which API calls are made
- mention_regex – a regex to identify a direct mention in a text
- id – the bot’s id
-
static
closing_time
(closing_hour: str, rtm_client=None) → None¶ Close an RTM client if the current hour is later than the given closing hour.
Parameters: - closing_hour (str) – hour after which RTM client must be stopped.
Must be in
HH:mm
string format - rtm_client (
slack.RTMClient()
) – The RTM client to stop (see above to see usage)
- closing_hour (str) – hour after which RTM client must be stopped.
Must be in
-
mention_regex
= '<@(|[WU].+?)>(.*)'¶
-
static
parse_mention
(message_text, regex) → tuple¶ From a message, find and extract a mention and the related text.
Result is a length-two tuple of the form
(user_id, text)
. If no mention is found in the message, the result is(None, text)
.Parameters: - message_text (str) – the body of a message
- regex (regex) – the regex identifying a mention. Using
SlackBot.mention_regex
will usually suffice
-
send_message
(channel, message) → None¶ Immediate wrap around
slack.WebClient.chat_postMessage()
-
users_list
() → list¶ Get the list of non-deleted users.
A wrapper around
slack.WebClient.users_list
. It returns a flat list of dictionaries, with keysname
,id
andemail
.id
is used to tag people in messages with the syntax<@%s> % id
.