-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbitbot.py
80 lines (62 loc) · 2.19 KB
/
bitbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Author: BIT Community
Description:
bitbot is a simple slack greetings bot developed by the BIT community
to help promote opensource and BIT code collaboration.
"""
import time
import sys
from slackclient import SlackClient
from helpers import load_config
slack_settings = dict()
slack_settings['slack'] = ['slack_token', {'channels': 'introduction'}, 'bot_user', {'messages': 'greeting_message'}]
try:
slack_config = load_config(get_settings=slack_settings)
except:
print("Problem loading config settings")
if slack_config:
slack_token = slack_config['slack_token']
introduction_channel_id = slack_config['introduction']
bot_user = slack_config['bot_user']
greeting_message = slack_config['greeting_message']
else:
sys.exit("Couldn't set variables")
def slack_client_connect():
try:
slack_client = SlackClient(slack_token)
except:
slack_client = False
return slack_client
def slack_client_test():
slack_client = slack_client_connect()
print(slack_client.api_call('api.test'))
print(slack_client.api_call('auth.test'))
def slack_post_message(message=None):
slack_client = slack_client_connect()
if message:
post = slack_client.api_call("chat.postMessage", as_user="true:",
channel=introduction_channel_id,
text=message)
else:
post = "There was no message to send."
return post
def slack_event_parser(output):
output_list = output
if output_list and len(output_list) > 0:
for output in output_list:
#print(output['text'], output['channel'])
return output
#slack_client_test()
if __name__ == "__main__":
slack_client = slack_client_connect()
socket_delay = 1
if slack_client.rtm_connect():
print("BITBOT is connected to Slack")
while True:
output = slack_event_parser(slack_client.rtm_read())
if output:
if output['type'] == 'team_join':
slack_post_message(message=greeting_message)
time.sleep(socket_delay)
else:
print("Could not connect to slack. Check your token or network connection.")