Search code examples
pythonbotframework

how to use the ShowTypingMiddleware in a python bot


I would like to use the ShowTypingMiddleware middleware in a python bot , but i cant find how to hook in to that middleware correctly.
botbuilder-core 4.7.1
botbuilder-schema 4.7.1

#app.py 
ADAPTER = BotFrameworkAdapter(SETTINGS)
# show typing indicator on long activities
ADAPTER.use(ShowTypingMiddleware(delay=0.5, period=2.0))
#bot.py 
...

    async def on_message_activity(self, turn_context: TurnContext):
        if turn_context.activity.text == "middleware":
            await asyncio.sleep(10) # mock getting some data 
            await turn_context.send_activity("done")

...

I expect that calling the middleware - shows a TI for activities taking longer than .5 seconds - repeat sending a TI to the client every 2 seconds

actual results :

  • TI is sent one time only
  • no repeat TI are sent
  • a runtime warning is shown:
 c:\develop\x\pybot1\.venv\lib\site-packages\botbuilder\core\show_typing_middleware.py:79: 
RuntimeWarning: coroutine 'ShowTypingMiddleware.on_turn.<locals>.start_interval' was never awaited
  start_interval(context, period, period)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

In the emulator log it is clear that only one TI indicator is sent , and no repeats are to be seen

[16:55:12]<- messageYou said 'middleware'
[16:55:12]POST200conversations.:conversationId.activities.:activityId
[16:55:12]POST201directline.conversations.:conversationId.activities
[16:55:43]-> messagemiddleware
[16:55:44]<- typing
[16:55:44]POST200conversations.:conversationId.activities.:activityId
[16:55:54]<- messagedone
[16:55:54]POST200conversations.:conversationId.activities.:activityId
[16:55:54]POST201directline.conversations.:conversationId.activities

Solution

  • Since I can reproduce this problem in Python and not .NET, it looks like you found a bug in the Python SDK. Please report it here and make sure to link to this post.

    Also note that while the Emulator is fine for testing, typing indicators are a channel-specific feature and you should not expect all channels to treat typing indicators the same way. Even if you get typing indicators to work in the Emulator the way you desire, the channel you're targeting may behave differently.