Mark Hawkins-Wood
Mark Hawkins-Wood

Reputation: 43

Send Proactive Email in the Botframework C# SDK

I have a bot with LUIS integrated, I've emulated it for basic text responses such as responding to a greeting, with a greeting back.

I now want to add an additional message to the NoneIntent so that when a message sent to the bot is classed as being of the NoneIntent, the bot emails a mailbox (the helpdesk) as it's call to action alongside responding with a text response.

So far I have:

 [LuisIntent("None")]
        public async Task NoneIntent(IDialogContext context, LuisResult result)
        {
            //Send call to action email here

            // The text response given back to the user in the bot channel
            await context.PostAsync("I'm sorry, I don't understand your question :(");

        }

Upvotes: 1

Views: 290

Answers (1)

Mark B
Mark B

Reputation: 581

To send a proactive email message in C# via your bot you'll need to set up the email channel for your bot and then set up a new email message (along with connector) in your code:

var botAccount = new ChannelAccount(name: $"{ConfigurationManager.AppSettings["BotId"]}", id: $"{ConfigurationManager.AppSettings["BotEmail"]}".ToLower()); //taken from the email channel on your bot config
var userAccount = new ChannelAccount(name: "Name", id: $"{ConfigurationManager.AppSettings["UserEmail"]}"); //the email account you are sending your messages to
MicrosoftAppCredentials.TrustServiceUrl(@"https://email.botframework.com/", DateTime.MaxValue);
var connector = new ConnectorClient(new Uri("https://email.botframework.com/" ));
var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.Text = "This is the content text of an email!";
message.Locale = "en-Us";


try
{
    await connector.Conversations.SendToConversationAsync((Activity)message);
}
catch (ErrorResponseException e)
{
    Console.WriteLine("Error: ", e.StackTrace);
}

A new conversation is required because you don't have a context saved to send a new email to if you aren't responding directly to an email from your helpdesk account.

Upvotes: 2

Related Questions