[Python] Generate a password with Slackbot

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".

How to generate a password in 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.

Generate password with Slackbot + Python

Embed the above process in the Slackbot response.

Slackbot response specifications

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

Send password to direct message

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 ** Slackbotでパスワード生成

The password has been sent to the direct message.

Slackbotでパスワード生成2

At the end

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

[Python] Generate a password with Slackbot
Generate XML (RSS) with Python
Make a fortune with Python
Password management with python: keyring
Create a directory with python
Make a simple Slackbot with interactive button in python
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
Generate a normal distribution with SciPy
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Password generation in texto with python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables
I made a daemon with Python
Generate a Python library download badge
Write a batch script with Python3.5 ~
[Pyenv] Building a python environment with ubuntu 16.04
Generate a first class collection in Python
Generate all files with a specific extension
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
[Python] A program that creates stairs with #
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
Add a Python data source with Redash
Create a dummy image with Python + PIL.
Generate Japanese test data with Python faker
I made a character counter with Python
[Python] Drawing a swirl pattern with turtle
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Search the maze with the python A * algorithm
Daemonize a Python web app with Supervisor
Check stock prices with slackbot using python
Let's make a voice slowly with Python
Created a darts trip with python (news)
Simplify PDF password unlock with python + bat
I tried a functional language with Python
[Python] A quick web application with Bottle!
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Write a TCP client with Python Twisted
What I did with a Python array
Solve AtCoder ABC168 with python (A ~ D)
Try HTML scraping with a Python library
I made a Hex map with Python
Generate a class from a string in Python