-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.py
62 lines (44 loc) · 1.89 KB
/
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
# lambdacooler, the open-source slack watercooler.
# Copyright (C) 2021 Vanshaj Singhania
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import requests, urllib
import variables as var
def get(url):
return requests.get(url, headers={"Authorization": f"Bearer {var.TOKEN}"}).json()
def get_channels():
api_url = var.BASE_URL.format("conversations.list")
return get(api_url)["channels"]
def all_users():
api_url = var.BASE_URL.format("users.list")
members = get(api_url)["members"]
res = {}
for member in members:
res[member["id"]] = member
return res
def user_from_email(email): # unused
api_url = var.BASE_URL.format("users.lookupByEmail")
api_url += "?email=" + email
return get(api_url)["user"]
def lambdacooler_users():
api_url = var.BASE_URL.format("conversations.members")
api_url += "?channel=" + var.LAMBDACOOLER_CHANNEL
return get(api_url)["members"]
def open_dm(users):
api_url = var.BASE_URL.format("conversations.open")
api_url += "?users=" + ",".join(users)
id = get(api_url)["channel"]["id"]
return lambda message: send_message(id, message)
def send_message(channel, message):
api_url = var.BASE_URL.format("chat.postMessage")
api_url += "?channel=" + channel
api_url += "&text=" + urllib.parse.quote_plus(message)
get(api_url)