Python: Reply a message with Bot Framework

Adão Duque 20 Reputation points
2024-05-16T21:17:09.3633333+00:00

Hi everyone,

I'm trying to send a message to user (Reply, Chat OneToOne), but i can't find any documentation to this with Python

Scenario:
User "A" -> Types a message in Teams App (bot)

  1. Now Bot has to reply to the same thread that user "A" has started in Teams App (bot)
  2. Now Bot has to reply to the same thread that user "A" has started in Teams App (bot)

Expected:Captura de tela 2024-05-16 175954

Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,965 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nivedipa-MSFT 2,881 Reputation points Microsoft Vendor
    2024-05-17T06:21:14.9033333+00:00

    @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:

    1. 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)
    
    1. Create Conversation Parameters: Define the conversation parameters for the new conversation thread. Set the is_group parameter to False 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,
    )
    
    1. 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.")
    
    1. 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)
    
    1. 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.