当机器人说没有答案时,我想触发邮件

时间:2019-06-12 11:00:20

标签: email botframework

当机器人说它没有答案时,我想触发邮件。

我使用的是MS机器人框架SDk4,还使用LUIS和QnA maker,当该机器人到达表示无法回答的地步时,我们希望触发邮件或在其中添加新项目共享点

2 个答案:

答案 0 :(得分:1)

如果您想对SharePoint列表添加否定答案,我设法使用csom-node程序包和Bot Framework v4 / NodeJS使其正常运行。当然,这不是最优雅的解决方案,但是它可以工作。

Bot.JS

const csomapi = require('../node_modules/csom-node');
settings = require('../settings').settings;

// Set CSOM settings
csomapi.setLoaderOptions({url: settings.siteurl});

再往下一点...

// If no answers were returned from QnA Maker, reply with help.
            } else {
                await context.sendActivity("Er sorry, I don't seem to have an answer.");
                console.log(context.activity.text);
                var response = context.activity.text;
                var authCtx = new AuthenticationContext(settings.siteurl);
                authCtx.acquireTokenForApp(settings.clientId, settings.clientSecret, function (err, data) {

                    var ctx = new SP.ClientContext("/sites/yoursite");  //set root web
                    authCtx.setAuthenticationCookie(ctx);  //authenticate
                        var web = ctx.get_web();
                        var list = web.get_lists().getByTitle('YourList');
                        var creationInfo = new SP.ListItemCreationInformation();
                        var listItem = list.addItem(creationInfo);
                        listItem.set_item('Title', response);
                        listItem.update();
                        ctx.load(listItem);
                        ctx.executeQueryAsync();
                });
            }

答案 1 :(得分:0)

Proactive Messaging不能真正用于电子邮件(防止垃圾邮件),因此最好不要在邮件部分使用Bot Framework SDK。 @Baruch的链接How to send email in ASP.NET C#很好,如果您使用的是C#SDK。这是sending emails in Node的一个。

您要做的就是在QnA Maker没有返回任何结果时发送电子邮件。在this sample中,您可以这样做here

if (response != null && response.Length > 0)
{
    await turnContext.SendActivityAsync(MessageFactory.Text(response[0].Answer), cancellationToken);
}
else
{
    await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);

    // Add code that sends Notification Email

}

话虽如此,如果您想尝试半主动式路由,则可以在漫游器中启用电子邮件通道,然后使用以下方法:

if (response != null && response.Length > 0)
{
    await turnContext.SendActivityAsync(MessageFactory.Text(response[0].Answer), cancellationToken);
}
else
{
    await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);

    MicrosoftAppCredentials.TrustServiceUrl(@"https://email.botframework.com/", DateTime.MaxValue);

    var user = new ChannelAccount(name: "MyUser", id: "<notified Email Address>");
    var parameters = new ConversationParameters()
    {
        Members = new ChannelAccount[] { user },
        Bot = turnContext.Activity.Recipient
    };
    var connector = new ConnectorClient(new Uri("https://email.botframework.com"), "<appId>", "<appPassword>");
    var conversation = await connector.Conversations.CreateConversationAsync(parameters);

    var activity = MessageFactory.Text("This is a notification email");
    activity.From = parameters.Bot;
    activity.Recipient = user;

    await connector.Conversations.SendToConversationAsync(conversation.Id, activity);

}

要注意的是,<notified Email Address>必须先向机器人发送一条消息,然后才能进行任何通知。如果没有,它将返回一个401: Unauthorized错误。再说一次,我不推荐这条路线。


注意:如果使用的是Dispatch示例,则应放置代码here

private async Task ProcessSampleQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            _logger.LogInformation("ProcessSampleQnAAsync");

            var results = await _botServices.SampleQnA.GetAnswersAsync(turnContext);
            if (results.Any())
            {
                await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
            }
            else
            {
                // PLACE IT HERE
                await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
            }
        }