Some functions for dealing with mentions in slack messages
Project description
slackmentions
Provides functions for dealing with mentions in slack messages.
Installation
$ pip install slackmentions
You will also need the slackclient package for this to be useful.
Usage
Available functions:
- findpeople:
Finds username mentions in slack text and creates SlackPerson objects for those people. Returns a list of those SlackPerson objects. This can be used to get more information about users or if the same text will be processed multiple times.
Arguments:
text: text to find @ mentions in.
userlist: output of slack api users.list
silent: slackperson.SlackDataError will be raised if a mention is found in the text but not in the userlist. Setting to True will swallow this error and ignore that mention.
Returns: List of SlackPerson objects or empty list.
- mention_text:
Replaces username mentions in text with user id mentions for tagging by slack api message sending.
Arguments:
text: The text containing @ mentions
people: A list of SlackPerson objects for people found in the text. If not specified, one will be generated by passing text and userlist to findpeople. Required if userlist is not provided.
userlist: The json from slack api users.list. Required if people is not provided.
silent: slackperson.SlackDataError will be raised if a mention is found in the text but not in the userlist. Setting to True will swallow this error and ignore that mention. Only applies if userlist is specified, not people.
- clean_text:
Deletes @ mentions from text Arguments:
text: The text containing @ mentions
people: A list of SlackPerson objects for people found in the text. If not specified, one will be generated by passing text and userlist to findpeople. Required if userlist is not provided.
userlist: The json from slack api users.list. Required if people is not provided.
silent: slackperson.SlackDataError will be raised if a mention is found in the text but not in the userlist. Setting to True will swallow this error and ignore that mention. Only applies if userlist is specified, not people.
clean_all: set to true to ignore the user lists and people and just nuke all the mentions
import slackmentions
from slackclient import SlackClient
sc = SlackClient(os.environ['SLACK_API_TOKEN'])
userlist = sc.api_call('users.list')
text = 'hi @joe, could you tell @kathy to call me?'
people = slackmentions.findpeople(text, userlist)
# people = [SlackPerson(userid='U0000001', username='joe'),
SlackPerson(userid='U0000002', username='kathy')]
withmentions = slackmentions.mention_text(text, people=people)
# withmentions = 'hi <@U0000001>, could you please tell <@U0000002> to call
me?'
withmentions2 = slackmentions.mention_text(text, userlist=userlist)
assert withmentions = withmentions2
cleantext = slackmentions.clean_text(text, people=people)
# cleantext = 'hi, could you please tell to call me?'
Tests
There are tests for each method with successes, errors raised, errors swallowed, and missing keyword arguments. They can be run with pytest.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for slackmentions-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 554dfd90bfc29ab167f6792273d28f1ef9b0fb8be476c28f45dca6bd3da8851e |
|
MD5 | 83b2d3b5cb4da2a3191c2cac07ab556e |
|
BLAKE2b-256 | 358af56b2415c86679f7cfcdcbe1360590ca9df434ee918ee7ce781b26a0459d |