Esta pagina se ve mejor con JavaScript habilitado

How to: send messages to a bot using the Bot Connector [English]

 ·  ☕ 4 minutos lectura  ·  ✍️ Saturnino Pimentel

I have been working with the Microsoft Bot Framework V4 and I was surprised by the changes that the Microsoft’s Team have implemented for support in a better way the conversations and the different dialogs that the bot can keep with the end user, also the changes made to the projects for being executed with ASP.Net Core that enable other servers to execute this type of projects.

Using this framework, I have found some scenarios in which is necessary to send messages to a chat in a direct way without pass through the portal where I have published the bot. An example of these scenarios could be when you want to send messages to a user in a personal or group channel to remind them of a process that the user requested to postpone, for this type of scenarios we can use the Bot Connector to make the work easier because also we can use the REST API. The main advantage to use the Bot Connector is that the NuGet package contains all the necessary classes to create the Activity that we should send to the Bot Service.

In order to send messages, it’s necessary that we have the conversation’s information which we want to interact with. Surely you will ask how you can obtain that information, the easy and safe way is to store the conversation’s information the first time that the user interacts with the bot although it’s possible that you need to handle the case when the ServiceUrl change (a rare event according to the documentation).


Time to code.

To start, we need to generate the ConnectorClient where we will pass the bot’s id and secret both of them should be in the settings of your App Service.

static ConnectorClient GetConnectorClient()

            MicrosoftAppCredentials appCredentials =
               new MicrosoftAppCredentials(botClientId, botSecret);

            Uri uri = new Uri(serviceUrl);
            ConnectorClient connectorClient =
                new ConnectorClient(uri, appCredentials);

            return connectorClient;

After that, we will create a method for sending the message in which we will pass the conversation’s information, also remember that you need to specify the channel that you wish the bot connector uses.

private static async Task SendMessageAsync(
            ConnectorClient connectorClient,
            string conversationId,
            string recipientId,
            string fromId,
            string message)
            Activity messageActivity =
                new Activity();
            messageActivity.Type = ActivityTypes.Message;
            messageActivity.Text = message;
            messageActivity.ChannelId = Channels.Webchat;
            messageActivity.ServiceUrl = serviceUrl;
            messageActivity.Conversation = new ConversationAccount()
                Id = conversationId
            messageActivity.Recipient = new ChannelAccount()
                Id = recipientId
            messageActivity.From = new ChannelAccount()
                Id = fromId

            await connectorClient


In this example, we will use a console application for sending the message but you can use a web application.

private static string botClientId = "bot_id";
        private static string botSecret = "bot_secret";
        //This url could change
        private static string serviceUrl = "service_url";
        private static string conversationId = "conversation_id";
        private static string recipientId = "recipient_Id";
        private static string fromId = "from_Id";

        static async Task Main(string[] args)
            ConnectorClient connectorClient = GetConnectorClient();
                await SendMessageAsync(connectorClient,
                    "Mensaje desde la consola.");
            catch (Exception ex)


As usual, I let you the code in my GitHub repository.

Now that you know how to send messages to a conversation, you could add functionality for support scheduled of the task that the user request to your bot, the easier way that I found in StackOverflow was using queues with delays and an azure function that read the queues’ message when it turns visible and after that sends the message to the user.

I leave you, but I like to ask you. Have you ever had a similar scenario? How did you solve it?

Happy coding!


Bot Connector


I am learning English and I need to practice, I probably made mistakes in this publication. If you think I need to change something in this publication, please let me know, I’ll work on that. Thank you!

compartir en

Saturnino Pimentel
Escrito por
Saturnino Pimentel
Software developer