slack 使用說明_我如何使用Node和Botkit構建HR Slack Bot

slack 使用說明

為什么要創建Slack Bot? (Why create a Slack Bot ?)

I am an HR professional. More specifically I am a Human Resources Information System (HRIS) Consultant. I work with Application Tracking Systems, Learning Management Systems, and Core HR. But I have never had the opportunity to work with an HR Bot. Which may be the Future of HR.

我是人力資源專業人員。 更具體地說,我是人力資源信息系統(HRIS)顧問。 我使用應用程序跟蹤系統,學習管理系統和核心HR。 但是我從來沒有機會與人力資源Bot合作。 這可能是人力資源的未來。

I read a lot about bots on Slack and Messenger, and used some of them in my daily life — Product Hunt, GitHub and Trello. But for HR purposes, I have never had the opportunity to work with a tool tailored for my needs.

我閱讀了很多有關Slack和Messenger的機器人的信息,并在我的日常生活中使用了其中的一些機器人-Product Hunt,GitHub和Trello。 但是出于人力資源的目的,我從來沒有機會使用針對我的需求量身定制的工具。

That’s why I decided to work on my own bot.

這就是為什么我決定使用自己的機器人的原因。

我的目標 (My Goals)

My bot should be able to manage all the needs a small company could have on Slack:

我的機器人應該能夠管理小公司對Slack的所有需求:

  • Onboarding

    入職
  • Putting people in touch

    與人們保持聯系
  • Reminders

    提醒事項
  • Announcements

    公告內容
  • Birthdays /Anniversary

    生日/周年紀念
  • And many more

    還有很多

復習基礎 (Reviewing the basics)

For this program, I’ll use:

對于此程序,我將使用:

  • Botkit

    Botkit
  • Node JS

    節點JS
  • Express Server

    Express服務器
  • MongoDB

    MongoDB
  • Slack API & of course

    Slack API&當然

Botkit is:

Botkit是:

One easy way to build bot users, especially if you already work with Node.js, is Howdy’s Botkit. Botkit is a framework that takes care of most these API gymnastics, so you can focus on your bot’s behavior.

建立機器人用戶的一種簡單方法是Howdy的Botkit ,尤其是如果您已經使用Node.js的 。 Botkit是負責處理大多數這些API體操的框架,因此您可以專注于機器人的行為。

Exactly what I was looking for :-)

正是我在找什么:-)

Botkit provides a boilerplate for Slack. But I have chosen to start from scratch to have a better understanding of my bot. However, it’s a good idea to train yourself with a bot created on Glitch.

Botkit提供了Slack的樣板。 但是我選擇從頭開始,以更好地了解我的機器人。 但是,最好使用在Glitch上創建的機器人來訓練自己。

Slack機器人如何工作? (How do Slack bots work?)

I am not an expert. I have read again and again Slack and Botkit’s official documentation. I’m still not sure I understood everything. Here is my understanding of a Slack bot’s behavior:

我不是專家。 我一遍又一遍地閱讀了Slack和Botkit的官方文檔。 我仍然不確定我是否了解一切。 這是我對Slack機器人行為的理解:

Every App on Slack has a “scope” which is a perimeter on which an app can read or perform actions. A bot is part of an application created and installed on Slack.

Slack上的每個應用程序都有一個“范圍”,范圍是應用程序可以讀取或執行操作的范圍。 機器人是Slack上創建并安裝的應用程序的一部分。

Therefore, when you install an app on Slack, you give access to some information and permissions to it. For your bot, you want it to be, at least, able to send and reply to messages of other users.

因此,在Slack上安裝應用程序時,您可以訪問某些信息和權限。 對于您的漫游器,您至少希望它能夠發送和回復其他用戶的消息。

There are then two cases:

然后有兩種情況:

  1. You want your bot to react to events happening directly in Slack

    您希望您的機器人對直接在Slack中發生的事件做出React

  2. You want your bot to react to events happening on your server

    您希望機器人對服務器上發生的事件做出React

We will view both of them in this post!

我們將在這篇文章中同時查看它們!

入門 (Getting Started)

Before anything else, you will need a server. In my case, Express.

首先,您需要一臺服務器。 就我而言,快遞。

Below you’ll find my server.js file:

