I use windows7 Professional and other Bot run without problem.
I want to return an image to my BOT, but the code suggested by Microsoft guide doesn't run
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity reply = activity.CreateReply( $" Hello");
reply.Attachments.Add(new Attachment()
{
ContentUrl = $"https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png",
ContentType = "image/png",
Name = "Bender_Rodriguez.png"
});
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
I tried this code with Bot Emulator and got the following error:
You need to initialize the Attachments array. In your code, it's null so the Add call is causing a NULL ref exception.
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity reply = activity.CreateReply($" Hello");
reply.Attachments = new List<Attachment>(); //****** INIT
reply.Attachments.Add(new Attachment()
{
ContentUrl = $"https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png",
ContentType = "image/png",
Name = "Bender_Rodriguez.png"
});
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
You have to add these lines of code after reply activity initialization.
reply.Attachments = new List<Attachment>();
reply.Recipient = activity.From;
reply.Type = "message";
You can refer this link for more details
downloading file (pdf/image) from using Microsoft bot framework
Related
I have a controller action method when triggered post data to the ForgotPassword method:
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking here");
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
// If we got this far, something failed, redisplay form
return View(model);
}
web.config file
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="SendGridUsername" value="XXXXXXXXXXXXXXXXXXXXXX" />
<add key="SendGridPassword" value="XXXXXXXXXXXXXX" />
<add key="SendGridApiKey" value="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</appSettings>
My IdentityConfig file looks like this:
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}
// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
var myMessage = new SendGridMessage();
myMessage.AddTo(message.Destination);
myMessage.From = new System.Net.Mail.MailAddress(
"Joe#contoso.com", "Joe S.");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["SendGridUsername"],
ConfigurationManager.AppSettings["SendGridPassword"]
);
// Create a Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
if (transportWeb != null)
{
await transportWeb.DeliverAsync(myMessage);
}
else
{
Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);
}
}
}
However no email is sent , what am I doing wrong and how can I fix this as I have followed the instructions from Rick Andersons post on msdn.
You are using an outdated version of sendgrid C# SDK which points to V2 api. The current API version is V3. The document which you are following is not updated . I am currently in progress to update the document. You need to add the below namespace in your service -
using SendGrid;
using SendGrid.Helpers.Mail; //optional if you want to use the MailHelper class
And then use below code in your configSendGridasync method -
private async Task configSendGridasync(IdentityMessage message)
{
var apiKey = Convert.ToString(ConfigurationManager.AppSettings["SendGridApiKey"]);
var client = new SendGridClient(apiKey);
var myMessage = new SendGridMessage();
myMessage.AddTo(new EmailAddress(message.Destination));
myMessage.SetFrom(new EmailAddress("Joe#contoso.com", "Joe S."));
myMessage.SetSubject(message.Subject);
myMessage.AddContent(MimeType.Text, message.Body);
myMessage.AddContent(MimeType.Html, message.Body);
try
{
var response = await client.SendEmailAsync(msg);
}
catch(Exception err)
{
Trace.TraceError("Failed to create Web transport: ."+err.message);
await Task.FromResult(0);
}
}
You need to install Sendgrid package from nuget using the below command before writing the code -
Install-Package Sendgrid -Version 9.10.0
The full code is like below -
using SendGrid;
using SendGrid.Helpers.Mail;
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}
// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
var apiKey = Convert.ToString(ConfigurationManager.AppSettings["SendGridApiKey"]);
var client = new SendGridClient(apiKey);
var myMessage = new SendGridMessage();
myMessage.AddTo(new EmailAddress(message.Destination));
myMessage.SetFrom(new EmailAddress("Joe#contoso.com", "Joe S."));
myMessage.SetSubject(message.Subject);
myMessage.AddContent(MimeType.Text, message.Body);
myMessage.AddContent(MimeType.Html, message.Body);
try
{
var response = await client.SendEmailAsync(msg);
}
catch(Exception err)
{
Trace.TraceError("Failed to create Web transport: ."+err.message);
await Task.FromResult(0);
}
}
}
Here is a link to example code :
https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md
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.
I am completely new to windows phone app development. I just installed the Visual Studio 2015 and started working on a new app. Was going all smooth until the time arrived to call a web service of my back-office. First I was unable to debug why and where the code threw exception. Then I realized and tried writing the exception's message to a text block.
Note I have tried using the same code what I got from working examples found by Googling.
Error: "HRESULT E_FAIL has been returned from a call to a COM component"
Code:
public async Task<dynamic> getHomeCategories()
{
string url = string.Format("my working api url");
var uri = new Uri(url);
var client = new HttpClient();
dynamic resultObj = "";
//using (HttpResponseMessage response = await client.GetAsync(url))
try
{
var response = await client.GetStringAsync(uri);
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
wsresult.Text = "okay";
return resultObj;
}
catch(Exception Ex) {
wsresult.Text = Ex.Message.ToString(); return resultObj;
}
}
Try this
HttpResponseMessage response;
public async Task<string> webserviceResponse(string HttpMethod)
{
// check internet connection is available or not
if (NetworkInterface.GetIsNetworkAvailable() == true)
{
// CancellationTokenSource cts = new CancellationTokenSource(2000); // 2 seconds
HttpClient client = new HttpClient();
MultipartFormDataContent mfdc = new MultipartFormDataContent();
mfdc.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
string GenrateUrl = "your url";
if (HttpMethod == "POST")
{
response = await client.PostAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "PUT")
{
response = await client.PutAsync(GenrateUrl, mfdc);
}
else if (HttpMethod == "GET")
{
response = await client.GetAsync(GenrateUrl);
}
var respon = await response.Content.ReadAsStringAsync();
string convert_response = respon.ToString();
return convert_response;
}
else
{
return "0";
}
}
It is your use of dynamic types giving you the expection
dynamic resultObj = "";
This makes resultObj a string
resultObj = Newtonsoft.Json.Linq.JObject.Parse(response);
The you try and stick a class in it. I would recommend you parse the response in to a type.