excel - How to read attachment content from bot framework C#? -
i writing bot , expecting user send me attachment, want read , translate objects.
i have following code far:
if (message.attachments != null && message.attachments.any()) { var attachment = message.attachments.first(); using (httpclient httpclient = new httpclient()) { if ((message.channelid.equals("skype", stringcomparison.invariantcultureignorecase) || message.channelid.equals("msteams", stringcomparison.invariantcultureignorecase)) && new uri(attachment.contenturl).host.endswith("skype.com")) { var token = await new microsoftappcredentials().gettokenasync(); httpclient.defaultrequestheaders.authorization = new authenticationheadervalue("bearer", token); } var responsemessage = await httpclient.getasync(attachment.contenturl); var contentlenghtbytes = responsemessage.content.headers.contentlength; // populated correctly if(attachment.name.tolower().equals("opportunity.xlsx")) { var temp = attachment.content; // content null, though else populated. } } }
anyone can suggest how can read attachment xlsx content please?
thanks
the attachment not available in content
property. first need download attachment using contenturl
, perform whatever want, using response message after downloading file.
take @ receive-attachments c# sample.
public virtual async task messagereceivedasync(idialogcontext context, iawaitable<imessageactivity> argument) { var message = await argument; if (message.attachments != null && message.attachments.any()) { var attachment = message.attachments.first(); using (httpclient httpclient = new httpclient()) { // skype & ms teams attachment urls secured jwttoken, need pass token our bot. if ((message.channelid.equals("skype", stringcomparison.invariantcultureignorecase) || message.channelid.equals("msteams", stringcomparison.invariantcultureignorecase)) && new uri(attachment.contenturl).host.endswith("skype.com")) { var token = await new microsoftappcredentials().gettokenasync(); httpclient.defaultrequestheaders.authorization = new authenticationheadervalue("bearer", token); } var responsemessage = await httpclient.getasync(attachment.contenturl); var contentlenghtbytes = responsemessage.content.headers.contentlength; await context.postasync($"attachment of {attachment.contenttype} type , size of {contentlenghtbytes} bytes received."); } } else { await context.postasync("hi there! i'm bot created show how can receive message attachments, no attachment sent me. please, try again sending new message including attachment."); } context.wait(this.messagereceivedasync); }
Comments
Post a Comment