Skip to content

OpenAI How to create an AI-powered chatbot with OpenAI in WhatsApp?#

To create a chatbot using generative AI and integrate OpenAI into Make, we will need a Green-API account with an instance configured, as well as Make and OpenAI accounts.

Table of Contents#

  1. Setting up the GREEN-API application
  2. Creating a scenario
  3. Setting up receiving incoming messages
  4. Processing the request through OpenAI
  5. Sending the response message to the user
  6. Testing the functionality

Setting up the Green-API application#

Setting up the Green-API application is described in detail in the section Configuring Integration

Creating a scenario#

Scenario is an automated process created in Make.
Each scenario consists of a trigger and an action. When a trigger is triggered in one of the applications, Make automatically performs an action in the other.

Scenario template

You can create a scenario according to the instructions below or use our ready-made template.

Algorithm for creating a scenario:

  • Log in or register an account in Make
  • Click the Scenarios button in the sidebar menu

go-to-scenarios

  • On the scenarios page, click Create a new scenario to create a new scenario

create-new-scenario

  • Scenario successfully created!

After creating the scenario, a plus sign (adding a module) will appear on the Make page.


Setting up receiving incoming messages#

To receive messages in Make, you need to set up a trigger:

Setting up an instance

When using the Watch webhooks trigger, instance setup is automatically installed:

"webhookUrl": "webhookserver make",
"incomingWebhook": "yes"

The order of setting up a trigger:

app

  • Select the Watch webhooks trigger at the beginning of the list

watch-webhooks

  • Click on the added module to open its settings

  • Click Create a webhook to add a webhook

add-webhook

  • Click Create a connection to add a connection to the instance

create-webhook

  • Specify your instance's idInstance and apiTokenInstancevalues

    (Script will receive messages from the number associated with this instance)

make-green-api-form

The Connection name field can be used to name the instance being added (e.g. "work number" or "sales").

This will allow you to easily find the desired account in the list of available integrations when creating.

  • Click Save to save the values.

  • Test by clicking the Run once button.
    If the selected instance has incoming messages and the test is successful, click the magnifying glass to see which fields will be available when configuring the action.

test-trigger

Done! The trigger is set up and ready to use!

Error processing webhook
  • If the OpenAI module receives an error like the one below when running the script, you need to set a filter between the Watch webhooks modules and the OpenAI module

Error

  • In the filter, set a condition where type Webhook must be equal to incomingMessageReceived.

Filter1

Filter2


Processing a request via OpenAI#

This step configures sending a user message to the OpenAI integration to receive a response from the neural network.

  1. Find OpenAI among integrations

    Image with OpenAI application search

  2. To generate a response to the user, select the Create a Completion (Prompt) module

    Image with the "Create a Completion" module selected

  3. Before further configuration, get the Api key and Organization ID from your OpenAI account

    1. Register or log in to your OpenAI account
    2. Go to the Api Keys tab and click the Create new secret key button

    Image with transition to the desired button

    1. Enter the name of the key and click the Create secret key button

    Image with entering data for creating an Api Key

    1. Save the received key

    Image with a window with the Api Key

    1. In addition to the Api Key, you need the Organization ID. It is located on the General tab

    Image from the General page, where the Organization ID is specified

  4. Enter account data in the module

    1. Click the Create a connection button

    Image with the button to create a connection

    1. Paste the data into the API Key and Organization ID fields obtained in step 3

    Image with the entered data for connecting to OpenAI

  5. The next step is to select a model that will generate a response to the user. Each model from the list has its own advantages and disadvantages, which can be found in the official documentation.

    For our purposes, the "gpt-4o-mini" model is suitable, as it offers high quality answers with fast generation speed and low cost

    Image with model selection for response

  6. Create a request to the model. At this stage, you need to select a role and its message

    In this example, we will create a request with the Developer / System role to pass the username and dialogue format to the model.

    What are roles and what is the difference between them?

    When using the OpenAI API, you may encounter the fact that when forming a request to ChatGPT, the request has a "Role". What is the difference between them?

    • A request with the Developer / System role is a request directly to the language model, setting its context and behavior. For example, you can make the model's tone more formal or provide additional information on the basis of which the model will form a response

    • A request with the User role passes a user request to which the language model responds

    choose a role

    1. To do this, click on the Add message button

      Image with a button for creating a message with a role

    2. Choose a role. First, let's create a Developer / System role with additional instructions for the model

      Image with the "System" role selected

    3. Fill in the message by mentioning the username based on the senderName

      Image with the filled system message for the model

    4. Create a second request from the User role, passing the variable with the user message in the Text Content field

      Since the message text can be passed in different fields depending on the message type, it is recommended to specify the condition:
      "if textMessageData: textmessage is not empty, then use it; otherwise, use extendedTextMessageData.text"

    Image with filled user message for the model

Before moving on to the next step, you need to send a message again to the number linked to the instance for further configuration


Sending a response message to the user#

  1. Create a new "Send Message" module from the Green-API integration

    Image with the "Send Message" module selected

  2. As a recipient, select the sender from the incomingMessageReceived notification received in the Watch webhooks module

    Image with the sender's ChatId selected as a recipient

  3. The text message will be the response from the language model. Select the Result variable from the Create a Completion module

    Image with the OpenAI result selection as a response message

  4. Launch automation by clicking the switch in the corner of the screen

    Image with the scenario switch enabled


Testing the work#

To test the automation, it is enough to send a message to the number linked to the instance. In a few seconds, a response message will be sent in response, containing a response from ChatGPT, and in Make you will be able to see the successful execution of the automation.

Image of successful automation

The personal AI chatbot for WhatsApp to communicate with users is set up and ready to go.


Examples of using the GREEN-API application#

How to make messaging between WhatsApp and Slack

How to make messaging between WhatsApp and Discord

How to use Google Sheets for organizing mailings in Make

How to make messaging between WhatsApp and Google Sheets