@Adão Duque - You need to use the proactive messaging feature to send a message to the user who initiated the conversation.
Here is a step-by-step guide on how to achieve this:
- Get Conversation Reference: First, you need to get the conversation reference of the user who initiated the chat. This reference will help you create a new conversation thread to reply to the user.
conversation_reference = TurnContext.get_conversation_reference(turn_context.activity)
- Create Conversation Parameters: Define the conversation parameters for the new conversation thread. Set the
is_group
parameter toFalse
to indicate a one-to-one chat.
conversation_parameters = ConversationParameters(
is_group=False,
bot=turn_context.activity.recipient,
members=[member],
tenant_id=turn_context.activity.conversation.tenant_id,
)
- Send Proactive Message: Implement a function to send a proactive message to the user. This function will be used to reply to the user in the new conversation thread.
async def send_message(tc2: TurnContext):
return await tc2.send_activity(f"Hello {member.name}. I'm a Teams conversation bot.")
- Continue Conversation: Use the
adapter.continue_conversation
method to continue the conversation with the user and send the message.
async def get_ref(tc1):
conversation_reference_inner = TurnContext.get_conversation_reference(tc1.activity)
return await tc1.adapter.continue_conversation(conversation_reference_inner, send_message, self._app_id)
- Create New Conversation Thread: Finally, use the
adapter.create_conversation
method to create a new conversation thread and reply to the user.
await turn_context.adapter.create_conversation(conversation_reference, get_ref, conversation_parameters)
By following these steps, you can send a message to a user in a one-to-one chat thread in Teams using Python. This approach allows your bot to reply to the same thread that the user has started in the Teams app.
Ref Doc: official Microsoft Teams platform documentation on sending proactive messages.
Thanks,
Nivedipa
*************************************************************************
If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedbacklink. Click here to escalate.