Search code examples
pythonbotframeworkmicrosoft-teams

MS teams bot - create new conversation


I'm using the botbuilder-python to build MS Teams bot. Following samples I am able to respond to messages. What I'm struggling with is creating completely new message, without existing activity passed from Teams. I modified some code from the tests (https://github.com/Microsoft/botbuilder-python/blob/62b0512a4dd918fa0d3837207012b31213aaedcc/libraries/botframework-connector/tests/test_conversations.py) but I'm getting:

botbuilder.schema.error_response_py3.ErrorResponseException: (BadSyntax) Could not parse tenant id

What is it, where can I find it (I can fish it out from request but it's not ideal) and how do I pass it? Can anyone point me at any Python samples of creating a new conversation?


Solution

  • I figured it out, just in case anybody else is trying to do the same thing and gets stuck:

    to = ChannelAccount(id=to_user_id)
    
    bot_channel = ChannelAccount(id=bot_id)
    activity_reply = Activity(type=ActivityTypes.message, channel_id='msteams',from_property=bot_channel,recipient=to,text=message)
    
    credentials=MicrosoftAppCredentials(app_id, app_password)
    JwtTokenValidation.authenticate_request(activity_reply, "Authorization", credentials)
    # That's where you pass the tenant id
    reply_conversation_params=ConversationParameters(bot=bot_channel, members=[to], activity=activity_reply, channel_data={ 'tenant': { 'id': tenant_id } })
    connector = ConnectorClient(credentials, base_url='https://smba.trafficmanager.net/uk/')
    
    # Create conversation
    conversation = connector.conversations.create_conversation(reply_conversation_params)
    # And send it
    connector.conversations.send_to_conversation(conversation.id, activity_reply)