I want to trigger mail when the bot says that it h

2019-09-22 05:25发布

问题:

I want to trigger mail when the bot says that it has no answer.

I'm using MS bot framework SDk4, and using LUIS and QnA maker also, when the bot reached the to a point where it says that it has no answer , we want a mail to be triggered or add a new item in the sharepoint

回答1:

If you want to add a no answer to a SharePoint List, I managed to get it working using the csom-node package and Bot Framework v4 / NodeJS. Granted, it's not the most elegant solution, but it works.

Bot.JS

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

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

Bit further down the page...

// 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();
                });
            }


回答2:

Proactive Messaging doesn't really work for email (to prevent spam), so you're better off not using the Bot Framework SDK for the email portion. @Baruch's link, How to send email in ASP.NET C# is good if you're using the C# SDK. Here's one for sending emails in Node.

All you have to do is send the email when QnA Maker doesn't return any results. In this sample, you would do so 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

}

That being said, if you'd like to try a semi-proactive route, you can enable the Email Channel in your bot, then use this:

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);

}

The catch is that <notified Email Address> has to send a message to the bot before any notifications will work. If it doesn't, it will return a 401: Unauthorized error. Again, I don't recommend this route.


Note: If you're using the Dispatch sample, you'd place the code 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);
            }
        }