Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.5 KB
Raw Blame
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { wonderchatAuth } from '../..';

export const askQuestion = createAction({
  name: 'askQuestion',
  classification: 'WRITE',
  displayName: 'Ask Question',
  description:
    'Sends a question to a Wonderchat bot; returns the bot’s answer.',
  audience: 'both',
  aiMetadata: {
    description:
      'Sends a question to a specific Wonderchat chatbot (identified by chatbotId) and returns the bot’s generated answer. Use to query a deployed knowledge-base chatbot programmatically. Optionally pass a chatlogId to continue an existing chat session plus custom context/contextUrl to ground the answer. Not idempotent: each call posts a new message and produces a fresh AI-generated reply.',
    idempotent: false,
  },
  auth: wonderchatAuth,
  props: {
    chatbotId: Property.ShortText({
      displayName: 'Chatbot Id',
      description: 'The ID of your chatbot (can be found in the URL when viewing your bot: /bot/YOUR_BOT_ID)',
      required: true,
    }),
    question: Property.LongText({
      displayName: 'Question',
      description: 'The question you wish to ask your chatbot',
      required: true,
    }),
    chatlogId: Property.ShortText({
      displayName: 'Chatlog Id',
      description:
        'The ID of your chat session (can be found under Chatlog Details section, labeled as "ID")',
      required: false,
    }),
    context: Property.LongText({
      displayName: 'Custom context',
      description: 'Additional custom context about the chat session (e.g., user information)',
      required: false,
    }),
    contextUrl: Property.LongText({
      displayName: 'Context URL',
      description: 'URL of the page the user is on to provide additional context',
      required: false,
    }),
  },

  async run({propsValue}) {
    const { chatbotId, question, chatlogId, context, contextUrl } = propsValue;

    const requestbody: any = {
      chatbotId,
      question,
    };
    if (chatlogId) {
      requestbody.chatlogId = chatlogId;
    }
    if (context) {
      requestbody.context = context;
    }
    if (contextUrl) {
      requestbody.contextUrl = contextUrl;
    }

    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: 'https://app.wonderchat.io/api/v1/chat',
      headers: { 'Content-Type': 'application/json' },
      body: requestbody,
    });
    return response.body;
  },
});