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

DialogFlow中fulfillmentText与fulfillmentMessages区别及多平台聊天机器人开发难题

Hey there! Let's break this down step by step to help you out with your chatbot setup.

DialogFlow中fulfillmentText与fulfillmentMessages的区别

These two fields serve distinct purposes when sending responses from DialogFlow to your chat platform:

  • fulfillmentText: This is a single, plain-text response. It's the most basic, universal text fallback that works across all chat platforms. Think of it as a default message—if you don't have platform-specific responses configured, this is what gets sent. It’s a simple string, and some platforms may prioritize it if no other messages are specified, but it’s best used for straightforward text scenarios.
  • fulfillmentMessages: This is a flexible array of multi-type, platform-specific responses. You can include text, cards, images, quick replies, and more, and even tailor each entry to a specific platform (like Hangout Chat, Facebook Messenger, etc.) using the platform field. For example, you can define a card layout optimized for Hangout Chat, and a different one for WhatsApp. This is the better choice if you need rich, channel-specific interactions—especially important since you plan to migrate to other platforms later.
解析DialogFlow响应并处理特殊内容(数学公式/符号)

Since you’re using Node.js and targeting Hangout Chat (with future migration plans), here’s how to handle response parsing and special content:

1. Parse DialogFlow Responses for Hangout Chat

Instead of relying solely on fulfillmentText, prioritize extracting platform-specific messages from fulfillmentMessages—this will make your migration to other platforms smoother later. Here’s a sample Node.js function to do this:

// Assume dialogflowResponse is the object returned from DialogFlow's API
const processDFResponse = (dialogflowResponse, threadName) => {
  let responseText = '';
  let chatCards = [];

  // Filter for Hangout Chat-specific messages
  const hangoutSpecificMsgs = dialogflowResponse.fulfillmentMessages?.filter(msg => 
    msg.platform === 'PLATFORM_HANGOUTS_CHAT'
  );

  if (hangoutSpecificMsgs?.length) {
    // Extract text content (DialogFlow stores text as an array)
    const textMsg = hangoutSpecificMsgs.find(msg => msg.text);
    if (textMsg) {
      responseText = textMsg.text.text.join('\n');
    }

    // Convert DialogFlow cards to Hangout Chat's card format
    const cardMsgs = hangoutSpecificMsgs.filter(msg => msg.card);
    chatCards = cardMsgs.map(card => ({
      header: { title: card.card.title },
      sections: [{
        widgets: card.card.buttons?.map(btn => ({
          textButton: {
            text: btn.text,
            onClick: { openLink: { url: btn.postback } }
          }
        })) || []
      }]
    }));
  } else {
    // Fallback to fulfillmentText if no platform-specific messages exist
    responseText = dialogflowResponse.fulfillmentText || "Sorry, I couldn't understand that.";
  }

  // Return formatted response for Hangout Chat
  return {
    thread: { name: threadName },
    text: responseText,
    cards: chatCards
  };
};

2. Handle Special Math Symbols & Formulas

How you handle this depends on the type of content and Hangout Chat's capabilities:

  • Unicode symbols (∑, π, x²): These work natively in Hangout Chat as long as your Node.js backend uses UTF-8 encoding (which it does by default). You can pass these directly in the text field without extra processing.
  • LaTeX formulas ($E=mc^2$): Hangout Chat doesn’t natively render LaTeX. You have two solid options:
    • Convert LaTeX to images using a Node.js library (like latex-to-image), then insert the image URL into a Hangout Chat card.
    • Fall back to plain-text representations (e.g., "E equals m times c squared") or wrap LaTeX in code blocks ($E=mc^2$) so users can recognize the formula structure.

内容的提问来源于stack exchange,提问作者0xC0DED00D

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:05:49