11
11
MessageReaction ,
12
12
ResourceResponse ,
13
13
)
14
-
14
+ from botbuilder .schema .teams import (
15
+ ChannelInfo ,
16
+ NotificationInfo ,
17
+ TeamInfo ,
18
+ TeamsChannelAccount ,
19
+ TeamsChannelData ,
20
+ TenantInfo ,
21
+ )
22
+ from botframework .connector import Channels
15
23
16
24
class TestingTeamsActivityHandler (TeamsActivityHandler ):
17
25
def __init__ (self ):
18
26
self .record : List [str ] = []
19
27
28
+ async def on_conversation_update_activity (self , turn_context : TurnContext ):
29
+ self .record .append ("on_conversation_update_activity" )
30
+ return await super ().on_conversation_update_activity (turn_context )
31
+
32
+ async def on_teams_members_added_activity (self , teams_members_added : [TeamsChannelAccount ], turn_context : TurnContext ):
33
+ self .record .append ("on_teams_members_added_activity" )
34
+ return await super ().on_teams_members_added_activity (teams_members_added , turn_context )
35
+
36
+ async def on_teams_members_removed_activity (self , teams_members_removed : [TeamsChannelAccount ], turn_context : TurnContext ):
37
+ self .record .append ("on_teams_members_removed_activity" )
38
+ return await super ().on_teams_members_removed_activity (teams_members_removed , turn_context )
39
+
20
40
async def on_message_activity (self , turn_context : TurnContext ):
21
41
self .record .append ("on_message_activity" )
22
42
return await super ().on_message_activity (turn_context )
23
43
24
- async def on_members_added_activity (
25
- self , members_added : ChannelAccount , turn_context : TurnContext
26
- ):
27
- self .record .append ("on_members_added_activity" )
28
- return await super ().on_members_added_activity (members_added , turn_context )
29
-
30
- async def on_members_removed_activity (
31
- self , members_removed : ChannelAccount , turn_context : TurnContext
32
- ):
33
- self .record .append ("on_members_removed_activity" )
34
- return await super ().on_members_removed_activity (members_removed , turn_context )
35
-
36
- async def on_message_reaction_activity (self , turn_context : TurnContext ):
37
- self .record .append ("on_message_reaction_activity" )
38
- return await super ().on_message_reaction_activity (turn_context )
39
-
40
- async def on_reactions_added (
41
- self , message_reactions : List [MessageReaction ], turn_context : TurnContext
42
- ):
43
- self .record .append ("on_reactions_added" )
44
- return await super ().on_reactions_added (message_reactions , turn_context )
45
-
46
- async def on_reactions_removed (
47
- self , message_reactions : List [MessageReaction ], turn_context : TurnContext
48
- ):
49
- self .record .append ("on_reactions_removed" )
50
- return await super ().on_reactions_removed (message_reactions , turn_context )
51
-
52
44
async def on_token_response_event (self , turn_context : TurnContext ):
53
45
self .record .append ("on_token_response_event" )
54
46
return await super ().on_token_response_event (turn_context )
@@ -60,7 +52,32 @@ async def on_event(self, turn_context: TurnContext):
60
52
async def on_unrecognized_activity_type (self , turn_context : TurnContext ):
61
53
self .record .append ("on_unrecognized_activity_type" )
62
54
return await super ().on_unrecognized_activity_type (turn_context )
55
+
56
+ async def on_teams_channel_created_activity (
57
+ self , channel_info : ChannelInfo , team_info : TeamInfo , turn_context : TurnContext
58
+ ):
59
+ self .record .append ("on_teams_channel_created_activity" )
60
+ return await super ().on_teams_channel_created_activity (channel_info , team_info , turn_context )
61
+
62
+ async def on_teams_channel_renamed_activity (
63
+ self , channel_info : ChannelInfo , team_info : TeamInfo , turn_context : TurnContext
64
+ ):
65
+ self .record .append ("on_teams_channel_renamed_activity" )
66
+ return await super ().on_teams_channel_renamed_activity (channel_info , team_info , turn_context )
67
+
68
+ async def on_teams_channel_deleted_activity (
69
+ self , channel_info : ChannelInfo , team_info : TeamInfo , turn_context : TurnContext
70
+ ):
71
+ self .record .append ("on_teams_channel_deleted_activity" )
72
+ return await super ().on_teams_channel_renamed_activity (channel_info , team_info , turn_context )
73
+
74
+ async def on_teams_team_renamed_activity (self , team_info : TeamInfo , turn_context : TurnContext ):
75
+ self .record .append ("on_teams_team_renamed_activity" )
76
+ return await super ().on_teams_team_renamed_activity (team_info , turn_context )
63
77
78
+ async def on_invoke_activity (self , turn_context : TurnContext ):
79
+ self .record .append ("on_invoke_activity" )
80
+ return await super ().on_invoke_activity (turn_context )
64
81
65
82
class NotImplementedAdapter (BotAdapter ):
66
83
async def delete_activity (
@@ -76,18 +93,140 @@ async def send_activities(
76
93
async def update_activity (self , context : TurnContext , activity : Activity ):
77
94
raise NotImplementedError ()
78
95
79
-
80
96
class TestTeamsActivityHandler (aiounittest .AsyncTestCase ):
81
- async def test_message_reaction (self ):
82
- # Note the code supports multiple adds and removes in the same activity though
83
- # a channel may decide to send separate activities for each. For example, Teams
84
- # sends separate activities each with a single add and a single remove.
97
+ async def test_on_teams_channel_created_activity (self ):
98
+ #arrange
99
+ activity = Activity (
100
+ type = ActivityTypes .conversation_update ,
101
+ channel_data = {
102
+ "eventType" : "channelCreated" ,
103
+ "channel" : {
104
+ "id" : "asdfqwerty" ,
105
+ "name" : "new_channel"
106
+ }
107
+ },
108
+ channel_id = Channels .ms_teams
109
+ )
110
+
111
+ turn_context = TurnContext (NotImplementedAdapter (), activity )
112
+
113
+ # Act
114
+ bot = TestingTeamsActivityHandler ()
115
+ await bot .on_turn (turn_context )
116
+
117
+ # Assert
118
+ assert len (bot .record ) == 2
119
+ assert bot .record [0 ] == "on_conversation_update_activity"
120
+ assert bot .record [1 ] == "on_teams_channel_created_activity"
121
+
122
+ async def test_on_teams_channel_renamed_activity (self ):
123
+ #arrange
124
+ activity = Activity (
125
+ type = ActivityTypes .conversation_update ,
126
+ channel_data = {
127
+ "eventType" : "channelRenamed" ,
128
+ "channel" : {
129
+ "id" : "asdfqwerty" ,
130
+ "name" : "new_channel"
131
+ }
132
+ },
133
+ channel_id = Channels .ms_teams
134
+ )
135
+
136
+ turn_context = TurnContext (NotImplementedAdapter (), activity )
137
+
138
+ # Act
139
+ bot = TestingTeamsActivityHandler ()
140
+ await bot .on_turn (turn_context )
85
141
86
- # Arrange
142
+ # Assert
143
+ assert len (bot .record ) == 2
144
+ assert bot .record [0 ] == "on_conversation_update_activity"
145
+ assert bot .record [1 ] == "on_teams_channel_renamed_activity"
146
+
147
+ async def test_on_teams_channel_deleted_activity (self ):
148
+ #arrange
149
+ activity = Activity (
150
+ type = ActivityTypes .conversation_update ,
151
+ channel_data = {
152
+ "eventType" : "channelDeleted" ,
153
+ "channel" : {
154
+ "id" : "asdfqwerty" ,
155
+ "name" : "new_channel"
156
+ }
157
+ },
158
+ channel_id = Channels .ms_teams
159
+ )
160
+
161
+ turn_context = TurnContext (NotImplementedAdapter (), activity )
162
+
163
+ # Act
164
+ bot = TestingTeamsActivityHandler ()
165
+ await bot .on_turn (turn_context )
166
+
167
+ # Assert
168
+ assert len (bot .record ) == 2
169
+ assert bot .record [0 ] == "on_conversation_update_activity"
170
+ assert bot .record [1 ] == "on_teams_channel_deleted_activity"
171
+
172
+ async def test_on_teams_team_renamed_activity (self ):
173
+ #arrange
174
+ activity = Activity (
175
+ type = ActivityTypes .conversation_update ,
176
+ channel_data = {
177
+ "eventType" : "teamRenamed" ,
178
+ "team" : {
179
+ "id" : "team_id_1" ,
180
+ "name" : "new_team_name"
181
+ }
182
+ },
183
+ channel_id = Channels .ms_teams
184
+ )
185
+
186
+ turn_context = TurnContext (NotImplementedAdapter (), activity )
187
+
188
+ # Act
189
+ bot = TestingTeamsActivityHandler ()
190
+ await bot .on_turn (turn_context )
191
+
192
+ # Assert
193
+ assert len (bot .record ) == 2
194
+ assert bot .record [0 ] == "on_conversation_update_activity"
195
+ assert bot .record [1 ] == "on_teams_team_renamed_activity"
196
+
197
+ async def test_on_teams_members_added_activity (self ):
198
+ #arrange
199
+ activity = Activity (
200
+ type = ActivityTypes .conversation_update ,
201
+ channel_data = {
202
+ "eventType" : "teamMemberAdded"
203
+ },
204
+ members_added = [ChannelAccount (id = "123" , name = "test_user" , aad_object_id = "asdfqwerty" , role = "tester" )],
205
+ channel_id = Channels .ms_teams
206
+ )
207
+
208
+ turn_context = TurnContext (NotImplementedAdapter (), activity )
209
+
210
+ # Act
211
+ bot = TestingTeamsActivityHandler ()
212
+ await bot .on_turn (turn_context )
213
+
214
+ # Assert
215
+ assert len (bot .record ) == 2
216
+ assert bot .record [0 ] == "on_conversation_update_activity"
217
+ assert bot .record [1 ] == "on_teams_members_added_activity"
218
+
219
+ async def test_on_teams_members_removed_activity (self ):
220
+ #arrange
87
221
activity = Activity (
88
- type = ActivityTypes .message_reaction ,
89
- reactions_added = [MessageReaction (type = "sad" )],
222
+ type = ActivityTypes .conversation_update ,
223
+ channel_data = {
224
+ "eventType" : "teamMemberRemoved"
225
+ },
226
+ members_removed = [ChannelAccount (id = "123" , name = "test_user" , aad_object_id = "asdfqwerty" , role = "tester" )],
227
+ channel_id = Channels .ms_teams
90
228
)
229
+
91
230
turn_context = TurnContext (NotImplementedAdapter (), activity )
92
231
93
232
# Act
@@ -96,5 +235,5 @@ async def test_message_reaction(self):
96
235
97
236
# Assert
98
237
assert len (bot .record ) == 2
99
- assert bot .record [0 ] == "on_message_reaction_activity "
100
- assert bot .record [1 ] == "on_reactions_added "
238
+ assert bot .record [0 ] == "on_conversation_update_activity "
239
+ assert bot .record [1 ] == "on_teams_members_removed_activity "
0 commit comments