在下面,您可以找到我的server.js文件:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var dotenv = require('dotenv');// configuration ===========================================
//load environment variables,
dotenv.load();// public folder for images, css,...
app.use(express.static(__dirname + '/public'))//parsing
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true
})); //for parsing url encoded// view engine ejs
app.set('view engine', 'ejs');// routes
require('./routes/routes')(app);//botkit
require('./controllers/botkit')//START ===================================================
http.listen(app.get('port'), function() {console.log('listening on port ' + app.get('port'));
});

This port must be public and accessible, not just on a localhost.

此端口必須是公共的并且可以訪問,而不僅僅是在本地主機上。

For the moment, this server is a blank page, showing and processing nothing.

目前,該服務器是空白頁,什么也沒有顯示和處理。

You’ll then need a Slack App: just follow this link to create one.

然后,您將需要一個Slack應用程序:只需點擊此鏈接即可創建一個。

Then, you’ll have to configure your controller. The controller is the brain of your bot. It contains every skill and configuration. Below is my botkit.js file. It has almost the same content found in Botkit’s Starter kit available here: https://github.com/howdyai/botkit-starter-slack

然后,您必須配置控制器。 控制器是您的機器人的大腦。 它包含所有技能和配置。 以下是我的botkit.js文件。 它具有與Botkit入門工具包中可用內容幾乎相同的內容: https : //github.com/howdyai/botkit-starter-slack

var mongoUri = 'mongodb://localhost:27017/nameofyourDB'
var database = require('../config/database')({mongoUri: mongoUri
})
var request = require('request')if (!process.env.SLACK_ID || !process.env.SLACK_SECRET || !process.env.PORT) {console.log('Error: Specify SLACK_ID SLACK_SECRET and PORT in environment');process.exit(1);
}var controller = Botkit.slackbot({storage: database,clientVerificationToken: process.env.SLACK_TOKEN
})exports.controller = controller//CONNECTION FUNCTIONS=====================================================exports.connect = function(team_config) {var bot = controller.spawn(team_config);controller.trigger('create_bot', [bot, team_config]);}// just a simple way to make sure we don't// connect to the RTM twice for the same team
var _bots = {};function trackBot(bot) {_bots[bot.config.token] = bot;
}controller.on('create_bot', function(bot, team) {if (_bots[bot.config.token]) {// already online! do nothing.console.log("already online! do nothing.")} else {bot.startRTM(function(err) {if (!err) {trackBot(bot);console.log("RTM ok")controller.saveTeam(team, function(err, id) {if (err) {console.log("Error saving team")} else {console.log("Team " + team.name + " saved")}})} else {console.log("RTM failed")}bot.startPrivateConversation({user: team.createdBy}, function(err, convo) {if (err) {console.log(err);} else {convo.say('I am a bot that has just joined your team');convo.say('You must now /invite me to a channel so that I can be of use!');}});});}
});//REACTIONS TO EVENTS==========================================================
// Handle events related to the websocket connection to Slackcontroller.on('rtm_open', function(bot) {console.log('** The RTM api just connected!')
});controller.on('rtm_close', function(bot) {console.log('** The RTM api just closed');// you may want to attempt to re-open
});

解鎖第一種情況:對Slack上發生的事件做出React (Unlocking the first case: react to the events happening on Slack)

When you give the right permissions to your app, every time a message is sent on a channel, Slacks sends a request to your server with some information — the channel ID, the user, the timestamp and most importantly, the content of the message.

當您為應用程序授予正確的權限時,每次在通道上發送消息時,Slacks都會向服務器發送請求,其中包含一些信息-通道ID,用戶,時間戳,最重要的是消息的內容。

If we want our bot to react to a simple message like “Hi”, we have to give Slack an address to send the information to.

如果我們希望我們的機器人對諸如“ Hi”這樣的簡單消息做出React,我們必須給Slack一個地址來發送信息。

In a routes.js file write:

在routes.js文件中編寫:

