您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

发送和接收文本消息Send and receive text message

适用范围:yesSDK v4 noSDK v3 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,但支持可能因通道而异。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:

在机器人的活动处理程序中,使用轮次上下文对象的 SendActivityAsync 方法发送单个消息响应。In the bot's activity handlers, use the turn context object's SendActivityAsync method to send a single message response. 还可以使用该对象的 SendActivitiesAsync 方法一次性发送多个响应。You can also use the object's SendActivitiesAsync method to send multiple responses at once.

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

接收短信Receive a text message

若要接收简单的文本消息,请使用 activity 对象的 text 属性。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 和 Direct Line 通道机器人可以支持发送键入指示,向用户显示已接收并且正在处理消息。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. 请注意,机器人需要让该轮聊天在 15 秒内结束,否则连接器服务会超时。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