> For the complete documentation index, see [llms.txt](https://help.iviva.com/lucy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.iviva.com/lucy/my-work-folders/workflows-and-integrations/creating-a-workflow-to-run-when-a-message-from-a-chatbot-is-received.md).

# Creating a Workflow to Run When a Message from a Chatbot is Received

You can create workflow automations linked to popular messaging apps. This way, users can interact with Lucy through their favorite chat app, while the system automatically handles tasks whenever a message is posted in a chat.

Lucy now supports creating bots for Telegram, the widely-used messaging platform. Easily register a new bot and integrate it with a Lucy workflow for seamless bot operations.

Let's create a simple chatbot that will receive requests from clients, store them in a database and send a confirmation email to the client after the request is received.

Here are the steps:

1. Creating a Database
2. Creating a Workflow
3. Testing the Workflow

## Creating a Database

We'll create a database manually.

1. On the app **homepage**, go to the **My Work** tab on the left sidebar.
2. Click on the required folder to open it.
3. If you cannot locate the folder in the list, type in the folder name under **Search Folders.** From the search results, click on the desired folder to open it.
4. Go to the **Databases** tab.
5. Click the **Plus** icon and pick **Create Manually** option.
6. Enter a **name** for your database, *e.g., Complaints.*
7. Click **Continue**.
8. Click **Skip this step.**
9. Add required fields.
   1. Click **Add New Field** button.
   2. Type in the **Field names.**
   3. The type of the field will be automatically picked by the system.
   4. Click **Go to next Step.**
10. Click **Create New Database**. The database will be created within the folder.

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FEDs64P9sxkc4NESlq4Vc%2FS1-adding%20fields.png?alt=media&amp;token=7b23f274-606a-4442-933d-e890fceb60a7" alt=""><figcaption><p>Adding Fields to the database</p></figcaption></figure>

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FAUtog35ZBzQ0gK7Szxlj%2FDatabase.png?alt=media&amp;token=9d857ee0-01dd-476e-913f-3d607639eaee" alt=""><figcaption><p>Newly created database</p></figcaption></figure>

## Creating a Workflow

Upon creating the database, we'll create a simple workflow that triggers whenever someone sends a message via a telegram bot.

1. On the app **homepage**, go to the **My Work** tab on the left sidebar.
2. Open the same folder where you created the database.
3. Go to the **Workflows** tab.
4. Click the **Plus** icon and the Workflow Editor page opens.
5. Select **How do you want to trigger your workflow?** box.
6. Select **When a telegram message from bot has been received** trigger.
7. Choose a Telegram bot.
   1. If you have existing bots created, pick the bot from the selection box or you can create a new one.
8. Register a bot. To register a bot, you need to start conversation with the BotFather account.
   1. To create new bot, click **Create a Telegram bot** option.
   2. This opens a instruction window with a video to guide you.
   3. Download the **Telegram** desktop app and sign in to your account.
   4. Add BotFather user.
   5. To start a conversation with the BotFather account, enter the '**/newbot'** command.
   6. Enter a **name** for your bot.
   7. Then provide a **Username** for the bot.
   8. Your bot will be created and you will get a token from the bot as a reply message.
9. Enter your bot details.
   1. On the workflow editor window, open the instructions window.
   2. Under the **Step 4: Enter your bot details** section, enter your Bot Registration Token.
   3. Type in the **Bot Name.**
   4. In the **Bot Handle** text box, provide the username.
   5. Click **Save**.
10. Populate data into the database.
    1. On the workflow editor, click the **plus** icon to add another block.
    2. Select the **Databases and Analytics** category block and pick the **Insert into Database** block.&#x20;
    3. Click the arrow icon![](https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FIdIMwAI7CTuhs43SYz5g%2FArrow.png?alt=media\&token=a4a40d64-9b0c-4d91-a5a9-9137015804d5) and pick the database we previously created.
    4. Pick required tokens for the database attributes.
11. On the workflow editor, click the **plus** icon to add another block.
12. Pick the **Send Email** block.
13. Click the arrow icon![](https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FIdIMwAI7CTuhs43SYz5g%2FArrow.png?alt=media\&token=a4a40d64-9b0c-4d91-a5a9-9137015804d5) and fill in the email details (Enter the Subject, To and Body fields).
14. Provide a meaningful n**ame** for the workflow and save it. Now our workflow is created.

{% hint style="info" %}
All your saved workflows will be listed under the **Workflows** tab within the respective folder.
{% endhint %}

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FMtFjutssF9FClSgCtgny%2FSelecting%20a%20Bot.png?alt=media&amp;token=086aba69-d3e4-494d-be62-f3afed77d5ae" alt=""><figcaption><p>Selecting a Telegram Bot or creating a new bot<br></p></figcaption></figure>

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FpAheV59cTfJvOEjhKX6d%2FEntering%20bot%20details.png?alt=media&amp;token=56579d26-1b18-42e8-b481-2c3128af0b7a" alt=""><figcaption><p>Entering your bot details</p></figcaption></figure>

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FzQUGIkjWwo3TkfCJd1Rx%2FDesigning%20the%20Workflow.png?alt=media&amp;token=cd022649-3311-4539-a19c-6429a63daa17" alt=""><figcaption><p>Designing the workflow screen 1</p></figcaption></figure>

<figure><img src="https://333252015-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTG5DCdGHEfzVksM49xh8%2Fuploads%2FjkPvW1PMEq32ZMxvbBKR%2FDesigning%20the%20Workflow_s2.png?alt=media&amp;token=41a61baf-9374-4810-b18f-e4789d6a5da9" alt=""><figcaption><p>Designing the workflow screen 2</p></figcaption></figure>

## Testing the Workflow

Let's see how our workflow functions.

1. Open the bot in the Telegram app.
2. Send a complaint message.
3. Let's check our database.
   1. Go to the **Databases** tab within your folder.
   2. Click the respective database to open it. You will see the message from the bot has been added to the database.

When a message is sent via the Telegram app, it is added to the database, and send an email to the client.

Watch below video on setting up a chatbot to receive requests from clients and add them into a database. Also, send an email to the client, once the request is received.

{% embed url="<https://drive.google.com/file/d/1mZvpeHfo1DnrV9sIFM33DdXg8UIQYYFO/view?usp=sharing>" %}