var Request = require('request')
var slack = require('../controllers/botkit')
module.exports = function(app) {app.post('/slack/receive', function(req,res){
//respond to Slack that the webhook has been received.res.status(200);
// Now, pass the webhook into be processedslack.controller.handleWebhookPayload(req, res)})
}

We now have a webhook : http://your-ip-or-domain:port/slack/receive

現在,我們有了一個Webhook: http:// your-ip-or-domain:port / slack / receive

Once Slack is informed of this route via the Event Subscriptions page of your Slack App, it will be able to send it JSON. You will be able to receive it thanks to the parsing part of the server.js file above.

一旦通過Slack應用程序的“事件訂閱”頁面將此路由通知給Slack,它將能夠向其發送JSON。 由于上面的server.js文件的解析部分,您將能夠收到它。

Here is a (simple) schema to explain the process behind it:

這是一個(簡單的)模式來說明其背后的過程:

1- SLACK ? Here is a JSON file with the latest event on your Slack Channel ?

1- SLACK?這是Slack頻道上具有最新事件的JSON文件?

2- SERVER ? Okay well received, I send it to Botkit?

2-服務器?很好,我將其發送給Botkit?

3- BOTKIT ?Here is a temporary answer, wait a second?

3- BOTKIT?這是暫時的答案,請稍等?

4- BOTKIT ? Yeah! I hear a keyword, here is a JSON object with the action to perform ?

4- BOTKIT?是的! 我聽到一個關鍵字,這是一個JSON對象,具有要執行的操作?

If we want our bot to react every time it hears “Hello”, we can simply add this .hears() function to our controller:

如果我們希望我們的機器人在每次聽到“ Hello”時做出React,我們可以簡單地將此.hears()函數添加到我們的控制器中:

controller.hears(['hello', 'hi'], 'direct_message,direct_mention,mention', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {if (user && user.name) {bot.reply(message, 'Hello ' + user.name + '!!');} else {bot.reply(message, 'Hello.');}});
});

Notice the storage.users.get() part in this snippet. Botkit is compatible with almost all the database systems available on the market. I have decided to use MongoDB because it was on my learning list for a long time. Plus the documentation with Botkit is detailed.

注意此片段中的storage.users.get()部分。 Botkit與市場上幾乎所有可用的數據庫系統兼容。 我決定使用MongoDB,因為它在我的學習清單上已經很長時間了。 此外,還詳細介紹了Botkit的文檔。

Now, we have to let our imagination do the work and find some fun features to create.

現在,我們必須讓我們的想象力完成工作,并找到一些有趣的功能來創建。

第二種情況:與您的機器人進行對話 (Second Case: initiate a conversation with your bot)

For this feature, I wanted my bot to react to events which were not initiated on Slack. For example, do a daily routine. If it’s someone’s anniversary in the company, send them a survey asking their feelings about their first months/weeks.

對于此功能,我希望我的機器人對未在Slack上啟動的事件做出React。 例如,做一個日常工作。 如果是公司的周年紀念日,請向他們發送一份調查表,詢問他們對頭幾個月/幾周的感覺。

I have decided to use node-cron: https://github.com/kelektiv/node-cron to manage the daily check.

我決定使用node-cron: https : //github.com/kelektiv/node-cron管理日常檢查。

Here is below a cronjob firing every weekday at 9:00 am. Thanks to the Date() method, the bot gets today’s date and can compare it to the “joinedDate” of the user.

以下是每個工作日上午9:00觸發的cronjob。 借助Date()方法,該機器人可以獲取今天的日期并將其與用戶的“ joinedDate”進行比較。

To get only the right users and avoid a forEach loop, we can use a query on our Database:

為了只獲取合適的用戶并避免forEach循環,我們可以對數據庫使用查詢:

