12
12
ConversationState ,
13
13
UserState ,
14
14
MessageFactory ,
15
- TurnContext
15
+ TurnContext ,
16
16
)
17
17
from botbuilder .schema import (
18
18
Activity ,
21
21
DeliveryModes ,
22
22
ChannelAccount ,
23
23
OAuthCard ,
24
- TokenExchangeInvokeRequest
24
+ TokenExchangeInvokeRequest ,
25
+ )
26
+ from botframework .connector .token_api .models import (
27
+ TokenExchangeResource ,
28
+ TokenExchangeRequest ,
25
29
)
26
- from botframework .connector .token_api .models import TokenExchangeResource , TokenExchangeRequest
27
30
28
31
from config import DefaultConfig
29
32
from helpers .dialog_helper import DialogHelper
@@ -44,7 +47,7 @@ def __init__(
44
47
self ._user_state = user_state
45
48
self ._dialog = dialog
46
49
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
48
51
self ._connection_name = config .CONNECTION_NAME
49
52
50
53
async def on_turn (self , turn_context : TurnContext ):
@@ -57,7 +60,10 @@ async def on_message_activity(self, turn_context: TurnContext):
57
60
# for signin, just use an oauth prompt to get the exchangeable token
58
61
# also ensure that the channelId is not emulator
59
62
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
+ ):
61
67
await self ._conversation_state .load (turn_context , True )
62
68
await self ._user_state .load (turn_context , Tru
57AE
e )
63
69
await DialogHelper .run_dialog (
@@ -68,31 +74,35 @@ async def on_message_activity(self, turn_context: TurnContext):
68
74
elif turn_context .activity .text == "logout" :
69
75
bot_adapter = turn_context .adapter
70
76
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
+ )
72
80
elif turn_context .activity .text in ("skill login" , "skill logout" ):
73
81
# incoming activity needs to be cloned for buffered replies
74
82
clone_activity = MessageFactory .text (turn_context .activity .text )
75
83
76
84
TurnContext .apply_conversation_reference (
77
85
clone_activity ,
78
86
TurnContext .get_conversation_reference (turn_context .activity ),
79
- True
87
+ True ,
80
88
)
81
89
82
90
clone_activity .delivery_mode = DeliveryModes .expect_replies
83
91
84
- response_1 = await self ._client .post_activity (
92
+ activities = await self ._client .post_buffered_activity (
85
93
self ._from_bot_id ,
86
94
self ._to_bot_id ,
87
- "http://localhost:2303 /api/messages" ,
95
+ "http://localhost:3979 /api/messages" ,
88
96
"http://tempuri.org/whatever" ,
89
97
turn_context .activity .conversation .id ,
90
98
clone_activity ,
91
99
)
92
100
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 )
96
106
97
107
return
98
108
@@ -102,22 +112,20 @@ async def on_message_activity(self, turn_context: TurnContext):
102
112
TurnContext .apply_conversation_reference (
103
113
activity ,
104
114
TurnContext .get_conversation_reference (turn_context .activity ),
105
- True
115
+ True ,
106
116
)
107
117
activity .delivery_mode = DeliveryModes .expect_replies
108
118
109
- response = await self ._client .post_activity (
119
+ activities = await self ._client .post_buffered_activity (
110
120
self ._from_bot_id ,
111
121
self ._to_bot_id ,
112
- "http://localhost:2303 /api/messages" ,
122
+ "http://localhost:3979 /api/messages" ,
113
123
"http://tempuri.org/whatever" ,
114
124
str (uuid4 ()),
115
- activity
125
+ activity ,
116
126
)
117
127
118
- if response .status == int (HTTPStatus .OK ):
119
- await turn_context .send_activities (response .body )
120
-
128
+ await turn_context .send_activities (activities )
121
129
await turn_context .send_activity (MessageFactory .text ("parent: after child" ))
122
130
123
131
async def on_members_added_activity (
@@ -130,36 +138,39 @@ async def on_members_added_activity(
130
138
)
131
139
132
140
async def _intercept_oauth_cards (
133
- self ,
134
- activities : List [Activity ],
135
- turn_context : TurnContext ,
141
+ self , activities : List [Activity ], turn_context : TurnContext ,
136
142
) -> bool :
137
143
if not activities :
138
144
return False
139
145
activity = activities [0 ]
140
146
141
147
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
+ ):
144
152
oauth_card : OAuthCard = OAuthCard ().from_dict (attachment .content )
145
153
oauth_card .token_exchange_resource : TokenExchangeResource = TokenExchangeResource ().from_dict (
146
- oauth_card .token_exchange_resource )
154
+ oauth_card .token_exchange_resource
155
+ )
147
156
if oauth_card .token_exchange_resource :
148
157
token_exchange_provider : BotFrameworkAdapter = turn_context .adapter
149
158
150
159
result = await token_exchange_provider .exchange_token (
151
160
turn_context ,
152
161
self ._connection_name ,
153
162
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
+ ),
155
166
)
156
167
157
168
if result .token :
158
169
return await self ._send_token_exchange_invoke_to_skill (
159
170
turn_context ,
160
171
activity ,
161
172
oauth_card .token_exchange_resource .id ,
162
- result .token
173
+ result .token ,
163
174
)
164
175
return False
165
176
@@ -168,33 +179,32 @@ async def _send_token_exchange_invoke_to_skill(
168
179
turn_context : TurnContext ,
169
180
incoming_activity : Activity ,
170
181
identifier : str ,
171
- token : str
182
+ token : str ,
172
183
) -> bool :
173
184
activity = self ._create_reply (incoming_activity )
174
185
activity .type = ActivityTypes .invoke
175
186
activity .name = "signin/tokenExchange"
176
- activity .value = TokenExchangeInvokeRequest (
177
- id = identifier ,
178
- token = token ,
179
- )
187
+ activity .value = TokenExchangeInvokeRequest (id = identifier , token = token ,)
180
188
181
189
# route the activity to the skill
182
190
response = await self ._client .post_activity (
183
191
self ._from_bot_id ,
184
192
self ._to_bot_id ,
185
- "http://localhost:2303 /api/messages" ,
193
+ "http://localhost:3979 /api/messages" ,
186
194
"http://tempuri.org/whatever" ,
187
195
incoming_activity .conversation .id ,
188
- activity
196
+ activity ,
189
197
)
190
198
191
199
# Check response status: true if success, false if failure
192
200
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
+ )
194
206
195
- await turn_context .send_activity (MessageFactory .text (
196
- message
197
- ))
207
+ await turn_context .send_activity (MessageFactory .text (message ))
198
208
199
209
return is_success
200
210
0 commit comments