Skip to content

Instantly share code, notes, and snippets.

@hery
Last active February 20, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hery/127ff82e0c760333603b01f9d61f28cb to your computer and use it in GitHub Desktop.
Save hery/127ff82e0c760333603b01f9d61f28cb to your computer and use it in GitHub Desktop.
Valid Slack Channel Name
def valid_channel_name(name):
"""Channel names can only contain lowercase letters,
numbers, hyphens, and underscores, and must be 21 characters or less.
Some sample conversions from Slack to determine some heuristics:
1..................20abc def => 1_20abc-def
1..................20abc..def => 1_20abc_def
Heuristics:
1. Encode input into ASCII
2. Replace non-alphanumeric groups of charaters by a single _
3. Replace all spaces by dashes
4. Truncate the resulting sring into first 21 characters
"""
result = unicodedata.normalize('NFD', name).encode('ascii', 'ignore')
result = re.sub("[^a-zA-Z\d\s-]+", '_', result)
result = result.replace(' ', '-')
result = result[:21].lower()
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment