創建機器人
利用机器人可以做一些重复性的工作。
创建 bot user,就是可以跟它进行实时聊天。
https://api.slack.com/bot-users
https://my.slack.com/services/new/bot

創建后拿到 token

讓機器人在綫
安装 botkit, botkit 是一个 node 的库。
| 1
 | npm install --save botkit
 | 
创建 bot.js
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | var Botkit = require('botkit');var controller = Botkit.slackbot();
 var bot = controller.spawn({
 token: your_slack_bot_token // 填入申請的 token
 })
 
 bot.startRTM(function(err,bot,payload) { //開啓實時聊天
 if (err) {
 throw new Error('Could not connect to Slack');
 }
 });
 
 //處理來自客戶端發來的請求
 controller.hears(["keyword", "^pattern$"], ["direct_message", "direct_mention", "mention", "ambient"],
 function(bot, message) {
 bot.reply(message, 'You used a keyword!');
 });
 
 
 | 
效果

機器人主動推送
調用 bot.reply(message, 'You used a keyword!');
如
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 |   var m = {type: 'message',
 channel: 'D2FFFEXS7',
 user: 'U2CE0K880',
 text: 'op',
 ts: '1474725618.000015',
 team: 'T2BD7KZH6',
 event: 'direct_message',
 }
 bot.reply(m, 'gagagaga'); //發送消息給 user
 
 | 
自定義消息的樣式
https://api.slack.com/docs/messages/builder

文章来自: https://hanks.pub