Hello, I Ya.
This time, I will explain how to implement password with Slackbot + Python. Specifically, the following two are explained.
-How to generate a password in Python ・ Generate password with Slackbot + Python
For the basics of Slackbot made with Python, please read "Create Slackbot with Python".
You can create a password generation process relatively easily by using Python's random library.
Function implementation example
Argument 1: Number of password digits (default 8)
Argument 2: String type(Default: uppercase / lowercase / numbers)
Return value: Password
Returns the character string for the number of digits specified in argument 1. In addition, argument 2 allows you to specify detailed character strings such as "lowercase and number", "uppercase and lowercase", and "uppercase only".
import random
def make_password(digit=8, word_type=None):
words = ''
password = ''
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
number = '0123456789'
#Specify all if there is no argument
if word_type is None:
word_type = ['uppercase', 'lowercase', 'number']
#Judgment of characters to use
if 'uppercase' in word_type:
words = words + uppercase
if 'lowercase' in word_type:
words = words + lowercase
if 'number' in word_type:
words = words + number
#Password generation loop
for i in range(0, digit):
#Since it is returned as an array, get the 0th value
password = password + random.sample(words, 1)[0]
return password
For more information on how to implement passwords, see Implementing password generation in Python.
Embed the above process in the Slackbot response.
Argument 1: Number of password digits Argument 2: Password character type
Specify the character type in the following form.
Numbers:Numbersのみ
Large alphanumeric characters: uppercase letters and numbers
Alphanumeric characters: lowercase letters and numbers
Alphabet:小文字と大文字のAlphabet
Large letters: Uppercase letters only
Small alphabet:Only lowercase letters
Other: All alphanumeric characters
If you respond to the channel with message.send (), other people will see your password, which is not very good for security. Therefore, it responds to direct messages.
Responses to direct messages can be achieved using the slacker library.
When using direct message, you need to specify the user ID instead of the channel name. The user ID is stored in message.
#Get user ID
user_id = message.user['id']
#Send password by direct message
slack = Slacker(slackbot_settings.API_TOKEN)
slack.chat.post_message(user_id, password, as_user=True)
The implementation example is as follows.
import random
from slackbot.bot import respond_to
from slacker import Slacker
import slackbot_settings
def make_password(digit=8, word_type=None):
words = ''
password = ''
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
number = '0123456789'
#Specify all if there is no argument
if word_type is None:
word_type = ['uppercase', 'lowercase', 'number']
#Judgment of characters to use
if 'uppercase' in word_type:
words = words + uppercase
if 'lowercase' in word_type:
words = words + lowercase
if 'number' in word_type:
words = words + number
#Password generation loop
for i in range(0, digit):
#Since it is returned as an array, get the 0th value
password = password + random.sample(words, 1)[0]
return password
@respond_to('^Password generation s(.*)s(.*)$')
def response_pass(message, digit, param_type):
#Get user ID
user_id = message.user['id']
#Separation of patterns
if param_type == 'Numbers':
word_type = ['number']
elif param_type == 'Large alphanumeric characters':
word_type = ['lowercase', 'number']
elif param_type == 'Small alphanumeric characters':
word_type = ['lowercase', 'number']
elif param_type == 'Alphabet':
word_type = ['lowercase', 'uppercase']
elif param_type == 'English letters':
word_type = ['uppercase']
elif param_type == 'Small alphabet':
word_type = ['lowercase']
else:
word_type = ['uppercase', 'lowercase', 'number']
#Password generation
password = make_password(int(digit), word_type)
#Send password by direct message
slack = Slacker(slackbot_settings.API_TOKEN)
slack.chat.post_message(user_id, password, as_user=True)
message.send('I sent my password to a direct message!')
** ◆ Execution result **
The password has been sent to the direct message.
As introduced here, Slackbot can be used as a password generation tool relatively easily. It is convenient if you want to create a small password.
There are password generation sites, but the advantage that human-made generation sites do not have is that they can be freely customized. This time, only one pattern of alphanumeric characters was displayed, but please customize it in various ways, such as displaying multiple patterns and making it possible to use symbols.
How to use Slackbot is also summarized in the blog. How to make Slackbot-Python version-
Recommended Posts