Guest User

Untitled

a guest
Jun 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. """
  2. Simple ChatBot for Slack
  3. Author: Ada Kaminkure
  4. A.K.A: ada_92
  5. """
  6.  
  7. import time
  8. import os
  9. from random import randrange
  10. from slackclient import SlackClient
  11.  
  12. # Constant Variables
  13. SLACK_CLIENT = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
  14. BOT_ID = SLACK_CLIENT.api_call("auth.test")["user_id"]
  15. CHANNEL_LIST = SLACK_CLIENT.api_call("channels.list").get("channels", [])
  16. ANSWERS_LIST = [
  17. "Umm, interesting I will ask my master for its meaning",
  18. "I think it cool",
  19. "Howdy!",
  20. "What's up?",
  21. "I see",
  22. "Anythings else?",
  23. "I'm done!!!"
  24. ]
  25.  
  26.  
  27. def find_channels_id(channel_name, channel_list):
  28. """
  29. Find channel id by its name
  30. channel_name (string): name of finding channel
  31. channel_list (list): all channel list in the workspace
  32. return -> channel_id
  33. """
  34. match = [ch.get("id") for ch in channel_list if ch.get("name") == channel_name]
  35. return match[0]
  36.  
  37.  
  38.  
  39. if __name__ == "__main__":
  40. """
  41. Running Bot in this section
  42. """
  43. if SLACK_CLIENT.rtm_connect(with_team_state=False):
  44. print("I'm online, please order me...")
  45. response_channels = find_channels_id("some-channel", CHANNEL_LIST)
  46. SLACK_CLIENT.api_call(
  47. "chat.postMessage",
  48. channel=response_channels,
  49. text="Hello, My name is ICSCO-BOT",
  50. )
  51. while True:
  52. data = SLACK_CLIENT.rtm_read()
  53. if not data:
  54. continue
  55. else:
  56. new_data = data[0]
  57. print(new_data)
  58. if new_data.get("type") == "message" \
  59. and new_data.get("subtype") != "bot_message":
  60.  
  61. SLACK_CLIENT.api_call(
  62. "chat.postMessage",
  63. channel=response_channels,
  64. text=ANSWERS_LIST[randrange(len(ANSWERS_LIST))]
  65. )
  66.  
  67. time.sleep(1)
  68. else:
  69. print("Something wrong, please check your internet connection!")
Add Comment
Please, Sign In to add comment