You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于React+BotFramework-WebChat修改聊天机器人默认输入与响应

Hey there! Great to hear you've got the BotFramework-WebChat sample up and running without hitches. Let's break down exactly how to customize the default inputs/responses and add those new features you're looking for—help commands, markdown support, weather cards, and carousel layouts. Here's a step-by-step guide for each:

1. Tweak Default Inputs & Responses

Default Input Placeholder

In your WebChat React component (or wherever you're rendering WebChat), use the inputPlaceholder prop to change the default hint text in the message input box:

<WebChat
  directLine={directLine}
  inputPlaceholder="Type your question here..." // Customize this text
/>

Default Bot Responses

Modify your bot's turn handler (using Bot Builder SDK, e.g., Node.js) to replace the default fallback and greeting messages:

async onTurnAsync(context) {
  if (context.activity.type === ActivityTypes.ConversationUpdate) {
    // Custom greeting when user joins
    await context.sendActivity("👋 Welcome! Try typing 'help' to see what I can do.");
  } else if (context.activity.type === ActivityTypes.Message) {
    const userText = context.activity.text.toLowerCase();
    // Fallback response if no command matches
    if (!userText.includes('help') && !userText.includes('markdown') && !userText.startsWith('天气') && !userText.includes('轮播')) {
      await context.sendActivity("Sorry, I didn't catch that. Type 'help' to see supported commands!");
    }
    // ... rest of your logic
  }
}
2. Add a Help Command

Add a simple check for "help" (or localized terms like "帮助") in your message handler to return a clear list of features:

if (userText.includes('help')) {
  await context.sendActivity(`
    🛠️ Here's what I can help you with:
    - **help**: Show this help message
    - **markdown**: See examples of formatted text
    - **weather [city]**: Check weather for a city (e.g., "天气 北京")
    - **轮播**: View a carousel of sample cards
  `);
}
3. Enable Markdown Responses

BotFramework and WebChat natively support markdown, so just use markdown syntax in your sendActivity calls. For a dedicated markdown demo:

else if (userText.includes('markdown')) {
  await context.sendActivity(`
    ✨ Markdown Format Examples:
    - *Italic text*
    - **Bold text**
    - Ordered list:
      1. First item
      2. Second item
    - Block quote:
      > This is a quoted message
    - Inline code: \`const message = "Hello World";\`
  `);
}

WebChat will automatically render all these styles without extra configuration.

4. Add Weather Cards with Adaptive Cards

Use Adaptive Cards to display weather info in a clean, structured format. First, import CardFactory from Bot Builder, then build the card:

const { CardFactory } = require('botbuilder');

// ... inside your message handler
else if (userText.startsWith('天气')) {
  const city = userText.split(' ')[1] || '北京';
  // Replace with real weather API data in production
  const weatherCard = {
    type: 'AdaptiveCard',
    version: '1.0',
    body: [
      {
        type: 'TextBlock',
        text: `${city} Today's Weather`,
        size: 'Large',
        weight: 'Bolder'
      },
      {
        type: 'ColumnSet',
        columns: [
          {
            type: 'Column',
            width: 'auto',
            items: [{ type: 'TextBlock', text: '🌤️', size: 'ExtraLarge' }]
          },
          {
            type: 'Column',
            width: 'stretch',
            items: [
              { type: 'TextBlock', text: '22°C - 30°C', size: 'Medium' },
              { type: 'TextBlock', text: 'Sunny with light wind', isSubtle: true }
            ]
          }
        ]
      }
    ]
  };

  await context.sendActivity({
    attachments: [CardFactory.adaptiveCard(weatherCard)]
  });
}

Create a carousel using Hero Cards and set the attachmentLayout to carousel in your activity:

else if (userText.includes('轮播')) {
  // Create individual hero cards
  const card1 = CardFactory.heroCard(
    'Sample Card 1',
    'This is the first card in the carousel',
    ['https://your-image-url.com/card1.jpg'], // Replace with your images
    [{ type: 'imBack', title: 'Select Card 1', value: 'Card 1 selected' }]
  );
  const card2 = CardFactory.heroCard(
    'Sample Card 2',
    'This is the second card in the carousel',
    ['https://your-image-url.com/card2.jpg'],
    [{ type: 'imBack', title: 'Select Card 2', value: 'Card 2 selected' }]
  );
  const card3 = CardFactory.heroCard(
    'Sample Card 3',
    'This is the third card in the carousel',
    ['https://your-image-url.com/card3.jpg'],
    [{ type: 'imBack', title: 'Select Card 3', value: 'Card 3 selected' }]
  );

  // Send carousel
  await context.sendActivity({
    attachments: [card1, card2, card3],
    attachmentLayout: 'carousel'
  });
}

All these changes should integrate smoothly with your existing WebChat setup. If you run into any specific issues (like card rendering quirks or command matching bugs), feel free to share more details and I'll help troubleshoot!

内容的提问来源于stack exchange,提问作者partha.sj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:02:28