Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. const RtmClient = require('@slack/client').RtmClient;
  2. const WebClient = require('@slack/client').WebClient;
  3. const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
  4.  
  5. const bot_token = “your-token-here”
  6. const rtm = new RtmClient(bot_token);
  7. const web = new WebClient(bot_token);
  8.  
  9. const robotName = “examplebot”;
  10. const allCommands = {“command1:”, “command2:”}
  11.  
  12. function executeCommand(command){
  13. console.log(command);
  14. }
  15.  
  16. function updateUsers(data) {
  17. users = data.members;
  18. }
  19.  
  20. function getUsernameFromId(id) {
  21. const user = users.find(user => user.id === id);
  22. return user ? user.name : "unknown member";
  23. }
  24.  
  25.  
  26.  
  27. rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) {
  28. if (message.type === 'message' && message.text) {
  29. const userName = getUsernameFromId(message.user);
  30. if(userName !== robotName) {
  31. if(message.text.indexOf(robotName){
  32. rtm.sendMessage(“Hey “ + userName + “, I heard that!”, message.channel)
  33. }
  34. if (message.text.indexOf(":") > -1) {
  35. allCommands.forEach(function(command) {
  36. if (message.text.indexOf(command) > -1) {
  37. executeCommand(command);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44.  
  45. web.users.list(function(err, data) {
  46. if (err) {
  47. console.error('web.users.list Error:', err);
  48. } else {
  49. updateUsers(data)
  50. }
  51. });
  52.  
  53. rtm.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement