Adaptivecards Action.submit-如何通过单击提交发送电子邮件?

时间:2018-07-21 19:25:26

标签: c# botframework adaptive-cards

我的Luis Bot代码:

public async Task ScheduleAnAppointmentIntent(IDialogContext context, LuisResult result)
{
    var replyMessage = context.MakeMessage();
    var json = await GetCardText1("scheduleappointment");
    //await context.PostAsync(json);
    AdaptiveCardParseResult cardParseResult = AdaptiveCard.FromJson(json);
    replyMessage.Attachments.Add(new Attachment()
    {
        Content = cardParseResult.Card,
        ContentType = AdaptiveCard.ContentType,
        Name = "Card"
    });
    await context.PostAsync(replyMessage);
}
public async Task<string> GetCardText1(string cardName)
{
    var path = HostingEnvironment.MapPath($"/Dialogs/{cardName}.json");
    if (!File.Exists(path))
        return string.Empty;

    using (var f = File.OpenText(path))
    {
        return await f.ReadToEndAsync();
    }
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;
    if (message.Value != null)
    {
        dynamic value = message.Value;
        string name = value.myName.ToString();
        string email = value.myEmail.ToString();
        string tel = value.myTel.ToString();
        string selectvalue = value.CompactSelectVal.ToString();
        string discussiondate = value.mydiscussiondate.ToString();
        string discussionduration = value.mydiscussionduration.ToString();

        System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage();
        message1.To.Add("XXXXXXXX@gmail.com");
        message1.Subject = "Interview Schedule request from" + name;
        message1.From = new System.Net.Mail.MailAddress("XXXXXXX@gmail.com");
        message1.Body = "Name:" + name + "\n Email:" + email + "\nPhone:" + tel + "\nCalltype:" + selectvalue + "\nDiscussion Date:" + discussiondate + "\nDiscussiona Duration:" + discussionduration;
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("XXXXXXXXXXXXXXXXXX@gmail.com", "XXXXXXXXXXXXXXXX");
        smtp.Send(message1);
    }
}

这是带有C#的LUIS Bot。

我可以很好地渲染自适应卡。当我单击提交时,我希望在收件箱中收到一封电子邮件,而不是消息“我的机器人出了毛病”。请帮助我!

1 个答案:

答案 0 :(得分:0)

  

我可以很好地渲染自适应卡。当我单击“提交”时,我希望在收件箱中收到一封电子邮件,而不是消息“我的机器人有问题”。

您似乎想通过路易斯对话框发送AdaptiveCard表单,并希望用户通过单击AdaptiveCard提交按钮来发送电子邮件。根据您的要求,我使用以下示例代码创建并进行测试,这些示例代码对我有用,您可以参考它。

在RootDialog中:

[Serializable]
public class RootDialog : IDialog<object>
{
    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;

        if (activity.Value != null)
        {
            dynamic value = activity.Value;

            //please make sure you can extract all of these values (myName, myEmail ... mydiscussionduration etc) from activity.Value
            string name = value.myName.ToString();
            string email = value.myEmail.ToString();
            string tel = value.myTel.ToString();
            string selectvalue = value.CompactSelectVal.ToString();
            string discussiondate = value.mydiscussiondate.ToString();
            string discussionduration = value.mydiscussionduration.ToString();

            System.Net.Mail.MailMessage message1 = new System.Net.Mail.MailMessage();
            message1.To.Add("xxxx@hotmail.com");
            message1.Subject = "Interview Schedule request from" + name;
            message1.From = new System.Net.Mail.MailAddress("alexzwz1211@gmail.com");
            message1.Body = "Name:" + name + "\n Email:" + email + "\nPhone:" + tel + "\nCalltype:" + selectvalue + "\nDiscussion Date:" + discussiondate + "\nDiscussiona Duration:" + discussionduration;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "xxxx");
            smtp.Send(message1);
        }
        else
        {
            //forward to Luis dialog
            await context.Forward(new BasicLuisDialog(), AfterLuis, activity, CancellationToken.None);
        }

        context.Wait(MessageReceivedAsync);
    }

    private async Task AfterLuis(IDialogContext context, IAwaitable<object> result)
    {
        context.Wait(MessageReceivedAsync);
    }      
}

在BasicLuisDialog中:

[LuisIntent("Greeting")]
public async Task GreetingIntent(IDialogContext context, LuisResult result)
{

    var replyMessage = context.MakeMessage();

    var json = await GetCardText1("scheduleappointment");
    //await context.PostAsync(json);
    AdaptiveCardParseResult cardParseResult = AdaptiveCard.FromJson(json);
    replyMessage.Attachments.Add(new Attachment()
    {
        Content = cardParseResult.Card,
        ContentType = AdaptiveCard.ContentType,
        Name = "Card"
    });

    await context.PostAsync(replyMessage);

    //await this.ShowLuisResult(context, result);
}

public async Task<string> GetCardText1(string cardName)
{
    var path = System.Web.Hosting.HostingEnvironment.MapPath($"/Dialogs/{cardName}.json");
    if (!File.Exists(path))
        return string.Empty;

    using (var f = File.OpenText(path))
    {
        return await f.ReadToEndAsync();
    }
}

scheduleappointment.json

{
  "type": "AdaptiveCard",
  "version": "1.0",
  "body": [
    {
      "type": "TextBlock",
      "size": "large",
      "weight": "bolder",
      "text": "This is Adaptive Card"
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "data": {
        "myName": "Fei Han",
        "myEmail": "feihan@xxx.com",
        "myTel": "xxxx",
        "CompactSelectVal": "val",
        "mydiscussiondate": "date",
        "mydiscussionduration": "duration"
      },
      "title": "Send Email"
    }
  ]
}

注意:

1)您可以使用上面的示例代码进行测试,或者将其与您的实现进行比较

2)您可以检查是否打开了“允许安全程度较低的应用程序”

enter image description here