Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set user status and get another users status #1038

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 103 additions & 7 deletions O365/teams.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from enum import Enum

from dateutil.parser import parse

Expand All @@ -9,6 +10,24 @@
MAX_BATCH_CHAT_MESSAGES = 50
MAX_BATCH_CHATS = 50

class Availability(Enum):
"""Valid values for Availability."""

AVAILABLE = "Available"
BUSY = "Busy"
AWAY = "Away"
DONOTDISTURB = "DoNotDisturb"


class Activity(Enum):
"""Valid values for Activity."""

AVAILABLE = "Available"
INACALL = "InACall"
INACONFERENCECALL = "InAConferenceCall"
AWAY = "Away"
PRESENTING = "Presenting"


class ConversationMember(ApiComponent):
""" A Microsoft Teams conversation member """
Expand Down Expand Up @@ -667,13 +686,16 @@ class Teams(ApiComponent):
""" A Microsoft Teams class"""

_endpoints = {
'get_my_presence': '/me/presence',
'get_my_teams': '/me/joinedTeams',
'get_channels': '/teams/{team_id}/channels',
'create_channel': '/teams/{team_id}/channels',
'get_channel': '/teams/{team_id}/channels/{channel_id}',
'get_apps_in_team': '/teams/{team_id}/installedApps?$expand=teamsAppDefinition',
'get_my_chats': '/me/chats'
"get_my_presence": "/me/presence",
"get_user_presence": "/users/{user_id}/presence",
"set_my_presence": "/me/presence/setPresence",
"set_my_user_preferred_presence": "/me/presence/setUserPreferredPresence",
"get_my_teams": "/me/joinedTeams",
"get_channels": "/teams/{team_id}/channels",
"create_channel": "/teams/{team_id}/channels",
"get_channel": "/teams/{team_id}/channels/{channel_id}",
"get_apps_in_team": "/teams/{team_id}/installedApps?$expand=teamsAppDefinition",
"get_my_chats": "/me/chats"
}
presence_constructor = Presence
team_constructor = Team
Expand Down Expand Up @@ -727,6 +749,80 @@ def get_my_presence(self):
return self.presence_constructor(parent=self,
**{self._cloud_data_key: data})

def set_my_presence(
self,
session_id,
availability: Availability,
activity: Activity,
expiration_duration,
):
"""Sets my presence status

:param session_id: the session/capplication id.
:param availability: the availability.
:param activity: the activity.
:param activity: the expiration_duration when status will be unset.
:rtype: Presence
"""

url = self.build_url(self._endpoints.get("set_my_presence"))

data = {
"sessionId": session_id,
"availability": availability.value,
"activity": activity.value,
"expirationDutaion": expiration_duration,
}

response = self.con.post(url, data=data)

return self.get_my_presence() if response else None

def set_my_user_preferred_presence(
self,
availability: Availability,
activity: Activity,
expiration_duration,
):
"""Sets my user preferred presence status

:param availability: the availability.
:param activity: the activity.
:param activity: the expiration_duration when status will be unset.
:rtype: Presence
"""

url = self.build_url(self._endpoints.get("set_my_user_preferred_presence"))

data = {
"availability": availability.value,
"activity": activity.value,
"expirationDutaion": expiration_duration,
}

response = self.con.post(url, data=data)

return self.get_my_presence() if response else None

def get_user_presence(self, user_id=None, email=None):
"""Returns specific user availability and activity

:rtype: Presence
"""

url = self.build_url(
self._endpoints.get("get_user_presence").format(user_id=user_id)
)

response = self.con.get(url)

if not response:
return None

data = response.json()

return self.presence_constructor(parent=self, **{self._cloud_data_key: data})

def get_my_teams(self):
""" Returns a list of teams that I am in

Expand Down