pythonbotframeworkblobmicrosoft-teams

Bot Framework v4 - Saving conversation references in Blob and reusing them after a new release


I would like to save the conversation references to a blob file or SQL DB so that I can download this file and retrieve the conversation references and then send proactive messages to the users. I know that there is a sample that allows me to save the conversation references in a dictionary, but obviously this dictionary is deleted after a deployment of a new version of the bot and so I can't message the users anymore. So I thought to save this dictionary in a blob file in order to recover it and not to lose the conversation references. But this practice doesn't work.

I do the following to save the dictionary.

a = pickle.dumps(conversation_reference_dict)
blob_client.upload_blob(a, blob_type="BlockBlob", overwrite = True)

But I think this practice saves me a dictionary made like this : [id_client : address of the conversation_reference object] and this is clearly not what I want because after a future deployment this address will no longer mean anything.

Does anyone have any tips for doing this in python? Thank you very much

UPDATE

After testing, the code is correctly saved in the dictionary. However, the problem arises when I try to execute the following code snippet after an update of the bot's source code.

# Send a message to all conversation members.
# This uses the shared Dictionary that the Bot adds conversation references to.
async def _send_proactive_message():
    for conversation_reference in CONVERSATION_REFERENCES_STORED.values():
        await ADAPTER.continue_conversation(
            conversation_reference,
            lambda turn_context: turn_context.send_activity("proactive hello"),
            APP_ID,
        )

The adapter method fails to continue the conversation with users, as if it no longer found them. Is there a way to update the conversation references that are saved in the blob so that after a release the bot can continue the pending conversations?

UPDATE II

I would like to work in this scenario:

The last point doesn't happen, the proactive messages are no longer sent, is there any way I can continue to send these messages? I'm assuming that in the new bot release new id/url are created and these do not match the old id/url saved on the file and therefore calling the method of sending proactive messages via conversation reference will fail or otherwise not be executed. Does anyone know which fields do not match? Can I possibly send a message to the post-release bot and "modify" the entire dictionary on blob so that these urls/ids match? I make an example of what i mean: i know that after a release the id1 is modified, so i after the release for example through the test section inside azure i contact the bot, this contact triggers a method that calls the file saved on azure it scrolls it all and replaces the old id1 with the new id1 so the sending of proactive messages can continue safely. is this a possible scenario? Can you help me?

UPDATE III

I seem to have solved my problem by adding this line of code:

AppCredentials.trust_service_url(conversation_reference.service_url)

before:

await ADAPTER.continue_conversation(
        conversation_reference,
        lambda turn_context: turn_context.send_activity("proactive hello"),
        APP_ID,
    )

resulting in this final code:

# Send a message to all conversation members.
# This uses the shared Dictionary that the Bot adds conversation references to.
async def _send_proactive_message():
    for conversation_reference in CONVERSATION_REFERENCES_STORED.values():
        AppCredentials.trust_service_url(conversation_reference.service_url)
        await ADAPTER.continue_conversation(
            conversation_reference,   
            lambda turn_context: turn_context.send_activity("proactive hello"),
            APP_ID,
        )

Solution

  • I'm not sure why this was removed from the docs, but you always need to trust the service URL in order to send proactive messages in Teams, as explained in this answer: Proactive message not working in MS Teams after bot is restarted

    EDIT: This is apparently no longer true for all languages, so you may not have to do it in Python for much longer