Search code examples
pythonpdfbotframeworkmicrosoft-teamschatbot

Attached PDF to MS Teams chatbot


I am trying to attach a pdf file in a MS Teams bot.

I get the following error " [on_turn_error] unhandled error: (BadArgument) Unknown attachment type". Would anyone know why it might not work?

The following is a portion of my code that concerns the error... unfortunately since it is a chatbot it is not appropriate to put the full code here.

Thank you for your advice.

class MyBot(ActivityHandler):
    async def on_message_activity(self, turn_context: TurnContext):
        
        elif str(turn_context.activity.text).upper() in {'PDF'}:    
            reply = Activity(type=ActivityTypes.message)
            reply.text = "This is the pdf file."
            reply.attachments = [self._get_inline_attachment()]
            await turn_context.send_activity(reply)
         

#truncated#   


        def _get_inline_attachment(self) -> Attachment:
            
            file_path = os.path.join(os.getcwd(), "TEST.pdf") 
            with open(file_path, "rb") as pdf_file: 
                dencoded_string = base64.b64encode(pdf_file.read()).decode() 
            return Attachment( 
                name="TEST.pdf", 
                content_type="application/pdf", 
                content_url=f"data:application/pdf;base64,{dencoded_string}",
            )        

Solution

  • @Nivedipa-MSFT indicated that the sample code for file sharing on MS Teams is avail here: https://github.com/microsoft/BotBuilder-Samples/blob/22fcff680a3e11006eb09b81ac5ed4de345933e2/archive/samples/python/56.teams-file-upload/bots/teams_file_bot.py

    If I may add, "supportsFiles" has to be enabled in the manifest for this to work: https://learn.microsoft.com/en-us/microsoftteams/platform/resources/schema/manifest-schema#bots and https://learn.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bots-files#configure-your-bot-to-support-files