新手指南:dvwa
By Vishwa Shah
Vishwa Shah着
Update: code and tutorial updated on June 28 to reflect Slack API changes.
更新:代码和教程已于6月28日更新,以反映Slack API的更改 。
Slackbots:为什么要使用它们? (Slackbots: Why use them?)
Before we get into the tutorial part of this post, let’s take a look at why this can be a worthy project and tool.
在进入本文的教程部分之前,让我们看一下为什么这可以成为一个有价值的项目和工具。
Slack is an increasingly popular tool for team-wide communication. It’s grown to include plugins for other widely used project management tools, like JIRA, Google Drive, and the likes. Any slack user knows — the more you can do from within the conversation, the better.
Slack是用于团队范围通信的越来越流行的工具。 它已经成长为包括其他广泛使用的项目管理工具(例如JIRA,Google云端硬盘等)的插件。 任何闲散的用户都知道-您在对话中可以做的越多越好。
Common uses for a slackbot range from a simple notifier for when a task is complete (like a test build, or when your lunch is ready) to interactive, button-based bots that execute commands at the user’s will. You can build polling mechanisms, conversational bots, and more.
slackbot的常见用途包括从简单的通知程序完成任务(例如测试版本,或午餐准备就绪)到根据用户意愿执行命令的基于按钮的交互式bot。 您可以构建轮询机制,对话机器人等。
设置python编程环境 (Setting up a python programming environment)
If you’re a windows user and you haven’t used python before, you’ll need to install it. Linux/Mac users: Unix comes with python!
如果您是Windows用户,并且以前从未使用过python,则需要安装它。 Linux / Mac用户:Unix随附python!
Once installed, fire up your terminal and type python
or python3
(if you have multiple installations) to make sure it works and is there.
安装完成后,启动您的终端并输入python
或python3
(如果您有多个安装),以确保其正常工作并存在。
Also check to see you have a good text editor for code: sublime and atom are great choices.
还要检查一下,看看您有一个不错的代码文本编辑器: sublime和atom是不错的选择。
Optional: It might also be useful to work in a virtual environment — it’s good practice for when you have a lot of dependencies.
可选:在虚拟环境中工作可能也很有用-当您有很多依赖项时,这是一个好习惯。
pip install virtualenv
virtualenv tutorial
source tutorial/bin/activate
You should also fork the tutorial GitHub repo and clone to your local machine, as we’ll be using that code as a framework for this tutorial.
您还应该将教程GitHub存储库分叉并克隆到您的本地计算机上,因为我们将使用该代码作为本教程的框架。
To do this, go to the repo and click Fork
on the top right. The forked repo should be <yourusername>/slackbot-tutorial. Hit the green Clone or download
button on the right under the stats bar, and copy the url. Return to the terminal to clone the repository:
为此,请转到存储库 ,然后单击右上角的Fork
。 分叉的仓库应该是<yourusername> / slackbot-tutorial 。 点击统计信息栏下方右侧的绿色Clone or download
按钮,然后复制网址。 返回终端以克隆存储库:
cd Desktop/
git clone https://github.com/yourusername/slackbot-tutorial.git
cd slackbot-tutorial/
sublime . (or open your text editor and open this directory)
松弛应用 (Slack Apps)
There are two ways to go about creating your slackbot: standalone bots, or Slack apps. Apps allow a wider range of functionality going forward, and is Slack’s recommended route for creating a bot user.
创建slackbot有两种方法:独立bot或Slack应用。 应用程序可以支持更广泛的功能,这是Slack创建机器人用户的推荐途径。
Go to https://api.slack.com/apps and hit Create New App
on the top right. Give it a name and pick a workspace where you can create a channel to test your bot in. You can always reconfigure your bot for another workspace later, or even post it to the Slack App Directory.
转到https://api.slack.com/apps,然后点击右上角的Create New App
。 给它起个名字并选择一个工作区,您可以在其中创建一个通道来测试您的bot。以后,您始终可以为另一个工作区重新配置bot,甚至将其发布到Slack App Directory中。
I recommend making a #test channel in a workspace you create just for development purposes, or one that has relatively few users who wouldn’t mind you testing something out there.
我建议您在仅为开发目的而创建的工作空间中创建一个#test通道,或者在相对较少的用户不介意您在那里进行测试的工作空间中创建#test通道。
The first thing you’ll want to do is get the bot token. When you get to the above page, click Bots. Add some scopes; these determine what permissions your app’s bot user will have. To start, chat:write and im:write are probably enough.
您要做的第一件事就是获取机器人令牌。 当您转到上一页时,单击Bots。 添加一些范围; 这些决定了您的应用的机器人用户将拥有哪些权限。 首先, 聊天:写 和 im:写 可能就足够了。
Now, to actually get your tokens, you’ll want to go to OAuth & Permissions
on the left sidebar.
现在,要实际获取令牌,您需要转到左侧边栏上的OAuth & Permissions
。
Here, you’ll be able to Install the App to the Workspace
and generate the necessary tokens. As a rule of thumb, bot tokens start with xoxb-.
在这里,您将能够Install the App to the Workspace
并生成必要的令牌。 根据经验, 机器人令牌以xoxb-.
You’ll also want the signing secret, which is located under Basic Information > App Credentials.
您还将需要signing机密 ,该机密位于“基本信息”>“应用程序凭据”下。
扮演你的机器人 (Acting as your Bot)
Now you have the credentials necessary to make API calls and act as your bot. To test this out, fire up a terminal and run this (with the correct token and channel name):
现在,您具有进行API调用并充当机器人的必要凭据。 要对此进行测试,请启动一个终端并运行它(使用正确的令牌和通道名称):
curl -X POST \-H 'Authorization: Bearer xoxb-your-token' \-H 'Content-type: application/json;charset=utf-8' \--data '{"channel":"#test","text":"Hello, Slack!"}' \
https://slack.com/api/chat.postMessage
If you go to that channel in your slack workspace, you should now see a message from your bot! You just made an HTTP POST request — asked a server to post a message somewhere.
如果您在闲置的工作空间中转到该频道,现在应该会看到机器人发出的消息! 您刚刚发出了HTTP POST请求-要求服务器在某处发布消息。
对机器人编程 (Programming the Bot)
We want to do the above programatically. There are a few different ways you can set up a slackbot. I’ll cover the following:
我们要以编程方式执行上述操作。 设置slackbot的方式有几种。 我将介绍以下内容:
- Triggered periodically (on a schedule) to say something 定期触发(按计划)说些什么
- /slash commands /斜杠命令
The second requires a server running, while the first does not.
第二个要求服务器运行,而第一个不需要。
预定讯息 (Scheduled Messages)
Let’s say you want to periodically send a message somewhere — maybe every Monday morning. Go to the text editor where you opened up slackbot-tutorial
.
假设您想定期在某处发送消息-也许每个星期一早上。 转到打开slackbot-tutorial
的文本编辑器。
You should see a file scheduled.py
. Take a look: sendMessage
is a function that fires off the API call to slack and posts a message. At the bottom, you’ll see the main method: what executes when you run the script. Here, you’ll see a few things to note:
您应该看到一个文件scheduled.py
。 看一下: sendMessage
是一个函数,该函数会触发API调用以放松并发布消息。 在底部,您将看到主要方法:运行脚本时执行的方法。 在这里,您会看到一些注意事项:
SLACK_BOT_TOKEN
is pulled fromos.environ['SLACK_BOT_TOKEN']
— how? Runexport SLACK_BOT_TOKEN="xoxb-your-token"
in your terminal to set this variable.SLACK_BOT_TOKEN
是从os.environ['SLACK_BOT_TOKEN']
-如何? 在终端中运行export SLACK_BOT_TOKEN="xoxb-your-token"
来设置此变量。a scheduler is used here, and there’s an infinite loop that checks for events on the scheduler. By default here, I’ve scheduled the
sendMessage
function to be called every minute.这里使用了调度程序,并且存在一个无限循环,用于检查调度程序上的事件。 默认情况下,我已安排
sendMessage
函数每分钟调用一次。
To test this out, go back to the terminal where you’re in the slackbot-tutorial
directory and run
要进行测试,请返回到slackbot-tutorial
目录中的终端并运行
export SLACK_BOT_TOKEN="xoxb-your-token"
python scheduled.py
You should see the log messages print. Make sure you’ve changed channel=#test
in the code to your test channel name (if different) and added your bot (in the slack channel, type /invite @botname
. Let it run for a couple minutes and watch the messages show up on Slack!
您应该看到日志消息打印。 确保已将代码中的channel=#test
更改为测试频道名称(如果有),并添加了您的机器人(在松弛频道中,键入/invite @botname
。让它运行几分钟,然后观看显示的消息)靠松弛!
This is, of course, a super basic implementation of a scheduled message sender — you can actually do this just with slackbot /remind #test “Hello, Slack!” every Monday at 9am
.
当然,这是预定消息发送者的超基本实现-您实际上可以仅使用slackbot /remind #test “Hello, Slack!” every Monday at 9am
/remind #test “Hello, Slack!” every Monday at 9am
。
The true power here is that you can substitute in any function for sendMessage
, leveraging the power of interfacing with external services through APIs, doing math, etc and then constructing a message to post.
真正的强大之处在于,您可以用sendMessage
替代任何功能,利用API通过与外部服务接口,进行数学运算等功能来构造要发布的消息。
斜杠命令 (Slash Commands)
This one requires a little more setup — go back to your app settings > Slash Commands. Create a new slash command: for example, /test
. For the request URL, you’ll need to either deploy this web server (I use Heroku), or run a local ngrok
instance to test it. The latter will run it locally, and is best for testing. You can brew install ngrok
or get it from here.
这需要更多的设置-返回您的应用设置 > Slash命令。 创建一个新的斜杠命令:例如/test
。 对于请求URL,您将需要部署此Web服务器(我使用Heroku),或运行本地ngrok
实例对其进行测试。 后者将在本地运行,最适合测试。 您可以brew install ngrok
或从此处获取它。
In the starter code repo, look for slashCommand.py
to start understanding this method. To start the server, run python server.py
. The Request URL to put in Slack will be given by your ngrok
instance and the @app.route
in your code — it would be something like http://a1234b5cde6f.ngrok.io/slack/test (the bold part comes from the route defined in the code). You should be able to test the slash commands in your Slack workspace. From the tutorial code, try /test
.
在入门代码存储库中,查找slashCommand.py
以开始了解此方法。 要启动服务器,请运行python server.py
。 放入Slack的请求URL将由您的ngrok
实例和代码中的@app.route
提供-类似于http://a1234b5cde6f.ngrok.io / slack / test (粗体部分来自路由在代码中定义)。 您应该能够在Slack工作区中测试斜杠命令。 在教程代码中,尝试/test
。
向前进 (Moving Forward)
Now you have a very basic slackbot that either operates on a command or runs every so often. Be creative with how you use it! Think about what else you can link this skeleton to to make it more useful.
现在,您有了一个非常基本的slackbot,它可以在命令上运行,也可以经常运行。 发挥您的创造力! 考虑一下您还可以链接此框架以使其更有用。
您的漫游器可能会做出其他React (Other ways your bot might respond)
- Actions/responses could be triggered by mentions or certain phrases. This requires running a server and listening the messages somewhere. 动作/响应可能由提及或某些短语触发。 这需要运行服务器并在某处侦听消息。
- You bot could be conversational, and might contribute to threads. Check out some NLP to get started on having intelligible conversation! Word2Vec + TensorFlow or Keras might be a place to start. DialogFlow is also great. 您的漫游器可能是对话式的,并且可能有助于线程。 查看一些NLP,开始进行可理解的对话! Word2Vec + TensorFlow或Keras可能是一个起点。 DialogFlow也很棒。
Link it up with some other APIs. Maybe you want to be able to interact with a Google Sheet and run some calculations. You might want to send other users a message based on some actions. Integrate buttons. Perhaps you want to trigger messages based on something else.
将其与其他一些API链接起来。 也许您希望能够与Google表格进行交互并运行一些计算。 您可能希望根据某些操作向其他用户发送消息。 集成按钮 。 也许您想基于其他内容触发消息。
翻译自: https://www.freecodecamp.org/news/how-to-build-a-basic-slackbot-a-beginners-guide-6b40507db5c5/
新手指南:dvwa