1) 安裝Node.js
2) 安裝nodemon 套件
- npm install -g nodemon
- mkdir botTest && cd botTest
- npm init
- npm install @slack/client --save (目前版本是4.0++)
4) 創建Slack API Token
開啟瀏覽器輸入以下網址(my.slack.com 修改成自己的網址)
https://my.slack.com/apps/build/customintegration.
按照以下步驟:
1) 選擇 Custom Integrations
按照以下步驟:
1) 選擇 Custom Integrations
'use strict'; // Import the Real Time Messaging (RTM) client const RtmClient = require('@slack/client').RTMClient; // Import the Web Client const WebClient = require('@slack/client').WebClient; //Slack API Token const token = ""; const web = new WebClient(token); let slack = new RtmClient(token); // When the bot connects success slack.on('connected', () => { let user= {"user":slack.activeUserId}; (async() =>{ let userInfo = await getUserInfo(user); let teamInfo = await getTeamInfo(); console.log(`Connected to ${teamInfo.team.name} as ${userInfo.user.name}`); })(); }); // Start the login process slack.start(); function getUserInfo(user) { return web.users.info(user).then((resp) => { return resp; }).catch((error) => {console.log(error);});; } function getTeamInfo() { return web.team.info().then((resp) => { return resp; }).catch((error) => {console.log(error);}); }
當你在Terminal 看到類似的內容, 代表你的Chat Bot已經成功連結到Slack.
Connected to "Deikhoong" as "hellobot"
加入頻道
Bot 不能透過編程的方式加入頻道,這是防止Bot在沒有被邀請的情況下加入私人頻道。當Bot加入了頻道,頻道中的全部活動都會被Bot 監控。Bot相關可以執行和不可執行的操作在 https://api.slack.com/bot-users 有完整的文件說明。
要將Bot加入頻道可以通過Slack Client中的invite指令. 輸入後將會受到Bot加入頻道的確認訊息。要將Bot從頻道移除一樣通過Slack Client中的remove指令即可。
沒有留言:
張貼留言