Send and receive text message

APPLIES TO: yesSDK v4 no SDK v3

The primary way your bot will communicate with users, and likewise receive communication, is through message activities. Some messages may simply consist of plain text, while others may contain richer content such as cards or attachments. Your bot's turn handler receives messages from the user, and you can send responses to the user from there. The turn context object provides methods for sending messages back to the user. This article describes how to send simple text messages.

Markdown is supported for most text fields, but support may vary by channel.

For a running bot sending and receiving messages, follow the quickstarts at the top of the table of contents or check out the article on how bots work, which also links to simple samples available for you to run yourself.

Send a text message

To send a simple text message, specify the string you want to send as the activity:

In the bot's activity handlers, use the turn context object's SendActivityAsync method to send a single message response. You can also use the object's SendActivitiesAsync method to send multiple responses at once.

await turnContext.SendActivityAsync($"Welcome!");

Receive a text message

To receive a simple text message, use the text property of the activity object.

In the bot's activity handlers, use the following code to receive a message.

var responseMessage = turnContext.Activity.Text;

Send a typing indicator

Users expect a timely response to their messages. If your bot performs some long-running task like calling a server or executing a query without giving the user some indication that the bot heard them, the user could get impatient and send additional messages or just assume the bot is broken.

Web Chat and Direct Line channel bots can support the sending of a typing indication to show the user that the message was received and is being processed. Be aware that your bot needs to let the turn end within 15 seconds or the Connector service will timeout. For longer processes read more about sending proactive messages.

The following example demonstrates how to send a typing indication.

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
    if (string.Equals(turnContext.Activity.Text, "wait", System.StringComparison.InvariantCultureIgnoreCase))
    {
        await turnContext.SendActivitiesAsync(
            new Activity[] {
                new Activity { Type = ActivityTypes.Typing },
                new Activity { Type = "delay", Value= 3000 },
                MessageFactory.Text("Finished typing", "Finished typing"),
            },
            cancellationToken);
    }
    else
    {
        var replyText = $"Echo: {turnContext.Activity.Text}. Say 'wait' to watch me type.";
        await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
    }
}

Additional resources

Next steps