尝试接受照片作为 Microsoft 聊天机器人附件并将其作为电子邮件附件发送 C#
trying to accept photo as Microsoft chat bot attachment and send it as an email attachment C#
我正在尝试在 Microsoft 聊天机器人 waterFall 对话框中接受照片作为附件...然后将其作为附件或(甚至在正文中)电子邮件功能(我创建的)发送。
电子邮件功能似乎可以正常工作....但是我无法在电子邮件中传递照片附件
就像:
无法将 Microsoft bot 架构附件转换为字符串
这是我的代码:第一种方法是要求用户上传照片
private static async Task<DialogTurnResult> UploadAttachmentAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["desc"] = (string)stepContext.Result;
if (lang == "en")
{
lang = "en";
return await stepContext.PromptAsync(
attachmentPromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"Can you upload a ScreenShot of the Error?"),
});
}
else if (lang == "ar")
{
lang = "ar";
return await stepContext.PromptAsync(
attachmentPromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"هل يمكنك تحميل لقطة شاشة للخطأ؟"),
});
}
else return await stepContext.NextAsync();
}
第二种方法是上传代码本身:
private async Task<DialogTurnResult> UploadCodeAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
List<Attachment> attachments = (List<Attachment>)stepContext.Result;
string replyText = string.Empty;
foreach (var file in attachments)
{
// Determine where the file is hosted.
var remoteFileUrl = file.ContentUrl;
// Save the attachment to the system temp directory.
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
// Download the actual attachment
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
replyText += $"Attachment \"{file.Name}\"" +
$" has been received and saved to \"{localFileName}\"\r\n";
}
return await stepContext.NextAsync();
}
第三个是将照片存储为 activity 结果以便稍后传递
private static async Task<DialogTurnResult> ProcessImageStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["picture"] = ((IList<Attachment>)stepContext.Result)?.FirstOrDefault();
await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Attachment Recieved...Thank you!!") }, cancellationToken);
return await stepContext.EndDialogAsync();
}
最后是我存储从每个 stepcontext 获得的值并应在电子邮件正文中传递的步骤:(附件应该放在 ticketProfile.screenShot 中)
private static async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var ticketProfile = await _ticketDialogAccessor.GetAsync(stepContext.Context, () => new Ticket(), cancellationToken);
ticketProfile.userType = (string)stepContext.Values["userType"];
ticketProfile.staffType = (string)stepContext.Values["staffType"];
ticketProfile.empIdInfo = (string)stepContext.Values["shareId"];
ticketProfile.emailInfo = (string)stepContext.Values["shareEmail"];
ticketProfile.helpType = (string)stepContext.Values["helpType"];
ticketProfile.describeHelp = (string)stepContext.Values["desc"];
ticketProfile.screenShot = (Attachment)stepContext.Values["picture"];
string[] paths = { ".", "adaptiveCard.json" };
var cardJsonObject = JObject.Parse(File.ReadAllText(Path.Combine(paths)));
var userEmailValue = cardJsonObject.SelectToken("body[2].facts[0]");
var userIdValue = cardJsonObject.SelectToken("body[2].facts[1]");
var userTypeValue = cardJsonObject.SelectToken("body[2].facts[2]");
var staffTypeValue = cardJsonObject.SelectToken("body[2].facts[3]");
var helpTypeValue = cardJsonObject.SelectToken("body[2].facts[4]");
var describeValue = cardJsonObject.SelectToken("body[2].facts[5]");
userEmailValue["value"] = ticketProfile.emailInfo;
userIdValue["value"] = ticketProfile.empIdInfo;
userTypeValue["value"] = ticketProfile.userType;
staffTypeValue["value"] = ticketProfile.staffType;
helpTypeValue["value"] = ticketProfile.helpType;
describeValue["value"] = ticketProfile.describeHelp;
var attachment = new Attachment()
{
Content = cardJsonObject,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
Email($"Here you go{Environment.NewLine}{ticketProfile.screenShot}");
return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}
电子邮件功能:
public static void Email(string htmlString)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("hassanjarko55@gmail.com");
mail.Subject = "New Ticket";
mail.Body = htmlString;
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new NetworkCredential("user Name, "password");
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.To.Add(new MailAddress("hassan.jarko@yahoo.com"));
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
}
catch (Exception e)
{
e.ToString();
}
}
public class Ticket
{
public string userType { get; set; }
public string staffType { get; set; }
public string helpType { get; set; }
public string helpWith { get; set; }
public string describeHelp { get; set; }
public Attachment screenShot { get; set; }
public string emailInfo { get; set; }
public string empIdInfo { get; set; }
}
任何帮助将不胜感激...在此先感谢
您是否尝试过附加文件名而不是附加 microsoft.bot.schema.attachment 类型
也看看@这个可能对你有帮助
How do I add an attachment to an email using System.Net.Mail?
同样在您的电子邮件功能中,您需要添加此代码才能在您的电子邮件中添加附件
mail.Attachments.Add(new Attachment());
注意:我对C#没有那么深的了解,但我喜欢参与这类没有答案的问题,以帮助未来的程序员
非常感谢@Alphonso...感谢您为帮助...所做的努力
我做了以下操作,将从我的聊天机器人收到的图片作为电子邮件附件发送:
1-将我的电子邮件功能方法移动到 SummaryStepAsync 方法中。
2-使用 WebClient
下载用户接受的图像
string contentURL = ticketProfile.screenShot.ContentUrl;
string name = ticketProfile.screenShot.Name;
using (var client = new WebClient())
{
client.DownloadFile(contentURL, name);
client.DownloadData(contentURL);
client.DownloadString(contentURL);
}
3-将其传递到我的电子邮件功能
try
{
HttpClient httpClient = new HttpClient();
//Help Desk Email Settings
MailMessage helpDeskMail = new MailMessage();
if (ticketProfile.screenShot != null)
{
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(ticketProfile.screenShot.Name);
helpDeskMail.Attachments.Add(data);
}
就是这样:)
我正在尝试在 Microsoft 聊天机器人 waterFall 对话框中接受照片作为附件...然后将其作为附件或(甚至在正文中)电子邮件功能(我创建的)发送。 电子邮件功能似乎可以正常工作....但是我无法在电子邮件中传递照片附件
就像:
无法将 Microsoft bot 架构附件转换为字符串
这是我的代码:第一种方法是要求用户上传照片
private static async Task<DialogTurnResult> UploadAttachmentAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["desc"] = (string)stepContext.Result;
if (lang == "en")
{
lang = "en";
return await stepContext.PromptAsync(
attachmentPromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"Can you upload a ScreenShot of the Error?"),
});
}
else if (lang == "ar")
{
lang = "ar";
return await stepContext.PromptAsync(
attachmentPromptId,
new PromptOptions
{
Prompt = MessageFactory.Text($"هل يمكنك تحميل لقطة شاشة للخطأ؟"),
});
}
else return await stepContext.NextAsync();
}
第二种方法是上传代码本身:
private async Task<DialogTurnResult> UploadCodeAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
List<Attachment> attachments = (List<Attachment>)stepContext.Result;
string replyText = string.Empty;
foreach (var file in attachments)
{
// Determine where the file is hosted.
var remoteFileUrl = file.ContentUrl;
// Save the attachment to the system temp directory.
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
// Download the actual attachment
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
replyText += $"Attachment \"{file.Name}\"" +
$" has been received and saved to \"{localFileName}\"\r\n";
}
return await stepContext.NextAsync();
}
第三个是将照片存储为 activity 结果以便稍后传递
private static async Task<DialogTurnResult> ProcessImageStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["picture"] = ((IList<Attachment>)stepContext.Result)?.FirstOrDefault();
await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Attachment Recieved...Thank you!!") }, cancellationToken);
return await stepContext.EndDialogAsync();
}
最后是我存储从每个 stepcontext 获得的值并应在电子邮件正文中传递的步骤:(附件应该放在 ticketProfile.screenShot 中)
private static async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var ticketProfile = await _ticketDialogAccessor.GetAsync(stepContext.Context, () => new Ticket(), cancellationToken);
ticketProfile.userType = (string)stepContext.Values["userType"];
ticketProfile.staffType = (string)stepContext.Values["staffType"];
ticketProfile.empIdInfo = (string)stepContext.Values["shareId"];
ticketProfile.emailInfo = (string)stepContext.Values["shareEmail"];
ticketProfile.helpType = (string)stepContext.Values["helpType"];
ticketProfile.describeHelp = (string)stepContext.Values["desc"];
ticketProfile.screenShot = (Attachment)stepContext.Values["picture"];
string[] paths = { ".", "adaptiveCard.json" };
var cardJsonObject = JObject.Parse(File.ReadAllText(Path.Combine(paths)));
var userEmailValue = cardJsonObject.SelectToken("body[2].facts[0]");
var userIdValue = cardJsonObject.SelectToken("body[2].facts[1]");
var userTypeValue = cardJsonObject.SelectToken("body[2].facts[2]");
var staffTypeValue = cardJsonObject.SelectToken("body[2].facts[3]");
var helpTypeValue = cardJsonObject.SelectToken("body[2].facts[4]");
var describeValue = cardJsonObject.SelectToken("body[2].facts[5]");
userEmailValue["value"] = ticketProfile.emailInfo;
userIdValue["value"] = ticketProfile.empIdInfo;
userTypeValue["value"] = ticketProfile.userType;
staffTypeValue["value"] = ticketProfile.staffType;
helpTypeValue["value"] = ticketProfile.helpType;
describeValue["value"] = ticketProfile.describeHelp;
var attachment = new Attachment()
{
Content = cardJsonObject,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
Email($"Here you go{Environment.NewLine}{ticketProfile.screenShot}");
return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
}
电子邮件功能:
public static void Email(string htmlString)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("hassanjarko55@gmail.com");
mail.Subject = "New Ticket";
mail.Body = htmlString;
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new NetworkCredential("user Name, "password");
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.To.Add(new MailAddress("hassan.jarko@yahoo.com"));
mail.IsBodyHtml = true;
SmtpServer.Send(mail);
}
catch (Exception e)
{
e.ToString();
}
}
public class Ticket
{
public string userType { get; set; }
public string staffType { get; set; }
public string helpType { get; set; }
public string helpWith { get; set; }
public string describeHelp { get; set; }
public Attachment screenShot { get; set; }
public string emailInfo { get; set; }
public string empIdInfo { get; set; }
}
任何帮助将不胜感激...在此先感谢
您是否尝试过附加文件名而不是附加 microsoft.bot.schema.attachment 类型
也看看@这个可能对你有帮助 How do I add an attachment to an email using System.Net.Mail?
同样在您的电子邮件功能中,您需要添加此代码才能在您的电子邮件中添加附件
mail.Attachments.Add(new Attachment());
注意:我对C#没有那么深的了解,但我喜欢参与这类没有答案的问题,以帮助未来的程序员
非常感谢@Alphonso...感谢您为帮助...所做的努力 我做了以下操作,将从我的聊天机器人收到的图片作为电子邮件附件发送: 1-将我的电子邮件功能方法移动到 SummaryStepAsync 方法中。 2-使用 WebClient
下载用户接受的图像 string contentURL = ticketProfile.screenShot.ContentUrl;
string name = ticketProfile.screenShot.Name;
using (var client = new WebClient())
{
client.DownloadFile(contentURL, name);
client.DownloadData(contentURL);
client.DownloadString(contentURL);
}
3-将其传递到我的电子邮件功能
try
{
HttpClient httpClient = new HttpClient();
//Help Desk Email Settings
MailMessage helpDeskMail = new MailMessage();
if (ticketProfile.screenShot != null)
{
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(ticketProfile.screenShot.Name);
helpDeskMail.Attachments.Add(data);
}
就是这样:)