var dailyCheck = new CronJob('00 00 9 * * 1-5', function() {/** Runs every weekday (Monday through Friday)* at 09:00:00 AM. It does not run on Saturday* or Sunday.*/console.log(`DailyCheck triggered ${new Date()}`)//Gets today's datelet d = new Date()d.setUTCHours(0, 0, 0, 0)let threeMonthsAgo = new Date()threeMonthsAgo.setUTCMonth(d.getUTCMonth() - 3)threeMonthsAgo.setUTCHours(0, 0, 0, 0)let sevenDaysAgo = new Date()sevenDaysAgo.setUTCDate(d.getUTCDate() - 7)sevenDaysAgo.setUTCHours(0, 0, 0, 0)controller.storage.users.find({"joinedDate": {"$eq": +sevenDaysAgo}}, function(err, user) {user.forEach(function(member) {console.log(`Message was sent to ${member.name}(${member.id})`)bot.startPrivateConversation({user: member.id}, Conversations.sendSurvey7)})})}, function() {/* This function is executed when the job stops */}, true,/* Start the job right now */timeZone = 'Europe/Paris' /* Time zone of this job. */ )

And… Tada!

還有……多田!

結論 (Conclusion)

After more than a year of being a camper and learning to code, I am really happy to be able to start and finish a project like this one. I now have a bot working and performing almost all the actions I had in mind at the design phase. And I still have a lot of ideas!

在成為一名露營者并學習編碼超過一年之后,我非常高興能夠啟動和完成這樣的項目。 現在,我有一個機器人正在工作,并執行我在設計階段想到的幾乎所有動作。 而且我還有很多想法!

I am still working on this bot. The GitHub repository is available here: https://github.com/alexandrobin/hrbot. Some of the commits are in French, but the codebase is commented in English. :-)

我仍在研究這個機器人。 GitHub存儲庫可在此處找到: https : //github.com/alexandrobin/hrbot 。 一些提交使用法語,但是代碼庫用英語注釋。 :-)

Besides, it’s quite easy to deploy it on Heroku with a Mongolab database if you don’t have a server!

此外,如果沒有服務器,使用Mongolab數據庫在Heroku上部署它也很容易!

If you have some suggestions or are interested by this article and project, feel free to leave a comment ! I would be happy to discuss with you.

如果您有任何建議或對本文和項目感興趣,請隨時發表評論! 我很樂意與您討論。

翻譯自: https://www.freecodecamp.org/news/how-i-built-an-hr-slack-bot-with-node-and-botkit-6b23b81531bb/

slack 使用說明

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393768.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393768.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393768.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux 監聽數據包,linux下網絡監聽與發送數據包的方法(即libpcap、libnet兩種類庫的使用方法)...

linux下可以用libpcap函數庫實現監聽數據包,使用libnet 函數庫發送數據包安裝:在命令行下apt-get install 就可以了libpcap的使用:/*author hjjdate 2011-1-21function:capture packet with the ruler and output the packet informationmodify 2011-1-23function:g…

命令模式(Command Pattern)

1命令模式是一個高內聚的模式。定義如下:將一個請求封裝成一個對象,從而讓你使用不同的請求把客戶端參數化,對請求排隊或者記錄請求日志,可以提供命令的撤銷和恢復功能。 2.角色說明: ● Receive接收者角色 該角色就…

BZOJ 3270: 博物館

傳送門 顯然可以狀態轉移: 設 $f[k][x][y]$ 表示第 $k$ 時刻,第一個人在 $x$ ,第二個人在 $y$ 時的概率 那么轉移顯然: $f[k][x][y]\sum_{u}\sum_{v}f[k-1][u][v]*(1-P_u)(1-P_v)/du[u]/du[v]$ 其中 $u$ 和 $x$ 有邊相連&#xff…

graphpad7.04多組比較p值_同是折線圖為何你卻這么優秀,這才是多組數據作圖應該有的樣子...

相信大家對Excel做折線圖應該不陌生,在展示數據的時候,圖表是一種最好的展示方法。但是經常會碰到一種尷尬的事情就是,當數據維多比較多的時候,做出的圖表就會顯得非常難看。今天我們就來學習一下,多組數據怎么做折線圖…

Logic-算法-八個箱子找一個最輕的

ylbtech-Arithmetic:Logic-算法-八個箱子找一個最輕的-- -- ylb:算法-- Type:算法[logic]-- munu:八個箱子-找一個最輕的-- thankyou:gaoZhimin -- 7:11 2012/3/17-- 有八個正方形的箱子,外觀大小都一樣,其中七個是50斤的,一個是…

由衷的信來激勵有抱負的開發人員

by Logan Wright洛根賴特(Logan Wright) 由衷的信來激勵有抱負的開發人員 (A heartfelt letter to inspire the aspiring developer) I’m writing a letter to my friend. You should read it. He studies Computer Science, and he hates it. I build React Apps and I love…

linux 運行 chom,Hadoop安裝-單節點/偽分布(2.7.3)

1,下載Hadoop目前在Ubuntu的軟件庫里面 沒有發現Hadoop的壓縮包,沒猜錯Hadoop不是可執行文件 只是一個壓縮包吧!所以我們只能自己到官網下載(http://hadoop.apache.org/releases.html);在Apache社區中,下載軟件的時候…

leetcode944. 刪列造序

給定由 N 個小寫字母字符串組成的數組 A,其中每個字符串長度相等。 你需要選出一組要刪掉的列 D,對 A 執行刪除操作,使 A 中剩余的每一列都是 非降序 排列的,然后請你返回 D.length 的最小可能值。 刪除 操作的定義是&#xff1…

python學習:re模塊

常用正則表達式符號 123456789101112131415161718192021. 默認匹配除\n之外的任意一個字符,若指定flag DOTALL,則匹配任意字符,包括換行^ 匹配字符開頭,若指定flags MULTILINE,這種也可以匹配上(r"^a","\nabc\neee&qu…

app之---豆果美食

1.抓包 2.代碼 抓取: #!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import requests from multiprocessing import Queue from handle_pymongo import mongo from concurrent.futures import ThreadPoolExecutorclass Douguo():def __init__(self):s…

語言坐標度分秒的換算_測量位置度說明

測量位置度說明位置度是限制被測要素的實際位置對理想位置變動量的指標。它的定位尺寸為理論正確尺寸。位置度公差在評定實際要素位置的正確性, 是依據圖樣上給定的理想位置。位置度包括點的位置度、線的位置度和面的位置度。[1] 點的位置度:如公差帶前加S¢&#xf…

OpenStack創建win7實例遇到的問題(尚未解決,求幫助)

原地址在這里:(作者也是我,害羞)http://www.aboutyun.com/forum.php?modviewthread&tid22898 小白經過兩天嘗試,用fuel部署好了OpenStack的云平臺,接下來想在Compute節點上創建一個win7 實例&#xff…

VMware使兩臺windows虛擬機能夠互相ping通

如果以下內容測試無效,可參考另一篇:VMware虛擬機配置內網電腦能訪問 1.關閉防火墻 cmd命令行里輸入:netsh firewall set opmode disable 2.測試如果還不能ping通,就把網絡類型選nat類型 3.測試:vmware網關默認是.2 轉…

linux賬號前有個base,安裝 aconda 后Linux的終端界面前部出現(base)字樣

aconda 是做什么用的這里就不說了,一般玩Python的都知道這東西,最早接觸這東西是因為它把NVIDIA中cuda計算和Python互連的一個庫拿下了,是買下來了還是專業,還是唯一合作的也就記不清了,那就是 numba , 那些年頭Python…

回復郵件時如何不要郵件頭_如何為閱讀,點擊和回復率達到100%的CEO設計一封冷郵件...

回復郵件時如何不要郵件頭by Theo Strauss由西奧斯特勞斯(Theo Strauss) 如何為閱讀,點擊和回復率達到100%的CEO設計一封冷郵件 (How to design a cold email for a CEO with a 100% read, click, and response rate) 銀河電子郵件指南:第二…

leetcode1007. 行相等的最少多米諾旋轉(貪心)

在一排多米諾骨牌中,A[i] 和 B[i] 分別代表第 i 個多米諾骨牌的上半部分和下半部分。(一個多米諾是兩個從 1 到 6 的數字同列平鋪形成的 —— 該平鋪的每一半上都有一個數字。) 我們可以旋轉第 i 張多米諾,使得 A[i] 和 B[i] 的值…

Spring 學習教程(一): 認識 Spring 框架

Spring 框架是 Java 應用最廣的框架,它的成功來源于理念,而不是技術本身,它的理念包括 IoC (Inversion of Control,控制反轉) 和 AOP(Aspect Oriented Programming,面向切面編程)。 Spring 的框架結構 Data Access/Int…

小米網關控制空調伴侶_小米有品上架移動空調,支持語音控制

近日小米有品商城上架了一款互聯網可移動空調,機身僅有小米空氣凈化器一般大小,底部安裝了萬向輪,支持多方位自由移動,擁有三大功能,兼顧去暑除濕能力,產品售價1599元,有需求的用戶可以在小米有…

錯誤: 找不到符號

Error:(31, 29) 錯誤: 找不到符號 符號: 類 OnLaunchPluginCallback 位置: 類 IreaderPlugApi 明明我都可以ctrl 單擊點過去,但是就是運行的時候報錯。說錯誤: 找不到符號。 我試了兩遍,把工程clearn, 刪除build下面的文件夾,弄了兩遍&am…

leetcode910. 最小差值 II(貪心)

給定一個整數數組 A,對于每個整數 A[i],我們可以選擇 x -K 或是 x K,并將 x 加到 A[i] 中。 在此過程之后,我們得到一些數組 B。 返回 B 的最大值和 B 的最小值之間可能存在的最小差值。 示例 1: 輸入&#xff1…