我创建了一个QnA bot。为此,我在QnA制造商中添加了一些问题和答案,并在我的机器人应用程序中使用它。
如果用户询问预定义问题中不存在的问题,则bot需要从用户处获得未回答的问题。对于该机器人,将询问用户的详细信息,如电子邮件ID。然后机器人需要发送电子邮件给专业人士(机器人已经有他的电子邮件ID),电子邮件的内容应该是未答复的问题。我附上了我的代码。
但是我无法发送邮件。我在模拟器中输入电子邮件ID后说:
例外:发送邮件失败。[文件类型' text / plain']。
那么如何将邮件从机器人发送给特定的人。
代码:
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using System.Net;
using System.Net.Mail;
namespace QNA_Bot.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
private static string unans;
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
unans = activity.Text;
// calculate something for us to return
var text = (activity.Text ?? string.Empty);
var url = "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/b07f68a7-4a65-4330-8574-6529cf4c9391/generateAnswer";
var httpContent = new StringContent("{'question':'"+text+"'}",Encoding.UTF8,"application/json");
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "57feb2c9cd734fe48f624c4361dceb59");
var httpResponse = await httpClient.PostAsync(url, httpContent);
var httpResponseMessage = await httpResponse.Content.ReadAsStringAsync();
dynamic httpResponseJson = JsonConvert.DeserializeObject(httpResponseMessage);
var replyMessage = (string)httpResponseJson.answers[0].answer;
if(replyMessage== "No good match found in the KB")
{
await context.PostAsync("Sorry, I couldn't answer you...");
await context.PostAsync("I can contact a relevant person and let you know...");
await context.PostAsync("So could you give your mail id... If so type 'yes'");
context.Wait(GettingMailIdAsync);
}
else
{
await context.PostAsync(replyMessage);
context.Wait(MessageReceivedAsync);
}
}
private async Task GettingMailIdAsync(IDialogContext context,IAwaitable<object> result)
{
var activity = await result as Activity;
var ans = activity.Text;
if (ans == "yes")
{
await context.PostAsync("Type your Mail ID");
context.Wait(SendMailIdAsync);
}
}
private async Task SendMailIdAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var mailId = activity.Text;
var fromAddress = new MailAddress("From@gmail.com", "From Name");
var ToAddress = new MailAddress("To@gmail.com", "To Name");
const string fromPassword = "Pass";
const string subject = "User_Question_From_ERPBOT";
string body = "This Question was asked by" + mailId + " \n " + unans + "If you know the answer, Please contact the person through " + mailId+"The following is the question"+unans;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, ToAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
}
}