Create a fully-featured chatbot with n8n

Create a fully-featured chatbot with n8n
Photo by Mohamed Nohassi / Unsplash

Agentic AI agents are taking over! I've been experimenting recently with n8n, and the capabilities it enables through it's easy-to-use UI. Everything you see in this article is achieved using their free self-hosted version, but they offer decent packages on their site. I won't go through the process of setting up the self-hosted version here, but it's a relatively simple Docker image to set up!

When you create a new workflow, you're greeted with a blank canvas. This can be a bit intimidating at a first glance, but let's take it step by step.

Clicking on the 'Add first step' button presents a variety of different 'trigger' options. The classic example we'll use here is 'On Chat Message' - but you can run this as a webhook, or use one of the 'hooks' built into n8n to respond to various external services. I've been toying with a Telegram bot, and am considering use-cases for using Notion as a trigger.

Adding the 'On Chat Message' node launches a new window. Here we'll have options to make the chat public, or allow file upload. Making the chat public will allow you to either use a webhook in the @n8n/chat package, or via a hosted page. For simplicity sake we'll use the hosted page. You can also add an auth method at this point, if you want your bot hidden behind either Basic or n8n auth. For any production examples I add Basic auth, with a token shared between n8n and my client application - but for this purpose we'll leave it open! Once you're happy with the 'trigger' setup, click out of this pop-up, or click 'back to canvas'.

Now we're back on the canvas, you'll see an option to open chat. This is great for testing your bot, and you'll see the workflow execute in real-time as you use this. You'll also see after our trigger node there is a little plus symbol. Let's click that - and add in an AI Agent, by going into 'Advanced AI' and then clicking 'AI Agent'.

In our scenario, the setup of the AI Agent node is all we need to do. It'll take the JSON output from the chat trigger, and feed it into the agent. If you were using a different service, you'd have to plumb it into the 'source for prompt' field. n8n makes this incredibly easy once you've been running tests, by displaying all available inputs on the left-hand side of the screen for you to drag-and-drop into the relevant fields.

Returning to our main workflow canvas, you'll see three nodes below the 'AI Agent'. 'Chat Model' is pretty self-explanatory, you are able to bring your own AI credentials into n8n and determine which model you would like this agent to use. I've had a great time working with gpt-4o-mini from OpenAI, but you can use Anthropic, Ollama, Mistral, Groq, etc. Memory allows you to store the chat history in a database, or you can utilise 'Simple Memory' which will store the conversation history in cache during that particular chat session. We'll use that here - as we don't necessarily care too much about persisting our data.

The most powerful part of this AI Agent is the 'tools' section. Here, the 'nodes' of n8n really come into their own. Want your agent to be able to read from your Notion database? Add a Notion tool. Want it to create events in your Google Calendar? Easy with the correct credentials! n8n's true power is in the flexibility of the nodes you can work with as tools. The bot I recently built for a cat rescue used a HTTP request to their WordPress backend to list all the residents currently at their centre, as well as parsing the HTML content of their 'adoption journey' page to ensure the bot knew which cats were up for adoption, and what that process looks like.

Once you've added in your tools (or not, if you want a generic chat bot!) you're good to go! For the case of the built-in chat functionality, you don't need to add anything to the 'output' of the chatbot - but if you're using a different kind of response mechanism (Telegram, Messenger, etc) you'd need to add in a 'send a message' node with the output of the AI agent.

But what if you're not after a fairly generic chat bot? What if you want to give it a bit of personality? Well - you can! Head back into the 'AI Agent' node, and look at the available options. One of those is the 'system message'. If you've been playing with AI agents before now, you'll know that this is the message the agent uses behind-the-scenes to instruct whichever chat model you're using on how to behave. By default, the system message is simply You are a helpful assistant - which gets the job done, but boy is it boring! Again, for the cat rescue, I know the 'voice' they use with their audience in social media posts, and their target demographic. Feeding this information into the system message will help the AI to better tailor each response. Got some rules the bot must follow? Pop them in here! I've included my system message (which could do with some refining still) below:

You are a helpful assistant, working for a cat rescue tasked with helping visitors on the website find the perfect cat for them.

You have access to a database of cats in our care. The name against a cat might be a single cat, or multiple cats in a bonded group. The user may ask for either, so be sure to search thoroughly.

Only the cats with a status of Ready for Adoption are available through the enquiry form on the website. Those listed as Pending Visit have already been reserved by someone else, and those with a status of Long-Term Resident or Not Ready to Rehome you are welcome to talk about, but cannot offer for adoption.

The charity only allows cats to be adopted by people if they're going to be indoor cats, or if the owner has a cat-safe garden or catio. You should make this clear to the user throughout your discussion, so they know before enquiring.

There is also an adoption fee of £85 per cat, this applies to individual cats in a group too. For example, a group of 4 cats would cost £340 in adoption fees.

Once a user has settled on a cat, and submitted the adoption enquiry form - they will need to complete a home visit (virtual, or face-to-face) before we can proceed with their adoption enquiry. The housing co-ordinator will be in touch to schedule this, you don't organise the visits.

You can occasionally use cat-based puns such as 'purrfect' or 'furrever' - along with cat-themed emoji.

This gets all the important rules out of the way for the bot, and allows it to have a fun personality for users to interact with!

Once you've added all of this, you're good to go! The out-of-the-box chat widget is good enough for my use case - and if you're a developer the @n8n/chat JS package gives you embeddable widgets for most frameworks. Just be sure to 'save' your workflow at the top of the page, and 'activate' it to make the webhook/chat form available!