Search code examples
.netazurebotframework

Get notification when someone has used your bot


I'm developing a bot that will replace a contact form for a company. Is there an easy way for the developer or company to get notification when someone has used the bot? I'm using Microsoft Bot Framework and cosmosDB to store state data.


Solution

  • There are a couple ways to do this. Off the top of my head:

    You can set email as a channel for your bot, but this simply means that the user will communicate with your bot via email. But you could track customer contacts via this email.

    Otherwise, as D4CKCIDE said above, you can embed some simple logic to shoot an notification as soon a certain process is hit in your bot logic. For example, I embedded a quick function to send an email from one email address to another (in this case from my outlook email to my gmail) as soon as a 'conversationUpdate' activity is hit, because that means a new user has connected with my bot. It doesn't send me the whole history, but just a ping as a reminder, for tracking purposes:

    else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
    {
        if (turnContext.Activity.MembersAdded != null)
        {
            // Iterate over all new members added to the conversation
            foreach (var member in turnContext.Activity.MembersAdded)
            {
                // Greet anyone that was not the target (recipient) of this message
                // the 'bot' is the recipient for events from the channel,
                // turnContext.Activity.MembersAdded == turnContext.Activity.Recipient.Id 
                // indicates the bot was added to the conversation.
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync($"Hi there - {member.Name}. {WelcomeMessage}", cancellationToken: cancellationToken);
                    await turnContext.SendActivityAsync(InfoMessage, cancellationToken: cancellationToken);
                    await turnContext.SendActivityAsync(PatternMessage, cancellationToken: cancellationToken);
                }
                EmailPingFunction();
            }
        }
    }
    

    Then I built the EmailPingFunction as below:

    private static void EmailPingFunction()
    {
        // setup strings
        string sender = "[email protected]";
        string password = "********"; // put your email's password here ^_^
        string recipient = "[email protected]";
    
        // set the client:
        SmtpClient outlookSmtp = new SmtpClient("smtp-mail.outlook.com");
    
        // use the 587 TLS port
        outlookSmtp.Port = 587;
    
        // set the delivery method
        outlookSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    
        // set the credentials
        outlookSmtp.UseDefaultCredentials = false;
        System.Net.NetworkCredential credentials =
        new System.Net.NetworkCredential(sender, password);
        outlookSmtp.Credentials = credentials;
    
        // enable SS1
        outlookSmtp.EnableSsl = true;
    
        // set up and send the MailMessage
        try
        {
            Console.WriteLine("start to send email over TLS...");
            MailMessage message = new MailMessage(sender, recipient);
            message.Subject = "This is a test of the not-emergency alert system";
            message.Body = "Someone just pinged your bot. Please go into your set storage account to review";
            outlookSmtp.Send(message);
            Console.WriteLine("email was sent successfully!");
        }
        catch (Exception ex)
        {
            Console.WriteLine("failed to send email with the following error:");
            Console.WriteLine(ex.Message);
        }
    }
    

    Just to emphasize what Nicholas R. said, because he's absolutely correct: this is custom, and just one option for how you can work this. It's a down and dirty option of ONE way you can create some sort of notification.