My bot prompts the user for an attachment in a dialog and is suppose to receive an image from the user and save the content name, url and type of that file to variables. However, the file I am receiving is not the file the user sent. Instead its retrieving a file named 'blob' (no extension) from this link:
https://webchat.botframework.com/attachments/GkLhiqJcvH019mP0iGBKvo/0000052/0/blob?t=X5ICiUrhFas.dAA.RwBrAEwAaABpAHEASgBjAHYASAAwADEAOQBtAFAAMABpAEcAQgBLAHYAbwAtADAAMAAwADAAMAA1ADIA.mcEiHxuH0gE.VMcp6Yduqgc.4xT1ZTOvCX-B7A0nLto6eZNFrFi-0xSzGk5AKmA-EPE
That file contains:
{"type":"message","from":{"id":"9n0I1ZqSrLF","name":"You"},"locale":"en-US","timestamp":"2017-02-14T21:12:32.074Z","channelData":{"clientActivityId":"1487106689866.9060198022610071.8"}}
Here is the code for the attachment prompt. This prompt is within a dialog:
private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{
IEnumerable<Attachment> list = await result;
Attachment attachment = list.FirstOrDefault();
string filename = attachment.Name;
string url = attachment.ContentUrl;
string contentType = attachment.ContentType;
//Set attachmentFileNames
if (attachmentFileNames == null)
{
attachmentFileNames = "";
}
attachmentFileNames += contentType;
attachmentFileNames += ",";
numberOfFiles++;
//Set attachmentFileURLs
if (attachmentFileURLs == null)
{
attachmentFileURLs = "";
}
attachmentFileURLs += url;
attachmentFileURLs += ",";
attachmentHasBeenAdded = true;
await FirstMessageReceivedAsync(context, result);
}
Here is how I am handling attachments in the message controller. This is within the Post Task:
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
if (activity.Attachments != null && activity.Attachments.Any())
{
hasaAttachment = true;
var attachment = activity.Attachments.First();
using (HttpClient httpClient = new HttpClient())
{
// Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if ((activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || activity.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;
Activity reply = activity.CreateReply($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received. URL: {attachment.ContentUrl}");
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
I am following this example:
Receive Attachment Bot Sample
In the emulator it works correctly but the published version on azure does not. The code is very similar to the example so I don't see why the bot is not detecting the user's files.
How do I receive the correct file (the file the user replied with)?
The attachments will be in elements 1 onwards. E.g.
Attachment attachment = list.ElementAt(1);
The webchat (and maybe other platforms) has a weird quirk where it adds a blob file at the start of IEnumerable 'result' when prompting and getting attachments from the user.
private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{...}
Related
I'm using updating out Exchange Mail Client written in .NET 4 Framework using Exchange Web Services (EWS) to .NET Core 6 using Microsoft.Graph. I've managed to port most of the functions but I'm having trouble working out how to Forward a Message as an Attachment.
In EWS
// This method results in a GetItem call to EWS.
var msgToAttach = EWS.EmailMessage.Bind(
ServiceInstance,
message.Source.Id,
new EWS.PropertySet(EWS.ItemSchema.MimeContent, EWS.ItemSchema.Subject));
// Create an email message and set properties on the message.
var forward = new EWS.EmailMessage(ServiceInstance);
if (!bodyType.HasValue)
bodyType = message.BodyType;
forward.Subject = subject == null ? $"FW: {message.Subject}" : subject;
forward.Body = new EWS.MessageBody(
bodyType.Value == BodyTypes.Html ? EWS.BodyType.HTML : EWS.BodyType.Text,
body ?? "Please refer to the attachment message");
// Add additional recipients to the reply email message.
if (to?.Any() ?? false)
forward.ToRecipients.AddRange(to);
if (cc?.Any() ?? false)
forward.CcRecipients.AddRange(cc);
if (bcc?.Any() ?? false)
forward.BccRecipients.AddRange(bcc);
// before we can add the attachments, we need to save the draft
// according to the docoumentation this isn't required but without
// it throws an exception on Save/SendAndSaveCopy if the attachments
// are added to a message which doesn't exist
forward.Save();
// Add an email message item attachment and set properties on the item.
EWS.ItemAttachment<EWS.EmailMessage> itemAttachment = forward.Attachments.AddItemAttachment<EWS.EmailMessage>();
itemAttachment.Item.MimeContent = msgToAttach.MimeContent;
itemAttachment.Name = msgToAttach.Subject;
// Send the mail and save a copy in the Sent Items folder.
// This method results in a CreateItem and SendItem call to EWS.
forward.Update(EWS.ConflictResolutionMode.AlwaysOverwrite);
forward.Send();
UPDATE: Solution
You can read the mime content using
_serviceInstance.Users[UserId].Messages[message.Id].Content.GetAsync()
which returns a stream that can be saved as .eml file. The attachment name must end with .eml or it doesn't work.
public void ForwardMessageAsAttachment(
Message message,
IEnumerable<string> to,
string subject = null,
string body = null,
BodyType? bodyType = null)
{
// Download the mime content for the specific message
var mimeContentStream = _serviceInstance
.Users[UserId]
.Messages[message.Id]
.Content
.Request()
.GetAsync()
.GetAwaiter()
.GetResult();
// the mine content is returned as a Stream, so write it to buffer
var buffer = new Span<Byte>(new byte[mimeContentStream.Length]);
var mimeContent = mimeContentStream.Read(buffer);
// The attachment Name must ends with .eml else Outlook wont recognize its an email
// even with the contentType = message/rfc822
var messageAsAttachment = new FileAttachment
{
ContentBytes = buffer.ToArray(),
ContentType = "message/rfc822",
Name = $"{message.Subject}.eml"
};
// create a new message
var forward = new Message()
{
Attachments = new MessageAttachmentsCollectionPage(),
Subject = String.IsNullOrWhiteSpace(subject) ? subject : $"FW: {message.Subject}".Trim(),
Body = new ItemBody
{
ContentType = bodyType,
Content = body
},
ToRecipients = to.Select(x => new Recipient { EmailAddress = new EmailAddress { Address = x } })
};
// add the EML attachment
forward.Attachments.Add(messageAsAttachment);
_serviceInstance
.Users[UserId]
.SendMail(forward)
.Request()
.PostAsync()
.GetAwaiter()
.GetResult();
}
In the Graph there is no equivalent to
EWS.ItemAttachment<EWS.EmailMessage> itemAttachment =
forward.Attachments.AddItemAttachment<EWS.EmailMessage>();
itemAttachment.Item.MimeContent = msgToAttach.MimeContent;
So your options are to attach the message as an EML file as a File Attachment
or you can build the MIME message outside with a MIME parser like mailkit and then send the message as MIME https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/concepts/outlook-send-mime-message.md you will be limited on size with this method you can have anything larger then 4MB and with mimebloat that usually means around 2-3 MB
Mime content from a message can be read using MS Graph and converted to .EML, then sent FileAttachment on a new message. You need to make sure the attachment's name ends with .eml or outlook will not recognize attachment as a message. Adding content type of message/rfc822 does not appear to affect how the attachment is read by gmail, hotmail or Outlook desktop.
public void ForwardMessageAsAttachment(
Message message,
IEnumerable<string> to,
string subject = null,
string body = null,
BodyType? bodyType = null)
{
// Download the mime content for the specific message
// _serviceInstance is an instance of GraphServiceClient
var mimeContentStream = _serviceInstance
.Users[UserId]
.Messages[message.Id]
.Content
.Request()
.GetAsync()
.GetAwaiter()
.GetResult();
// the mine content is returned as a Stream, so write it to buffer
var buffer = new Span<Byte>(new byte[mimeContentStream.Length]);
var mimeContent = mimeContentStream.Read(buffer);
// The attachment Name must ends with .eml else Outlook wont recognize its an email
// even with the contentType = message/rfc822
var messageAsAttachment = new FileAttachment
{
ContentBytes = buffer.ToArray(),
ContentType = "message/rfc822",
Name = $"{message.Subject}.eml"
};
// create a new message
var forward = new Message()
{
Attachments = new MessageAttachmentsCollectionPage(),
Subject = String.IsNullOrWhiteSpace(subject) ? subject : $"FW: {message.Subject}".Trim(),
Body = new ItemBody
{
ContentType = bodyType,
Content = body
},
ToRecipients = to.Select(x => new Recipient { EmailAddress = new EmailAddress { Address = x } })
};
// add the EML attachment
forward.Attachments.Add(messageAsAttachment);
_serviceInstance
.Users[UserId]
.SendMail(forward)
.Request()
.PostAsync()
.GetAwaiter()
.GetResult();
}
I'm trying to retrieve and read emails from my Outlook mail. Unfortunately my mailbox uses Authenticity, which I need to deal with. I have tried a mailbox that does not use Authenticity and the code works. I followed the instructions here https://www.emailarchitect.net/eagetmail/ex/c/22.aspx
(I used the library to read a mailbox that does not use OAuth). So I registered my application on Microsoft Azure as instructed (except for authentication, which was the last step). Unfortunately I get this error System.ComponentModel.Win32Exception
HResult=0x80004005 Message=System cannot find the specified file.
Source=System.Diagnostics.Process
I also tried another library
https://afterlogic.com/mailbee-net/docs/OAuth2MicrosoftRegularAccountsInstalledApps.html
But with the same result
It is larger project, so I will post method where I am getting the error. I will paste more code, if you will need it.
Feel free to ask.
Thanks for any advice.(The documentation is great, so I didn't want to change it)
const string clientID = "Client ID";
const string clientSecret = "client Secret";
const string scope = "https://outlook.office.com/IMAP.AccessAsUser.All%20https://outlook.office.com/POP.AccessAsUser.All%20offline_access%20email%20openid";
const string authUri = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
const string tokenUri = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
async void DoOauthAndRetrieveEmail()
{
// Creates a redirect URI using an available port on the loopback address.
string redirectUri = string.Format("http://127.0.0.1:{0}/", GetRandomUnusedPort());
Console.WriteLine("redirect URI: " + redirectUri);
// Creates an HttpListener to listen for requests on that redirect URI.
var http = new HttpListener();
http.Prefixes.Add(redirectUri);
Console.WriteLine("Listening ...");
http.Start();
// Creates the OAuth 2.0 authorization request.
string authorizationRequest = string.Format("{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}&prompt=login",
authUri,
scope,
Uri.EscapeDataString(redirectUri),
clientID
);
// Opens request in the browser.
//There is issue
System.Diagnostics.Process.Start(authorizationRequest);
// Waits for the OAuth authorization response.
var context = await http.GetContextAsync();
// Brings the Console to Focus.
BringConsoleToFront();
// Sends an HTTP response to the browser.
var response = context.Response;
string responseString = string.Format("<html><head></head><body>Please return to the app and close current window.</body></html>");
var buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
var responseOutput = response.OutputStream;
Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
{
responseOutput.Close();
http.Stop();
Console.WriteLine("HTTP server stopped.");
});
// Checks for errors.
if (context.Request.QueryString.Get("error") != null)
{
Console.WriteLine(string.Format("OAuth authorization error: {0}.", context.Request.QueryString.Get("error")));
return;
}
if (context.Request.QueryString.Get("code") == null)
{
Console.WriteLine("Malformed authorization response. " + context.Request.QueryString);
return;
}
// extracts the code
var code = context.Request.QueryString.Get("code");
Console.WriteLine("Authorization code: " + code);
string responseText = await RequestAccessToken(code, redirectUri);
Console.WriteLine(responseText);
OAuthResponseParser parser = new OAuthResponseParser();
parser.Load(responseText);
var user = parser.EmailInIdToken;
var accessToken = parser.AccessToken;
Console.WriteLine("User: {0}", user);
Console.WriteLine("AccessToken: {0}", accessToken);
RetrieveMailWithXOAUTH2(user, accessToken);
}
public virtual async Task MotgaAtt(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())
{
var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
}
}
context.Done<object>(new object());
}
I want to get the uploaded attachment by the bot user so i can insert it in the database. I tried to use the attachment variable i created but it seems not to be the exact file the user uploaded. Please how do i get the uploaded file?. Thanks
See official samples: https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-ReceiveAttachment
Depending on the channel you must give a specific token:
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 are secured by a JwtToken, so we need to pass the token from 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 and size of {contentLenghtBytes} bytes received.");
}
}
else
{
await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
}
context.Wait(this.MessageReceivedAsync);
}
I am writing a bot and expecting the user to send me an attachment, which I want to read and translate into objects.
I have the following code so 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; // this is populated correctly
if(attachment.Name.ToLower().Equals("opportunity.xlsx"))
{
var temp = attachment.Content; // This Content is always null, even though everything else is populated.
}
}
}
Anyone can suggest how can I read the attachment xlsx content please?
Thanks
The attachment is not available in the Content property. You first need to download the attachment using the ContentUrl and then perform whatever you want, using the response message after downloading the file.
Take a look at the 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 are secured by a JwtToken, so we need to pass the token from 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 and size of {contentLenghtBytes} bytes received.");
}
}
else
{
await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
}
context.Wait(this.MessageReceivedAsync);
}
You will have to use some other dll to read the excel data, if that is what you mean. Do u mean to read the contents of the excel file after it has been uploded then you could use https://github.com/ExcelDataReader/ExcelDataReader
I have a bot develop with Microsoft Bot Framework, and in Debug run correctly
After the install on Skype, after the upload the image I have a link like this
https://df-apis.skype.com/v2/attachments/0-eus-d4-7e19a097c62f5fc21dd53eabfa19d85e/views/original
The code is very simply and run without skype
if ((activity.Attachments != null) && (activity.Attachments.Count > 0))
{
analysisResult = await AnalyzeUrl(activity.Attachments[0].ContentUrl);
}
........
How do I find the picture that I sent?
According to this comment, to fetch an attachment, the GET request should contain JwtToken of the bot as authorization header:
var attachment = activity.Attachments?.FirstOrDefault();
if (attachment?.ContentUrl != null)
{
using (var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl)))
{
var token = await (connectorClient.Credentials as MicrosoftAppCredentials).GetTokenAsync();
var uri = new Uri(attachment.ContentUrl);
using (var httpClient = new HttpClient())
{
if (uri.Host.EndsWith("skype.com") && uri.Scheme == Uri.UriSchemeHttps)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
}
else
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(attachment.ContentType));
}
var attachmentData = await httpClient.GetByteArrayAsync(uri);
analysisResult = await AnalyzeUrl(attachmentData);
}
}
}
That means you have to change the AnalyzeUrl to accept the image data instead of URL. I believe CognitiveServices, you are using, are able to accept the image data.