Chat bot (bot) is a kind of practical interactive Chat Service mode like Slack. If you've never built a chat bot before, then this article provides a simple getting Started guide to how to build your first chat bot with Python in conjunction with the Slack API.
We build your development environment, get a chat bot token for the Slack API, and develop a simple chat bot with Pyhon.
The tools that we need
Our chat bot we call it "Starterbot", which requires Python and Slack APIs. To run our Python code, we need:
-
< Span style= "FONT-SIZE:14PX;" >python 2 or Python 3
-
pip and virtualenv to handle Python application dependencies
-
-
The official Python Slack client code base created by the Slack team
-
slack API Test token
The Slack API documentation is useful when you build it in this tutorial.
All the code in this tutorial is placed in the Slack-starterbot Public Library and open source with the MIT license.
To build our environment
We now know what tools are needed for our projects, so let's build our development environment. First go to the terminal (or the command prompt on Windows) and switch to the directory where you want to store the item. In that directory, create a new virtualenv to separate our application dependencies from other Python projects.
Activate virtualenv:
Your prompt should now look like this:
The virtualenv command prompt for Starterbot has been activated by this official slack client API Help Library is established by slack, which can send and receive messages through the slack channel. Install the Slackclient library with this pip command:
When the PIP command is complete, you should see output similar to this and return a prompt.
Install slackclient output with PIP on an activated virtualenv we also need to get an access token for our slack project so that our chat bot can use it to connect to the slack API.
Slack Real-time message delivery (RTM) API
Slack allows programs to access their message delivery channels through a Web API. Go to this Slack Web API page to register to build your own Slack project. You can also log in to an existing account that you have administrative privileges on.
Use the login button in the top right corner of the Web API page to log in and you will reach the Chat bots user page.
Custom Chat Bot user page give your chat bot a name of "Starterbot" and then click on the "ADD bot integration" button.
Add a bot integration and named "Starterbot" this page will reload and you will see a newly generated access token. You can also change the logo to your own design. For example, I gave this "full Stack Python" flag.
Copy and paste the access token for your new Slack chat robot click on the "Save Integration" button at the bottom of the page. Your chat bot is now ready to connect to the Slack API.
A common practice for Python developers is to output secret tokens with environment variables. The output slack token name is Slack_bot_token:
Well, we now have the authorization to use this Slack API as a chat bot.
We also need more information to build a chat robot: The ID of our chat robot. Next we will write a short script to get the ID from the Slack API.
Get the ID of our chat robot
This is the last time to write some Python code! We write a short Python script to get Starterbot's ID to warm up a bit. This ID differs based on the Slack project.
We need this ID, which is used to Yue Heyue our application when parsing a message sent to Starterbot from Slack RTM. Our script will also test our Slack_bot_token environment variables for correct settings.
Create a new file named printbotid.py, and fill in the following code:
Our code imports Slackclient and instantiates it with the environment variable Slack_bot_token we set. When the script executes through the Python command, we list all the Slack users by accessing the Slack API and get an ID that matches the name "Satrterbot".
This script that gets the ID of the chat robot we only need to run once.
When it runs to provide us with the ID of the chat bot, the script prints out a simple line of output.
In your slack project, use the Python script to print the ID of the slack chat bot to copy the unique ID that the script prints out. And the ID as an environment variable bot_id output.
This script only needs to run once to get the ID of the chat bot. We can now use this ID in our Python application that runs Starterbot.
Code our Starterbot.
Now we have everything we need to write our Starterbot code. Create a new file named starterbot.py, which includes the following code.
Import of OS and slackclient we look familiar, because we've used them in theprintbotid.py.
Through our imported dependency packages, we can use them to obtain the environment variable values and instantiate the slack client.
The code instantiates the Slackclient ' client with the output environment variable Slack_bot_token.
The Slack client connects to the Slack RTM API WebSocket and then loops when parsing messages from Firehose. If there is any message sent to Starterbot, then a function called Handle_command will decide what to do.
Next, add two functions to parse the output of the Slack and process the commands.
The Parse_slack_output function accepts information from slack and makes judgments if they are sent to our Starterbot. The message begins with a direct command to our chat robot ID, which is then handed over to our code. At present, only through the Slack pipeline to publish a message back to tell users to write more Python code!
This is the way the entire program is combined (you can also view the file in GitHub):
Now that we have the code, we can run our Starterbot code through Python starterbot.py.
When Starterbot starts running and the output channel connected to the API creates a new channel in Slack, the Starterbot is invited in, or the Starterbot is invited into an existing channel.
Create a new channel in the Slack interface and invite Starterbot to send commands to Starterbot in your channel now.
Send orders to your Starterbot in your slack channel. If you encounter a problem in the response from the chat bot, you may need to make a change. As this tutorial is written above, one line At_bot = "<@" + bot_id + ":", a colon is required after "@starter" (the name you give your own chat bot). Remove from After At_bot string:. Slack seems to need to add a colon after a person's name, but this seems to be somewhat uncoordinated.
End
Well, now that you've got a simple chat robot, you can add any feature you want to create in many places in your code.
We are able to do many functions with the Slack RTM API and Python. See what you can learn from these articles:
-
< Span style= "FONT-SIZE:14PX;" > Attach a persistent relational database or NoSQL backend such as PostgreSQL, MySQL, or SQLite to save and retrieve user data
-
add another channel that interacts with the chat robot, such as text messages or phone calls
-
integration with other Web APIs such as GitHub, Twilio, or api.ai