private async Task <HttpResponseMessage> RejectMessageFromUnexpectedTenant(Activity activity, ConnectorClient connectorClient)
        {
            //Set the OFFICE_365_TENANT_FILTER key in web.config file with Tenant Information
            //Validate bot for specific teams tenant if any
            string currentTenant = "#ANY#";

            try
            {
                currentTenant = activity.GetTenantId();
            }
            catch (Exception e)
            {
                Trace.TraceError($"Exception from activity.GetTenantId(): {e}");
            }

            if (Middleware.RejectMessageBasedOnTenant(activity, currentTenant))
            {
                Bot.Connector.Activity replyActivity = activity.CreateReply();
                replyActivity.Text = Strings.TenantLevelDeniedAccess;

                await BotConnectorUtility.BuildRetryPolicy().ExecuteAsync(async() =>
                                                                          await connectorClient.Conversations.ReplyToActivityAsync(replyActivity));

                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }

            return(null);
        }
        private async Task HandleSystemMessageAsync(Activity message, ConnectorClient connectorClient, CancellationToken cancellationToken)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // This shows how to send a welcome message in response to a conversationUpdate event

                // We're only interested in member added events
                if (message.MembersAdded?.Count > 0)
                {
                    // Determine if the bot was added to the team/conversation
                    var botId       = message.Recipient.Id;
                    var botWasAdded = message.MembersAdded.Any(member => member.Id == botId);

                    // Create the welcome message to send
                    Bot.Connector.Activity welcomeMessage = message.CreateReply();
                    welcomeMessage.Attachments = new List <Attachment>
                    {
                        new HeroCard(Strings.HelpTitle)
                        {
                            Title    = "Hello! I am Expert Intelligence Bot.",
                            Subtitle = "I am supported by experts who can work for you. " +
                                       "You can also request virtual assistance such as booking an appointment with car dealer. " +
                                       $"Send me a SMS at {ConfigurationManager.AppSettings["BotPhoneNumber"]}",
                        }.ToAttachment()
                    };

                    if (!(message.Conversation.IsGroup ?? false))
                    {
                        // 1:1 conversation event

                        // If the user hasn't received a first-run message yet, then send a message to the user
                        // introducing your bot and what it can do. Do NOT send this blindly, as your bot can receive
                        // spurious conversationUpdate events, especially if you use proactive messaging.
                        using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
                        {
                            var address      = Address.FromActivity(message);
                            var botDataStore = scope.Resolve <IBotDataStore <BotData> >();
                            var botData      = await botDataStore.LoadAsync(address, BotStoreType.BotUserData, cancellationToken);

                            if (!botData.GetProperty <bool>("IsFreSent"))
                            {
                                await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(welcomeMessage, cancellationToken);

                                // Remember that we sent the welcome message already
                                botData.SetProperty("IsFreSent", true);
                                await botDataStore.SaveAsync(address, BotStoreType.BotUserData, botData, cancellationToken);
                            }
                            else
                            {
                                // First-run message has already been sent, so skip sending it again.
                                // Do not remove the check for IsFreSent above. Your bot can receive spurious conversationUpdate
                                // activities from chat service, so if you always respond to all of them, you will send random
                                // welcome messages to users who have already received the welcome.
                            }
                        }
                    }
                    else
                    {
                        // Not 1:1 chat event (bot or user was added to a team or group chat)
                        if (botWasAdded)
                        {
                            // Bot was added to the team
                            // Send a message to the team's channel, introducing your bot and what you can do
                            await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(welcomeMessage, cancellationToken);
                        }
                        else
                        {
                            // Other users were added to the team/conversation
                        }
                    }
                }
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing that the user is typing
            }
        }