ChoiceFactory class
一組公用程式函式,可協助格式化包含選項清單的「訊息」活動。
備註
此範例示範如何建立訊息,其中包含已根據基礎通道的功能有條件地格式化的選項清單:
const { ChoiceFactory } = require('botbuilder-choices');
const message = ChoiceFactory.forChannel(context, ['red', 'green', 'blue'], `Pick a color.`);
await context.sendActivity(message);
方法
| for |
傳回 'message' 活動,其中包含已根據指定通道的功能自動格式化的選項清單。 |
| hero |
建立一則訊息 活動,其中包含已新增為 的 |
| inline(string | Choice[], string, string, Choice |
傳回 'message' 活動,其中包含已格式化為內嵌列表的選項清單。 |
| list(string | Choice[], string, string, Choice |
傳回 'message' 活動,其中包含已格式化為編號或點符列表的選項清單。 |
| suggested |
傳回 'message' 活動,其中包含已新增為建議動作的選項清單。 |
| to |
採用混合的 |
方法詳細資料
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
目前交談回合的通道標識碼或內容物件。
- 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)
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[]。
如果 Choice 具有 Partial<CardAction>的 Choice.action,.toChoices() 會嘗試填入 Choice.action。
const choices = ChoiceFactory.toChoices(['red', 'green', 'blue']);