1212 ConversationState ,
1313 UserState ,
1414 MessageFactory ,
15- TurnContext
15+ TurnContext ,
1616)
1717from botbuilder .schema import (
1818 Activity ,
2121 DeliveryModes ,
2222 ChannelAccount ,
2323 OAuthCard ,
24- TokenExchangeInvokeRequest
24+ TokenExchangeInvokeRequest ,
25+ )
26+ from botframework .connector .token_api .models import (
27+ TokenExchangeResource ,
28+ TokenExchangeRequest ,
2529)
26- from botframework .connector .token_api .models import TokenExchangeResource , TokenExchangeRequest
2730
2831from config import DefaultConfig
2932from helpers .dialog_helper import DialogHelper
@@ -44,7 +47,7 @@ def __init__(
4447 self ._user_state = user_state
4548 self ._dialog = dialog
4649 self ._from_bot_id = config .APP_ID
47- self ._to_bot_id = config .SKILL_APP_ID
50+ self ._to_bot_id = config .SKILL_MICROSOFT_APP_ID
4851 self ._connection_name = config .CONNECTION_NAME
4952
5053 async def on_turn (self , turn_context : TurnContext ):
@@ -57,7 +60,10 @@ async def on_message_activity(self, turn_context: TurnContext):
5760 # for signin, just use an oauth prompt to get the exchangeable token
5861 # also ensure that the channelId is not emulator
5962 if turn_context .activity .type != "emulator" :
60- if turn_context .activity .text == "login" or turn_context .activity .text .isdigit ():
63+ if (
64+ turn_context .activity .text == "login"
65+ or turn_context .activity .text .isdigit ()
66+ ):
6167 await self ._conversation_state .load (turn_context , True )
6268 await self ._user_state .load (turn_context , True )
6369 await DialogHelper .run_dialog (
@@ -68,31 +74,35 @@ async def on_message_activity(self, turn_context: TurnContext):
6874 elif turn_context .activity .text == "logout" :
6975 bot_adapter = turn_context .adapter
7076 await bot_adapter .sign_out_user (turn_context , self ._connection_name )
71- await turn_context .send_activity (MessageFactory .text ("You have been signed out." ))
77+ await turn_context .send_activity (
78+ MessageFactory .text ("You have been signed out." )
79+ )
7280 elif turn_context .activity .text in ("skill login" , "skill logout" ):
7381 # incoming activity needs to be cloned for buffered replies
7482 clone_activity = MessageFactory .text (turn_context .activity .text )
7583
7684 TurnContext .apply_conversation_reference (
7785 clone_activity ,
7886 TurnContext .get_conversation_reference (turn_context .activity ),
79- True
87+ True ,
8088 )
8189
8290 clone_activity .delivery_mode = DeliveryModes .expect_replies
8391
84- response_1 = await self ._client .post_activity (
92+ activities = await self ._client .post_buffered_activity (
8593 self ._from_bot_id ,
8694 self ._to_bot_id ,
87- "http://localhost:2303 /api/messages" ,
95+ "http://localhost:3979 /api/messages" ,
8896 "http://tempuri.org/whatever" ,
8997 turn_context .activity .conversation .id ,
9098 clone_activity ,
9199 )
92100
93- if response_1 .status == int (HTTPStatus .OK ):
94- if not await self ._intercept_oauth_cards (response_1 .body , turn_context ):
95- await turn_context .send_activities (response_1 .body )
101+ if activities :
102+ if not await self ._intercept_oauth_cards (
103+ activities , turn_context
104+ ):
105+ await turn_context .send_activities (activities )
96106
97107 return
98108
@@ -102,22 +112,20 @@ async def on_message_activity(self, turn_context: TurnContext):
102112 TurnContext .apply_conversation_reference (
103113 activity ,
104114 TurnContext .get_conversation_reference (turn_context .activity ),
105- True
115+ True ,
106116 )
107117 activity .delivery_mode = DeliveryModes .expect_replies
108118
109- response = await self ._client .post_activity (
119+ activities = await self ._client .post_buffered_activity (
110120 self ._from_bot_id ,
111121 self ._to_bot_id ,
112- "http://localhost:2303 /api/messages" ,
122+ "http://localhost:3979 /api/messages" ,
113123 "http://tempuri.org/whatever" ,
114124 str (uuid4 ()),
115- activity
125+ activity ,
116126 )
117127
118- if response .status == int (HTTPStatus .OK ):
119- await turn_context .send_activities (response .body )
120-
128+ await turn_context .send_activities (activities )
121129 await turn_context .send_activity (MessageFactory .text ("parent: after child" ))
122130
123131 async def on_members_added_activity (
@@ -130,36 +138,39 @@ async def on_members_added_activity(
130138 )
131139
132140 async def _intercept_oauth_cards (
133- self ,
134- activities : List [Activity ],
135- turn_context : TurnContext ,
141+ self , activities : List [Activity ], turn_context : TurnContext ,
136142 ) -> bool :
137143 if not activities :
138144 return False
139145 activity = activities [0 ]
140146
141147 if activity .attachments :
142- for attachment in filter (lambda att : att .content_type == CardFactory .content_types .oauth_card ,
143- activity .attachments ):
148+ for attachment in filter (
149+ lambda att : att .content_type == CardFactory .content_types .oauth_card ,
150+ activity .attachments ,
151+ ):
144152 oauth_card : OAuthCard = OAuthCard ().from_dict (attachment .content )
145153 oauth_card .token_exchange_resource : TokenExchangeResource = TokenExchangeResource ().from_dict (
146- oauth_card .token_exchange_resource )
154+ oauth_card .token_exchange_resource
155+ )
147156 if oauth_card .token_exchange_resource :
148157 token_exchange_provider : BotFrameworkAdapter = turn_context .adapter
149158
150159 result = await token_exchange_provider .exchange_token (
151160 turn_context ,
152161 self ._connection_name ,
153162 turn_context .activity .from_property .id ,
154- TokenExchangeRequest (uri = oauth_card .token_exchange_resource .uri )
163+ TokenExchangeRequest (
164+ uri = oauth_card .token_exchange_resource .uri
165+ ),
155166 )
156167
157168 if result .token :
158169 return await self ._send_token_exchange_invoke_to_skill (
159170 turn_context ,
160171 activity ,
161172 oauth_card .token_exchange_resource .id ,
162- result .token
173+ result .token ,
163174 )
164175 return False
165176
@@ -168,33 +179,32 @@ async def _send_token_exchange_invoke_to_skill(
168179 turn_context : TurnContext ,
169180 incoming_activity : Activity ,
170181 identifier : str ,
171- token : str
182+ token : str ,
172183 ) -> bool :
173184 activity = self ._create_reply (incoming_activity )
174185 activity .type = ActivityTypes .invoke
175186 activity .name = "signin/tokenExchange"
176- activity .value = TokenExchangeInvokeRequest (
177- id = identifier ,
178- token = token ,
179- )
187+ activity .value = TokenExchangeInvokeRequest (id = identifier , token = token ,)
180188
181189 # route the activity to the skill
182190 response = await self ._client .post_activity (
183191 self ._from_bot_id ,
184192 self ._to_bot_id ,
185- "http://localhost:2303 /api/messages" ,
193+ "http://localhost:3979 /api/messages" ,
186194 "http://tempuri.org/whatever" ,
187195 incoming_activity .conversation .id ,
188- activity
196+ activity ,
189197 )
190198
191199 # Check response status: true if success, false if failure
192200 is_success = int (HTTPStatus .OK ) <= response .status <= 299
193- message = "Skill token exchange successful" if is_success else "Skill token exchange failed"
201+ message = (
202+ "Skill token exchange successful"
203+ if is_success
204+ else "Skill token exchange failed"
205+ )
194206
195- await turn_context .send_activity (MessageFactory .text (
196- message
197- ))
207+ await turn_context .send_activity (MessageFactory .text (message ))
198208
199209 return is_success
200210
0 commit comments