'How can you ensure botframework's (MSTeams) messages are sent successfully?
I'm sending messages using turn_context.send_message as follows:
# Generic bot handler with two functions:
# 1. Send Message (in theory, the problem lies here) tries to send a message, returns False if it doesn't
# finish within two seconds, and returns True if it's successful.
# 2. Persistent Message remains in a loop so long as Send Message is failing, until it eventually succeeds.
class MyBot(ActivityHandler):
@staticmethod
# The aim - if the message isn't sent in two seconds, kill this attempt and start over.
async def send_message(message, turn_context):
# This line sometimes hangs the ENTIRE thread and prevents all the following code from being read.
task = asyncio.create_task(turn_context.send_activity(message))
# This sleep is never reached.
await asyncio.sleep(2)
if task.done():
return True
task.cancel()
return False
async def persistent_message(self, message, turn_context):
x = await self.send_message(message, turn_context)
while not x:
x = await self.send_message(message, turn_context)
return x
async def on_message_activity(self, turn_context: TurnContext):
# This calls persistent_message, aiming to send "pong" to the user, using the context.
self.persistent_message("pong!", turn_context)
Issue:
When using wait_for, wait, create_task/task.cancel, etc, all of these functions fail to kill the task so that the send_message coro can end successfully.
For wait_for and wait, I'm assuming this behavior is because these methods must wait for the coro to get cancelled, but doesn't task.cancel immediately kill a running task?
Questions:
- Is there an easier way to ensure the messages that the bot is supposed to send are actually sent?
- Why does this code block behave this way, and how can it be circumvented to ensure that the messages are sent?
P.S: The code sends no exceptions - it simply doesn't send the messages. wait and wait_for did not work either.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
