forked from akarazeev/qiskit-slack-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_slack.py
171 lines (143 loc) · 5.01 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from flask import abort, Flask, jsonify, request
import concurrent.futures as cf
import requests
import logging
import utils
import json
import os
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
app = Flask(__name__)
slack_token = utils.get_token('res/token_slack.json')
pool = cf.ThreadPoolExecutor(4)
extension_gateerrors = '_gateerrors_full.png'
extension_readouterrors = '_readouterrors_full.png'
extension_jobs = '_jobs_full.png'
extension_full = '_full.png'
###
# Slack API
###
@app.route('/confirm', methods=['POST'])
def confirm():
req = request.form.to_dict()
data = json.loads(req["payload"])
backend = data["actions"][0]["name"]
value = data["actions"][0]["value"]
reply = None
name = None
if value.endswith(extension_jobs):
name = 'Pending jobs for {}'.format(backend)
reply = '*Pending jobs* for {} will be sent soon ...'.format(backend)
elif value.endswith(extension_gateerrors):
name = 'Gate errors for {}'.format(backend)
reply = '*Gate errors* for {} will be sent soon ...'.format(backend)
elif value.endswith(extension_readouterrors):
name = 'Readout errors for {}'.format(backend)
reply = '*Readout errors* for {} will be sent soon ...'.format(backend)
elif value.endswith(extension_full):
name = 'Full statistics for {}'.format(backend)
reply = '*Full statistics* for {} will be sent soon ...'.format(backend)
if name is not None:
pool.submit(send_image, 'tmp/{}'.format(value), name, data['channel']['id'])
return reply
###
# Bot Commands
###
@app.route('/gateerrors', methods=['POST'])
def gateerrors():
data = request.form.to_dict()
backend = data['text'].lower()
if backend in utils.backends:
name = 'Gate errors for {}'.format(backend)
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_gateerrors),
name, data['channel_id'])
return "Wait a sec ..."
else:
send_buttons(data["response_url"], extension_gateerrors)
return ''
@app.route('/readouterrors', methods=['POST'])
def readouterrors():
data = request.form.to_dict()
backend = data['text'].lower()
if backend in utils.backends:
name = 'Gate errors for {}'.format(backend)
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_readouterrors),
name, data['channel_id'])
return "Wait a sec ..."
else:
send_buttons(data["response_url"], extension_readouterrors)
return ''
@app.route('/jobs', methods=['GET', 'POST'])
def jobs():
data = request.form.to_dict()
backend = data['text'].lower()
return "kek"
if backend in utils.backends:
name = 'Pending jobs for {}'.format(backend)
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_jobs),
name, data['channel_id'])
return "Wait a sec ..."
else:
send_buttons(data["response_url"], extension_jobs)
return ''
@app.route('/full', methods=['POST'])
def full():
data = request.form.to_dict()
backend = data['text'].lower()
if backend in utils.backends:
name = 'Full statistics for {}'.format(backend)
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_full),
name, data['channel_id'])
return "Wait a sec ..."
else:
send_buttons(data["response_url"], extension_full)
return ''
###
# Helper Functions
###
def send_image(path, name, channel):
my_file = {
'file': (path, open(path, 'rb'), 'png')
}
payload = {
"filename": '{}.png'.format(name),
'token': slack_token,
"channels": [channel]
}
response = requests.post("https://slack.com/api/files.upload", params=payload, files=my_file)
def send_buttons(response_url, extension):
payload = {
"text": "What backend you are interested in?",
"attachments": [
{
"text": "Choose a backend :qiskit:",
"callback_id": 42,
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "ibmqx4",
"text": "ibmqx4",
"type": "button",
"value": "ibmqx4{}".format(extension)
},
{
"name": "ibmqx5",
"text": "ibmqx5",
"type": "button",
"value": "ibmqx5{}".format(extension)
},
]
}
]
}
headers = {
'content-type': "application/json",
}
response = requests.request("POST", response_url, data=json.dumps(payload), headers=headers)
print(response.text)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)