ChoiceFactory class

一组实用工具函数,可帮助设置包含选项列表的“message”活动的格式。

注解

此示例演示如何创建一条消息,其中包含基于基础通道的功能有条件设置格式的选项列表:

const { ChoiceFactory } = require('botbuilder-choices');

const message = ChoiceFactory.forChannel(context, ['red', 'green', 'blue'], `Pick a color.`);
await context.sendActivity(message);

方法

forChannel(string | TurnContext, string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已根据给定通道的功能自动设置格式的选项列表。

heroCard(string | Choice[], string, string)

创建一条消息 活动,其中包含已添加为 HeroCard 列表。

inline(string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已格式化为内联列表的选项列表。

list(string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已格式化为编号列表或项目符号列表的选项列表。

suggestedAction(string | Choice[], string, string)

返回一个“message”活动,其中包含已添加为建议操作的选项列表。

toChoices(string | Choice[] | undefined)

获取 string 和基于 Choice 的选择的混合列表,并将其作为 Choice[]返回。

方法详细信息

forChannel(string | TurnContext, string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已根据给定通道的功能自动设置格式的选项列表。

static function forChannel(channelOrContext: string | TurnContext, choices: string | Choice[], text?: string, speak?: string, options?: ChoiceFactoryOptions): Partial<Activity>

参数

channelOrContext

string | TurnContext

当前会话轮次的通道 ID 或上下文对象。

choices

string | Choice[]

要呈现的选项列表。

text

string

(可选)消息的文本。

speak

string

(可选)SSML 要为消息说话。

options
ChoiceFactoryOptions

(可选)将呈现为列表时要使用的格式设置选项。

返回

Partial<Activity>

已创建的消息活动。

注解

该算法倾向于将提供的选项列表的格式设置为建议的操作,但如果通道本身不支持建议的操作,或者通道的标题过长,则可以决定使用基于文本的列表。

如果算法决定使用列表,它将使用内联列表(如果有 3 个或更少的选项,并且都有短标题)。 否则,将使用编号列表。

const message = ChoiceFactory.forChannel(context, [
   { value: 'red', action: { type: 'imBack', title: 'The Red Pill', value: 'red pill' } },
   { value: 'blue', action: { type: 'imBack', title: 'The Blue Pill', value: 'blue pill' } },
], `Which do you choose?`);
await context.sendActivity(message);

heroCard(string | Choice[], string, string)

创建一条消息 活动,其中包含已添加为 HeroCard 列表。

static function heroCard(choices?: string | Choice[], text?: string, speak?: string): Activity

参数

choices

string | Choice[]

自选。 要添加 选项 列表。

text

string

自选。 消息的文本。

speak

string

自选。 机器人在启用了语音的通道上朗读的 SSML 文本。

返回

Activity

一个 活动,其中选项与按钮 HeroCard

inline(string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已格式化为内联列表的选项列表。

static function inline(choices: string | Choice[], text?: string, speak?: string, options?: ChoiceFactoryOptions): Partial<Activity>

参数

choices

string | Choice[]

要呈现的选项列表。

text

string

(可选)消息的文本。

speak

string

(可选)SSML 要为消息说话。

options
ChoiceFactoryOptions

(可选)用于调整列表呈现的格式设置选项。

返回

Partial<Activity>

已创建的消息活动。

注解

本示例生成“选取颜色:(1.红色、2.绿色或 3.蓝色)”的消息文本:

const message = ChoiceFactory.inline(['red', 'green', 'blue'], `Pick a color:`);
await context.sendActivity(message);

list(string | Choice[], string, string, ChoiceFactoryOptions)

返回一个“message”活动,其中包含已格式化为编号列表或项目符号列表的选项列表。

static function list(choices: string | Choice[], text?: string, speak?: string, options?: ChoiceFactoryOptions): Partial<Activity>

参数

choices

string | Choice[]

要呈现的选项列表。

text

string

(可选)消息的文本。

speak

string

(可选)SSML 要为消息说话。

options
ChoiceFactoryOptions

(可选)用于调整列表呈现的格式设置选项。

返回

Partial<Activity>

已创建的消息活动。

注解

此示例生成一条消息,其中包含显示为编号列表的选项:

const message = ChoiceFactory.list(['red', 'green', 'blue'], `Pick a color:`);
await context.sendActivity(message);

suggestedAction(string | Choice[], string, string)

返回一个“message”活动,其中包含已添加为建议操作的选项列表。

static function suggestedAction(choices: string | Choice[], text?: string, speak?: string): Partial<Activity>

参数

choices

string | Choice[]

要添加的选项列表。

text

string

(可选)消息的文本。

speak

string

(可选)SSML 要为消息说话。

返回

Partial<Activity>

具有选项作为建议操作的活动。

注解

此示例生成一条消息,其中包含显示为建议的操作按钮的选项:

const message = ChoiceFactory.suggestedAction(['red', 'green', 'blue'], `Pick a color:`);
await context.sendActivity(message);

toChoices(string | Choice[] | undefined)

获取 string 和基于 Choice 的选择的混合列表,并将其作为 Choice[]返回。

static function toChoices(choices: string | Choice[] | undefined): Choice[]

参数

choices

string | Choice[] | undefined

要添加的选项列表。

返回

Choice[]

选项列表。

注解

此示例将基于字符串的简单选择数组转换为格式正确的 Choice[]

如果 ChoicePartial<CardAction>Choice.action.toChoices() 将尝试填充 Choice.action

const choices = ChoiceFactory.toChoices(['red', 'green', 'blue']);