This repository has been archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_integration.py
55 lines (49 loc) · 2.19 KB
/
slack_integration.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
from slackclient import SlackClient
import requests, json
import argparse
parser = argparse.ArgumentParser(description='Launch integration')
parser.add_argument('--name', default='westebot', help='The name of our bot')
parser.add_argument('--token', help='Slack API token', required=True)
parser.add_argument('--url', help='backend url', default='http://ndi.eliott.tech/api/answer/')
args = parser.parse_args()
BOT_NAME = args.name
BOT_TOKEN = args.token
API_URL = args.url
slack_client = SlackClient(BOT_TOKEN)
if __name__ == "__main__":
api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
# retrieve all users so we can find our bot
users = api_call.get('members')
user = None
for u in users:
if 'name' in u and u.get('name') == BOT_NAME:
print("Bot ID for '" + u['name'] + "' is " + u.get('id'))
user = u
# Get every channels"""
for i in slack_client.api_call(
"conversations.list",
types="public_channel, private_channel"
)['channels']:
if i['name'] == 'general':
while True:
u = slack_client.api_call("channels.info", channel=i['id'])
if 'channel' in u:
message = u['channel']['latest']
if message['type'] == 'message':
# If the message was sent by a humain
if 'user' in message:
print(message)
question = message['text']
answer = json.loads(requests.post(
API_URL,
data = dict(question=question)
).text)['answer']
slack_client.api_call(
"chat.postMessage",
channel="#general",
text=answer,
user=user.get('id')
)
else:
print("could not find bot user with the name " + BOT_NAME)