Connecting ChatGPT and Directual scenarios

Pavel Ershov
Directual
Published in
3 min readMar 13, 2023

In this lesson, we’ll learn how to use the Open AI ChatGPT plugin with Directual no-code scenarios, to create a Telegram bot.

The video tutorial

Step 1. Connecting Telegram-bot to your app.

Go to BotFather to create new Telegram bot. You can then either create a new app using Telegram bot template or install the Telegram plugin in your existing app.

Step 2. Installing ChatGPT-plugin

First, go to OpenAI and create an account (EU/US telephone number is needed). Then, get the API key. Once you have the key, you can install the OpenAI plugin.

Step 3. Launching a scenario for message processing

You can explore the techniques for building Telegram bots on Directual in our documentation. Here we’ll just play around one scenario that is set to trigger on every new object in TMessageIn data structure.

Steps configuration:

  1. Start step. One context variable: response (type of string or file, doesn’t matter)
  2. Telegram step. Just an action of typing. OpenAI API usually processes the request for a few seconds. The bot will be “typing…” that time.
  3. Condition step. Checking if user wants to have a picture generated. We use regular expression here.
  4. Text-to-image ChatGPT call. We send text from telegram message to OpenAI and save response to a context variable.
  5. Telegram step. Send a generated picture to the user.
  6. Text-completion ChatGPT call. We send a prompt to ChatGPT (including the chat history, that we store on TChat object, in a certain string field).
  7. Telegram step. Send a generated text to the user.
  8. Edit object step. Add user’s request and bot’s response to chat history.

Step 4. Adding history reset

We’ve added field for storing chat history on TChat object

Storing chat history on TChat object

Let’s add a condition to the scenario if message text equals “/start”, clear the chat history and start from the beginning.

Afterword

That is it! The dialogue with the bot may look like (it gets the context):

If you have any questions, shoot us a message on hello@directual.com. Also, don’t hesitate to join Directual community of no-coders.

Originally published at https://www.directual.com.

--

--