Now4real Chatbot API

You can seamlessly integrate your chatbot into a Now4real pagechat. This integration enables automated interactions with your users, boosting engagement and enhancing the overall quality of conversations.

How does it work?

For each new chat message, you can receive a notification through a REST call, enabling you to respond promptly and effortlessly continue the conversation.

Implementing the chat service, which acts as the REST endpoint for incoming notifications, is straightforward. You can build it using your favorite framework and deploy it on your own infrastructure or via a serverless architecture. Your service has the freedom to process chat messages in any way you choose, and then respond accordingly, including generating AI-powered replies tailored to your needs.

API request and response overview

Now4real will perform a simple HTTPS call, providing all necessary data within a plain JSON object. You can use HTTP for testing purposes, too. The request includes essential information for understanding and processing the request.

This information includes:

  • context: The context helps determine the chat page the user is interacting with.
  • chat: All messages visible to the user, including the message the user is responding to.
  • newMessage: The content of the message that triggered the API call.

In return, your service needs to respond with a JSON object containing the reply to the newMessage. However, it’s also possible to send an empty JSON object if you don’t need to provide a response.

The response JSON consists of three main sections:

  • user: The chatbot’s identity, representing the entity replying to the user.
  • newMessages: The messages the chatbot will send in reply.
  • suggestions: Quick response options for the user.

Now, let’s take a closer look at the specification.

A detailed look at the API request

When Now4real receives a message, it gathers all the necessary data to build the JSON request, which is then sent to your endpoint via an HTTP POST.

Below is an example of the request format:

{
  "context": {
    "site": "now4real.test",
    "page": "/chatbot"
  },
  "chat": {
    "messages": [
      {
        "id": "m1",
        "time": "2000-01-01T12:01:00",
        "user": {
          "id": "hfre1",
          "displayName": "Adam",
          "jwtSub": "user1",
          "authProvider": "JWT"
        },
        "content": "Can you help me?"
      },
      {
        "id": "m2",
        "time": "2000-01-01T12:02:00",
        "user": {
          "id": "pungobg",
          "displayName": "Chat Bot",
          "jwtSub": "bot",
          "authProvider": "CHATBOT"
        },
        "content": "Sure!"
      }
    ]
  },
  "newMessage": {
    "id": "m3",
    "time": "2000-01-01T12:03:00",
    "user": {
      "id": "hfre1",
      "displayName": "Adam",
      "jwtSub": "user1",
      "authProvider": "JWT"
    },
    "replyMessageId": "m2",
    "content": "Suggest me something"
  }
}

The context section identifies the chat where the user’s attention is focused, with the site (e.g., “now4real.test”) and page (e.g., “/chatbot”), corresponding respectively at the Site Context and Page Context of the web page. If the chat scope is the entire site, the page is always represented as “/”.

The second part is the chat section. Here, all visible chat messages are available to the user, providing a meaningful representation of the user experience.

The chat section contains all visible messages. Each message includes:

  • id: A unique identifier for each message, useful for replies.
  • time: The timestamp in ISO 8601 format.
  • content: The actual message text.
  • user: The sender’s information, including their ID, display name, and optionally the JWT subject (jwtSub), if using JWT authentication.
  • replyMessageId: The ID of the message being replied to, if present.

The newMessage section, the most crucial part, contains the message that triggered the request. This message has not yet been published in the chat, and it includes:

  • replyMessageId: The ID of the message being replied to.
  • content: The content of the message the user is sending, such as “Suggest me something”.

Now, let’s proceed with crafting an appropriate response.

Crafting a response

When Now4real receives a message, it expects a response. Let’s start with the simplest response: an empty JSON object “{}”. You can always reply with an empty response, in which case Now4real will ignore it, and the conversation will proceed as usual.
However, you can use your API response for more than just an empty reply. You can display messages in the chat interface or suggest answers to the user.
Here’s an example of a response:

{
  "user": {
    "displayName": "Chat Bot",
    "displayIcon": "https://example.test/chatbot.png"
  },
  "newMessages": [
    {
      "content": "Chat on Now4real",
      "replyMessageId": "m3"
    }
  ],
  "suggestions": [
    "Great!",
    "Thank you"
  ]
}

To ensure a smooth user experience, it is highly recommended to respond as quickly as possible. Please be aware that the response must be processed within 5 seconds. If it takes longer, the request will time out.

Send messages to the chat

You can use the service’s response to send messages to the page chat. To send a message, Now4real requires two components: a chatbot user and the content of the message.

The user attribute identifies the chatbot and should contain the following properties:

  • displayName: The name of the chatbot, which will be visible in the chat to users.
  • displayIcon: An optional avatar icon URL for the chatbot, which will appear in the chat.

To send new messages, use the newMessages attribute in your response. This attribute accepts an array of message objects. You can send up to 10 messages at once in a single response. If the array is empty, no messages will be added to the chat.

Each message object should contain the following properties:

  • content: The text to be displayed in the chat (up to 1000 characters).
  • replyMessageId: The optional ID of a previous message that this new message is replying to.

Adding quick reply suggestions

To enhance the user experience, you can include up to three quick reply suggestions. These suggestions are displayed to the user, allowing them to select a response with a single click or tap.

Use the suggestions attribute to define an array of suggested responses. Each suggestion can contain up to 100 characters. This feature helps guide the conversation and is particularly useful for frequently asked questions, common queries, or to propose quick responses to the chatbot’s requests (e.g., Yes, No).

By providing predefined options, you can streamline the interaction, reduce user typing effort, and encourage quicker responses. Keep your suggestions concise, relevant, and easy to understand to maximize their effectiveness.

Activating your chatbot in Now4real

To integrate your chatbot with Now4real, you need to configure, test, and activate it through the Now4real Publisher Dashboard. This process is simple and requires a verified site along with a Premium, Business, or Enterprise subscription.

Follow these steps:

  1. Log in to your Now4real Dashboard.
  2. Navigate to the “Chatbots section within your site’s settings.
  3. Toggle the activation switch to enable the chatbot.
  4. Enter your chatbot’s URL into the “Webhook endpoint field.
  5. Click the “Test button to verify the connection.
  6. If the test is successful, click “Publish to confirm and deploy your chatbot.

That’s it! Your chatbot is now live and ready to interact with visitors on your site.

Extra: Leveraging chatbots for automated moderation

By configuring a chatbot on Now4real, you can enable not only automated responses to user messages but also advanced moderation capabilities. The chatbot can analyze user messages and block those that are deemed offensive, off-topic, or in violation of community guidelines. This is achieved by responding with a simple JSON indicating that the message should not be published.

{
  "moderation": {
    "publish": false
  }
}

When a message is moderated:

  1. The user is notified that their message has been blocked.
  2. Depending on your implementation, the system can allow the user to retry sending a message later.

This approach ensures a safe and engaging chat experience by applying moderation rules tailored to your specific requirements.

Add Now4real to your site now

It's easy. It's free.

Enable your visitors to chat with each other. Let them find out the most popular pages.
Spark up instant communities on your website!

© 2024 Now4real Srl. All rights reserved. P.IVA IT10328990964. Illustrations by Freepik Storyset