1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from botbuilder .core import TurnContext
5
+ from botbuilder .schema .teams import ChannelInfo , TeamsChannelData , TeamDetails
6
+ from botbuilder .connector import BotBuilder
7
+
8
+ from typing import List
9
+
10
+ class TeamsInfo :
11
+ @staticmethod
12
+ async def get_team_details (self , turn_context : TurnContext , team_id : str = None ) -> TeamDetails :
13
+ team_info = turn_context .activity .teams_get_team_info ()
14
+ t = team_id or (team_id .id if team_info and team_info .id else None )
15
+ if not t :
16
+ raise Exception ("This method is only valid within the scope of MS Teams team" )
17
+ return await self ._get_teams_connector_client (turn_context ).teams .fetch_team_details_activity (t )
18
+
19
+ @staticmethod
20
+ async def get_team_channel_activity (self , turn_context : TurnContext , team_id : str = None ) -> List [ChannelInfo ]:
21
+ team_info = turn_context .activity .teams_get_team_info ()
22
+ t = team_id or (team_id .id if team_info and team_info .id else None )
23
+ if not t :
24
+ raise Exception ("This method is only valid within the scope of MS Teams team" )
25
+ channel_list = await self ._get_teams_connector_client (turn_context ).teams .fetch_channel_list_activity (t )
26
+ return channel_list .conversations
27
+
28
+ @staticmethod
29
+ async def get_team_members_activity (self , turn_context : TurnContext , team_id : str = None ) -> List [TeamsChannelAccount ]:
30
+ team_info = turn_context .activity .teams_get_team_info ()
31
+ t = team_id or (team_id .id if team_info and team_info .id else None )
32
+ if not t :
33
+ raise Exception ("This method is only valid within the scope of MS Teams team" )
34
+ return self ._get_members_activity (self ._get_connector_client (turn_context ), t )
35
+
36
+ @staticmethod
37
+ async def get_members_activity (self , turn_context : TurnContext ):
38
+ team_info = turn_context .activity .teams_get_team_info ()
39
+ if team_info and team_info .id :
40
+ return self ._get_team_members_activity (turn_context , team_info .id )
41
+ else :
42
+ conversation_id = turn_context .activity .conversation .id if turn_context .activity and turn_context .activity .conversation else None
43
+ return self ._get_members_activity (self ._get_connector_client (turn_context ), conversation_id )
44
+
45
+ @staticmethod
46
+ async def get_members_activity (self , connector_client : ConnectorClient , conversation_id : str ) -> List [TeamsChannelAccount ]:
47
+ if not conversation_id :
48
+ raise Exception ("The GetMembers operation needs a valid conversation ID." )
49
+
50
+ team_members = await connector_client .conversations .get_conversation_members_activity (conversation_id )
51
+ teams_channel_accounts = [TeamsChannelAccount (** members ) for members in team_members ]
52
+ return teams_channel_accounts
53
+
54
+ @staticmethod
55
+ def get_team_id (self , turn_context : TurnContext ):
56
+ if not turn_context :
57
+ raise Exception ("Missing context parameter" )
58
+
59
+ if not turn_context .activity :
60
+ raise Exception ("Missing activyt on turn_context" )
61
+
62
+ channel_data = turn_context .activity .channel_data
63
+ team = channel_data .team if channel_data and channel_data .team else None
64
+ team_id = team .id if team and type (team .id ) == 'str' else None
65
+ return team_id
66
+
67
+ @staticmethod
68
+ def get_connector_client (self , turn_context : TurnContext ) -> ConnectorClient :
69
+ if not turn_context .adapter or (not 'createConnectorClient' in turn_context .adapter ):
70
+ raise Exception ("This method requries a connector client" )
71
+ return turn_context .adapter .create_connector_client (turn_context .activity .service_url )
72
+
73
+ @staticmethod
74
+ def get_teams_connector_client (self , turn_context : TurnContext ) -> TeamsConnectorClient :
75
+ connector_client = self ._get_connector_client (turn_context )
76
+ return TeamsConnectorClient (connector_client .credentials , {"base_url" : turn_context .activity .service_url })
0 commit comments