新手指南: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