-
Notifications
You must be signed in to change notification settings - Fork 298
Description
Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.
Version
4.16.2
Describe the bug
The turncontext middlware chain is as follows:
botbuilder-python/libraries/botbuilder-core/botbuilder/core/turn_context.py
Lines 287 to 304 in a7f5d91
handlers = copy(plugins) | |
async def emit_next(i: int): | |
context = self | |
try: | |
if i < len(handlers): | |
async def next_handler(): | |
await emit_next(i + 1) | |
await handlers[i](context, arg, next_handler) | |
except Exception as error: | |
raise error | |
await emit_next(0) | |
# logic does not use parentheses because it's a coroutine | |
return await logic |
Notice how the "logic" runs after all the middlware has run? This prevents any middlware from awaiting the result of the "next" middleware. This means that if you wanted to wait for the "response" from the last middleware (which runs the actual logic to send the activity), you can't.
This is in contrast with how the middleware for js is built:
https://github.com/microsoft/botbuilder-js/blob/3b8fcab21a0a5434706da8eace17117722ffd78b/libraries/botbuilder-core/src/turnContext.ts#L864.
Here, if you wanted to do:
async function MyMiddleware(context, args, next) {
console.log(args)
const res = await next()
console.log(res)
}
you can. But in python, the same thing would not be possible because the handlers don't wait for the last handler (which is the actual logic) to be run in the same chain as all the handlers.