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.
Related
I have an Alexa skill and a winform on a windows 10 device. I'm sending a message from the winform to Alexa using the Skill Messaging API. I've got the access token, sent the message and received a 202 status code to say the 'message has been successfully accepted, and will be sent to the skill' so I believe everything on the winform side is okay.
The code for it;
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, policyErrors) => true;
using (var httpClient = new HttpClient(handler))
{
// Obtain skill messaging token
using (var requestToken = new HttpRequestMessage(new HttpMethod("POST"), "https://api.amazon.com/auth/O2/token"))
{
requestToken.Content = new StringContent("grant_type=client_credentials&scope=alexa:skill_messaging&client_id=amzn1.application-oa2-client.************&client_secret=************");
requestToken.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var responseToken = httpClient.SendAsync(requestToken);
Response r = JsonConvert.DeserializeObject<Response>(responseToken.Result.Content.ReadAsStringAsync().Result);
// Send message
using (var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "https://api.eu.amazonalexa.com/v1/skillmessages/users/" + strUserId))
{
requestMessage.Headers.TryAddWithoutValidation("Authorization", "Bearer " + r.access_token);
requestMessage.Content = new StringContent("{ \"data\" : { \"message\" : \"Hi pickle\" }}");
requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var responseMessage = httpClient.SendAsync(requestMessage);
MessageBox.Show(responseMessage.Result.ToString());
}
}
}
How do I capture the incoming message event on the skill though?
Looking at the docs I need to handle an incoming request of type Messaging.MessageReceived? Is that correct?
I tried something like that in the skills FunctionHandler but didn't have any luck.
public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
// Initialise response
skillResponse = new SkillResponse
{
Version = "1.0",
Response = new ResponseBody()
};
ssmlResponse = new SsmlOutputSpeech();
if (input.GetRequestType() == typeof(LaunchRequest))
{
LaunchRequestHandler(input, context);
}
else if (input.GetRequestType() == typeof(IntentRequest))
{
IntentRequestHandler(input, context);
}
else if (input.GetRequestType() == typeof(SessionEndedRequest))
{
SessionEndedRequestHandler(input, context);
}
else if(input.GetRequestType().Equals("Messaging.MessageReceived"))
{
ssmlResponse.Ssml = "<speak>" + input.Request.Type + "</speak>";
}
skillResponse.Response.OutputSpeech = ssmlResponse;
return skillResponse;
}
How do I react to the message? Is it permissions I need to set up? Does the incoming message not trigger the functionhandler the same way the echo device does?
Thanks.
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'm using the following API to upload a file to watson discovery services. I'm getting the response as unsupported file format, even though i have provided a valid type.
API:discovery service api
public async Task<ActionResult> Index()
{
using (var httpClient = new HttpClient())
{
//ADD BASIC AUTH
var authByteArray = Encoding.ASCII.GetBytes("{auth key}");
var authString = Convert.ToBase64String(authByteArray);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authString);
var text = string.Empty;
var uri = "https://gateway.watsonplatform.net/discovery/api/v1/environments/{envid}/collections/{collectionid}/documents?version=2017-11-07";
var content = new MultipartFormDataContent();
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Views/UploadDocument/civilwar-api1.html"));
var file = new StreamContent(new MemoryStream(bytes));
content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/html");
content.Add(new StreamContent(new MemoryStream(bytes)), "file");
var response = await httpClient.PostAsync(uri, content);
var text1 = await response.Content.ReadAsStringAsync();
}
return View();
}
api response as :{
"code" : 415,
"error" : "Unsupported Media Type"
}
As you can see in Watson Developer Cloud, you can use the .NET SDK.
In this repository, you can see examples for using each service from Watson.
The method to add a document:
#region Documents
private void AddDocument()
{
Console.WriteLine(string.Format("\nCalling AddDocument()..."));
using (FileStream fs = File.OpenRead(_filepathToIngest))
{
var result = _discovery.AddDocument(_createdEnvironmentId, _createdCollectionId, _createdConfigurationId, fs as Stream, _metadata);
if (result != null)
{
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
_createdDocumentId = result.DocumentId;
}
else
{
Console.WriteLine("result is null.");
}
}
}
See more about Watson Discovery.
try httpClient.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
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
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)
{...}