Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const RtmClient = require('@slack/client').RtmClient;
- const WebClient = require('@slack/client').WebClient;
- const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
- const bot_token = “your-token-here”
- const rtm = new RtmClient(bot_token);
- const web = new WebClient(bot_token);
- const robotName = “examplebot”;
- const allCommands = {“command1:”, “command2:”}
- function executeCommand(command){
- console.log(command);
- }
- function updateUsers(data) {
- users = data.members;
- }
- function getUsernameFromId(id) {
- const user = users.find(user => user.id === id);
- return user ? user.name : "unknown member";
- }
- rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
- if (message.type === 'message' && message.text) {
- const userName = getUsernameFromId(message.user);
- if(userName !== robotName) {
- if(message.text.indexOf(robotName){
- rtm.sendMessage(“Hey “ + userName + “, I heard that!”, message.channel)
- }
- if (message.text.indexOf(":") > -1) {
- allCommands.forEach(function(command) {
- if (message.text.indexOf(command) > -1) {
- executeCommand(command);
- }
- }
- }
- }
- }
- }
- web.users.list(function(err, data) {
- if (err) {
- console.error('web.users.list Error:', err);
- } else {
- updateUsers(data)
- }
- });
- rtm.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement