我正在使用此Slack API user.getPresence来显示用户的在线状态,无论它是“活动”还是“离开”。我正在使用另一个API,users.info,以获取有关用户的信息。这两个API带有一个用户ID参数,所有参数均来自channels.info API(以下未显示)。
现在,我将两个API分开,因此users.info会获取所有用户的名称和电子邮件,而users.getPresence只会返回该频道所有用户的状态。
这是我尝试连接在一起的两个API:
def users_getPresence():
url = 'https://slack.com/api/users.getPresence'
headers = {'Accept': 'application/x-www-form-urlencoded'}
users = channels_info() # the user IDs from channels.info
result = []
for user in users:
payload = {
'token': API_KEY,
'user': user
}
r = requests.get(url, headers=headers, params=payload)
if r.status_code == 200:
# returns only the presence of 'active' or 'away'
result.append(r.json()['presence'])
else:
return None
return result
def users_info():
url = 'https://slack.com/api/users.info'
headers = {'Accept': 'application/x-www-form-urlencoded'}
users = channels_info() # the user IDs from channels.info
names = []
emails = []
# loops for every user
for user in users:
payload = {
'token': API_KEY,
'user': user
}
r = requests.get(url, headers=headers, params=payload)
name = r.json()['user']['real_name'] # the name of the user
email = r.json()['user']['profile'].get('email') # the email of the user
if r.status_code == 200:
# This is to ignore the users that don't have an email field
if email is not None:
names.append(name)
emails.append(email)
else:
return None
# result returns the name and email of the users
return names, emails
我想要的是先检查users.getPresence以查看哪些用户ID处于“活动”状态,然后获取这些“活动”用户ID并在users.info中使用它们,以仅获取具有“活跃”状态。我当时在想可以使用if语句或类似的语句,但是我不确定如何从users.getPresence获取“活动”用户ID。
您可以将进入的存在列表传递到users_info
中,并检查该用户是否在场。
presences = users_getPresence()
def users_info(presences):
url = 'https://slack.com/api/users.info'
headers = {'Accept': 'application/x-www-form-urlencoded'}
users = channels_info() # the user IDs from channels.info
names = []
emails = []
# loops for every user
for i, user in enumerate(users):
if presences[i] == 'away':
continue
payload = {
'token': API_KEY,
'user': user
}
r = requests.get(url, headers=headers, params=payload)
name = r.json()['user']['real_name'] # the name of the user
email = r.json()['user']['profile'].get('email') # the email of the user
if r.status_code == 200:
# This is to ignore the users that don't have an email field
if email is not None:
names.append(name)
emails.append(email)
else:
return None
# result returns the name and email of the users
return names, emails