-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_slack.py
85 lines (72 loc) · 2.38 KB
/
main_slack.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
81
82
83
84
85
from slackclient import SlackClient
import logging
import json
import time
import os
import utils
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
slack_client = SlackClient(utils.get_token('res/token_slack.json'))
starterbot_id = None
# Constants.
RTM_READ_DELAY = 0.1 # 1 second delay between reading from RTM.
counter = 0
def parse_bot_commands(slack_events):
for event in slack_events:
if event["type"] == "message" and "subtype" not in event:
message = event["text"]
return message, event["channel"]
return None, None
def handle_command(command, channel):
global counter
backend = command.lower()
if backend in utils.backends:
counter += 1
response = "Wait a sec ..."
slack_client.api_call(
"chat.postMessage",
channel=channel,
text=response
)
# utils.create_statistics(backend)
slack_client.api_call(
'files.upload',
channels=channel,
as_user=True,
filename=backend,
file=open('tmp/{}_to_send.png'.format(backend), 'rb'),
)
elif command == 'info':
response = str(counter)
slack_client.api_call(
"chat.postMessage",
channel=channel,
text=response
)
else:
response = list()
response.append("I'm sorry, I don't understand!")
response.append("I understand only these messages: *ibmqx4* or *ibmqx5*")
response = '\n'.join(response)
slack_client.api_call(
"chat.postMessage",
channel=channel,
text=response
)
def main():
if slack_client.rtm_connect(with_team_state=False):
# Read bot's user ID by calling Web API method `auth.test`.
starterbot_id = slack_client.api_call("auth.test")["user_id"]
logger.info("Bot is connected and running!")
while True:
command, channel = parse_bot_commands(slack_client.rtm_read())
if command:
handle_command(command, channel)
time.sleep(RTM_READ_DELAY)
else:
logger.info("Connection failed. Exception traceback printed above.")
if __name__ == "__main__":
main()