Build an AI Assistant with n8n and Telegram (No Code) β
n8nAgingπ
2024-12-16
Build Process β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BUILD CHATBOT WORKFLOW β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PLANNING β
β ββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Define βββββΊβ Choose βββββΊβ Setup β β
β β Scope β β Stack β β Project β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEVELOPMENT β
β βββββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Build βββββΊβ Add AI βββββΊβ Test & β β
β β Features β β Logic β β Debug β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEPLOYMENT β
β ββββββββββ β
β βββββββββββββββ βββββββββββββββ β
β β Deploy βββββΊβ Chatbot β βββ LIVE! β
β β to Cloud β β Running β β
β βββββββββββββββ βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββKey Components β
- Project architecture
- Core features implementation
- Best practices
Transcript β
[00:00] Imagine having your own AI assistant that can research topics, plan content, schedule appointments, read your emails, and help you 24-7. And you can access it from anywhere using Telegram. Let's say something like, "Hey Lucy, what's my name?" Our agent is able to dynamically retrieve our name from Telegram. This assistant is able to retrieve real-time information from the web. For example, what is the current weather in Cape Town? I personally like to use these assistants to perform research for my YouTube
[00:31] channel, which is an AI-based channel. So I could ask something like, "What happened on day 7 of the 12 days of OpenAI?" And if you're not familiar with the 12 days of OpenAI, it's simply an event that's currently ongoing, and at the time of recording, day 7 was the last day of the event, and we can use projects to organize our conversations within the chat GPT interface. And if we look at the response from our agent, we can see that on day 7, this enhanced organization feature was indeed released. So this is correct.
[01:03] We can then ask our assistant to draft some content for us, and as a YouTuber, I could ask her to come up with a script outline and title ideas. So I could say something like, "Let's create a YouTube video on this new organizational-slash-projects feature." And what our agent is doing in the backend is actually performing further research and generating titles and a script outline based on some very specific examples and system prompt that I provided. Let's have a look at the result. Here we can see some title ideas, and if we scroll down, we can also see
[01:34] the script outline, which contains the hook, the intro, as well as very specific talking points within the body. In this tutorial, I'll show you how to build this using N8N and Telegram. We'll create a YouTube content research assistant, but you can modify this for anything. Social media management, market research, or even personal task automation. Don't worry if this sounds complex. I'll break down every step, and you don't need any coding experience either. Before we create our assistant in N8N, we
[02:04] first need to set up our chatbot in Telegram. So in the description of the video, you'll find the link to this page, and all you have to do is click on "Start Bot". You can then start a conversation with BotFather, either in the Telegram app or on the Telegram website. Both will work. First, click on "Start", and this will show us all the available actions in BotFather. What we want to do is create a new bot, so either we can click on this link, or in the message, just send "New Bot". Now we can give our bot any name.
[02:36] I'll just call it "Max". Now we need to give our chatbot a username, and this needs to end with the word "bot". So I'll enter "Max_YouTube_bot", and it seems that this name has been taken, so let's try something else, like "Max Personal Assistant Bot". And there we go. We now have our chatbot created, which we can access by clicking on this link, but before we do so, we need to copy this API token. So go ahead and copy this token, and store it in a safe place.
[03:06] And be sure to use your own token, as I will delete mine after the recording of this video. We can now access our chatbot simply by clicking on this link, and let's click on "Start" to start a new conversation. Now at the moment this doesn't do anything, but in a minute we will create our NNN chatbot, which will receive our messages and respond back to Telegram. But first things first, we can also change the avatar image by clicking on "Edit", then we can click on this button to upload an avatar image. I'll simply select this image that I
[03:37] generated with AI, and let's save these changes. Great! Now let's move on to creating the NNN chat flow, which is effectively the brains behind this chatbot. It's important to note that you cannot use a local instance of NNN to make this work. There's no way for Telegram to send or receive messages from an NNN instance that's running on your own machine. There are many different ways to host NNN in the cloud. You could pay the 20 euros per month for the NNN Managed Cloud Service, or you could self-host NNN yourself,
[04:08] which is way cheaper. And this takes me to the sponsor of this video, RepoCloud. With RepoCloud, you can deploy NNN with one click. At only $3 per month, RepoCloud is the most affordable way to host NNN instances. And the best part, you get free credit when signing up, which should be enough to cover your first month. In the description of this video, you'll find a link to the NNN one-click deployment on RepoCloud. Simply click on "Deploy Now", then "Create Your Account", then let's give our project a name.
[04:39] I'll just call mine "NNN Cloud". Let's click on "Deploy App", and this should take a minute or two to deploy. Once deployment completes, you can click on this link to access your NNN instance. But one important note if you are using a custom domain is to update your environment variables as well. All you have to do is go to "Edit Environment Variables", and in Webhook URL, replace this value with your actual domain and save the changes. Keeping your NNN instance up to date is very easy as well.
[05:10] On this page, simply go to "Rebold", and this will pull the latest version from NNN and deploy it to RepoCloud. We can finally move on to creating our Assistant. If it's your first time using NNN, simply complete this form, and that will bring you to the NNN dashboard. The first thing I'm going to do is go to "Settings", then under "Personal", I'm going to change the theme to the dark theme. And let's kick side. Right now, back in our dashboard, let's create a new flow from scratch, and let's call this "Max Personal Assistant".
[05:41] And as a naming convention, I do like to add a little emoji just to indicate that this is an Assistant. And for the trigger, let's search for "Telegram", and under "Triggers", let's select "On Message". Let's create our new Telegram credentials, and then let's paste in a token that we created earlier using Botfather. Let's save this. We can upload this popup, and we can actually go back to the canvas. We can test our integration with Telegram by clicking on "Test Workflow". Now, this is waiting for a message from Telegram, so if we go back to Telegram,
[06:14] we can send the message like "Hello", and going back to NNN, we can see that this was successful, and we can also see the message that we received. So we get things like the message ID, the sender's first name and last name, and we can also get the text, which was "Hello". Let's add another node. This time, let's go to "Advanced AI", and select "AI Agent". For the agent, we will leave this on "Tool Agent", but for the prompt "Source", we'll actually change this to the "Find" below. And for the text, let's grab the text that we received from
[06:45] the Telegram trigger. Now, before we can use this, we do have to assign an "AllyleM" model, which is effectively the brains behind this node. And for this demo, I will be using OpenAI, but you're more than welcome to use any other provider of your choice. However, I do recommend that you assign an intelligent model, something that's at least at the level of GPT-4 or greater, as this agent will be responsible for calling multiple tools, and it needs to have a large context window. So I'll click on "OpenAI", let's set up
[07:15] the credentials, and to get the API key, go to platform.openai.com and click on "API Keys". Create a new key by clicking on this button, let's give it a name like "N8N Personal Assistant Demo", create the key, copy this value, and add it to "N8N". Click on "Save", and if everything was set up correctly, you should receive this message. We can now close this popup, select the credentials, then for the model, I'll be using GPT-4.0. As I mentioned, you do need an
[07:46] intelligent model to create these assistants. So let's run this node by itself, and let's have a look at the response. This is saying, "Hello, how can I assist you?" Great! We can also go ahead and send our responses back to Telegram by clicking on this button, let's search for "Telegram", then under "Actions", let's select "Send a Text Message". We can leave all of these defaults, but for the chat ID, let's go back to our Telegram trigger, and let's add this chat ID. Now for the text, we can actually go to
[08:16] our AI agent and select this output. Let's click on "Test Step", and we get our response back from Max, and it looks something like this. But we also get this extra text which we don't want. So to remove that, go to "Add Field", select the "PaintN8N" attribution, and simply disable it. Let's test this step again, and now we receive our response without this extra text. Let's test this end-to-end. Let's click on "Test Workflow", and let's say, "Why is the sky blue?" And we instantly receive our response
[08:48] back. How cool is that? Now if you're not familiar with "N8N", it's really a fantastic tool. You can build extremely complex workflows with this. So you can include "Rag", you can call several tools, you can even call other workflows as tools as well. So it's almost impossible to cover everything in a single video, but I am going to introduce you to a few cool concepts. We will add memory to this assistant, so it will be able to recall information from our past messages. We will assign tools to this agent as
[09:19] well, which will allow it to go online and perform research using real-time data, and we will also allow it to call another flow within "N8N", which will create our YouTube titles and scripts for us. But using these principles, you can build pretty much any automation that you want. Now let's start by adding memory to this agent. At the moment, if I send a message like, "The password is N8Nrocks", and what I expect it to do then is if I ask it, "What is the password?"
[09:49] Like, "What is the password?" It's supposed to repeat that password back to me, but it can't give us that password because it's got no view of previous conversations. So to fix that, we can add memory to this agent, and let's simply add this window buffer memory node. Now the limitation with this node is all this data will get lost if we had to restart our server, but this is fine for tutorial purposes. In production, you might want to use a persistent database like one of these other options. Let's select this buffer memory node.
[10:21] Then for the session ID, let's change this to define below. Then we can change the input to mapping, and under the Telegram trigger, let's add the chat ID. That is a unique identifier for this conversation that we're having with our assistant. We can also change the amount of messages that should be returned. I think five is too little, so I'm going to change this to, say, the last 20 messages. We can now save this, and I'm actually going to start a new conversation, and let's say, "My name is Leon",
[10:52] it stays this workflow again, this time it's asked, "What is my name?", and it's able to recall my name because of this memory node. Let's assign tools to this agent. Now, when we click on tools, we can see a lot of predefined tools with nn8n. So we can hook up our agent to Gmail, and this will allow the assistant to read and respond to emails. Or we can use the calendar tool to allow the assistant to read or even create calendar entries for us. Now, the most obvious tool that we might
[11:23] want to assign is the calculator tool, as LLMs are generally not very good at performing math. Now, here's a little pro tip for you. Whenever adding tools to an AI agent, I highly recommend changing the system prompt of the agent to make it aware of that tool, and when and how to use that tool. To add a system prompt to an agent, go to options, click on add option, and select system essay. This will start us off with some very generic system essay. I'm actually going to expand this, and
[11:54] now we can add to it, like you are a helpful assistant called Max. And I also like to use markdown to make it very clear to the assistant what each of these sections are about. Let's also say you are talking to, and now we can dynamically pass in the name of the user, and we can get that from the Telegram trigger. So let's pass in first name, like so. I also like to pass in some additional context, for instance the current date and time. So let's enter current date and time is
[12:26] equal to, and now we can add these double curly braces to pass in some JavaScript, and we can select this now property, which will inject the current date and time. Now, for the tools, we can add another section here called tools overview. Let's start off by typing something like, you have access to the following tools. Then we can use these double hashtags to indicate that we are now one level lower. It's almost like an h2 tag, where this represents an h1 tag. The first tool is the calculator tool.
[12:56] It's the calculator tool, and now we can just simply say when and how to use this. Use this tool to perform any math calculations. Now we will add more tools to this in a second, but I just wanted to show you how to logically build up these system prompts as you're building these agents. And of course passing in the current date and time is extremely valuable, especially when we ask it questions like retrieving the latest news or the current weather. Now let's move on to adding research capabilities to this agent. This research capability will allow our
[13:27] assistant to go online to retrieve information and formulate a response. Now of course we could simply go to tools and add something like the SERP API tool, which will allow it to perform a Google search. But I want my research capabilities to be way more advanced than simply calling a single tool. So therefore I'm going to create a separate agent that's completely responsible for doing research and then passing the results back to our personal assistant. So our personal assistant will kind of
[13:58] act like a supervisor agent that's able to add tasks over to other agents and then work with their responses. So let's go back to our dashboard. Let's create a new agent. So we'll just create a new workflow. Let's rename this to research agent. Again, I'm going to give it my little robot icon. Now for the trigger, let's select when called by another workflow. I do recommend setting mock data at this point in order to test this flow by itself. To do that, we can click on edit, then we
[14:30] can actually remove almost all of this. We just add back that curly brace and let's remove this comma. Now when another flow calls this flow, it will pass in a query parameter. And within this we can pass in something like latest news on llama 3.3. Let's save this and let's go back to the canvas. Then let's add a new action. Let's go to advanced AI. Let's select an AI agent and for the prompt source, let's change this to define below and let's
[15:00] add our query to this text. Let's also add a system message and for this message, let's enter role. Your role is to research the provided topic using the provided tools. Let's close this popup. Let's go back to the canvas. Let's assign a chat model. So again, we'll select open AI. We can actually use the GPT for our mini model since we won't be assigning too many tools to this agent. We also don't have to manage memory as this is a once off call. Now for the tools, I want to give this agent access
[15:32] to a few tools actually. First is the wikipedia tool and we don't have to set anything on this tool. It will just work. Let's add another tool and this time I'm going to add hacker news and for the resource, let's change this from a single article to all, which will allow us to perform a search query on hacker news. So we can leave the operation on get many and let's specify a limit like the last five articles. And under additional fields, let's add keywords and from the keywords, we want
[16:03] to extract the most relevant keywords from the query that this agent received. Now this query could be anything. It really depends on what the supervisor decides to pass into this flow, which might not be ideal. And we do not want it to pass in paragraphs of information to perform a search query. Instead, we want our agent to intelligently extract the keywords from whatever the sentence is to perform a search on hacker news. And that brings us to a super useful feature in N8N. Let's go to expression
[16:34] and within expression, let's enter our double curly braces. Now we can select from AI. We can use from AI to tell our agent what information to extract from the prompt and also how to extract that information. So for instance, in quotes, we can tell it what to extract. So I'll simply enter keywords. But if you feel like this is not descriptive enough, you can add another property. And in here, you could describe how the agent should identify this information from the prompt.
[17:06] For instance, the keywords that can be used to perform a query on Hacker News. Now let's go back to the canvas and let's add one more tool and specifically the SERP API tool, which will allow our agent to perform a Google SERP. Under credentials, let's click on create new credential. And for the API key, go to SERP API.com and sign into your account. Then simply copy your private key and add it to N8N. Let's click on save and let's close this pop-up. And that is actually all we have to do.
[17:37] Let's test this workflow. And if we have a look at the response and then the output, we do see our research results. Let's save this flow. Then let's go back to our personal assistant and we can now call another workflow as a tool by going to tools. Let's select call N8N workflow tool. Let's give it a name like research agent. Let's also rename this tool. And in the description, we can tell the agent what this tool is about. Use this tool in order to perform research on any given topic. Well, if the source is database and under workflow, we
[18:10] can select our research agent. Now let's go back to the canvas. And as I mentioned earlier, I highly recommend adding any tools to the system prompt of our supervisor agent. So here I'll add the name of the tool, which we called research agent. And I'll enter something like use this tool to perform research on any given topic. Pass a detailed query to this tool in order to perform the best possible research. Let's save this and let's run test workflow. Now let's ask Max something like what is the latest news on llama 3.3? And we can
[18:41] see that the research agent is being triggered. And now open AI is formulating a response. And in telegram, we can see our response, which contains all of our research results. Great. Now in order to practice the skill, let's create one more agent, which will be responsible for coming up with YouTube titles and drafting a script outline. Of course, in your application, this could be anything you might want to store the results in a database somewhere or perhaps draft a social media post. Let's go back to our dashboard. Let's
[19:12] create a new workflow. Let's call this YouTube research for the trigger. Let's select when called by another workflow. Let's click on edit to add some mock data. So I'll simply change this to query and for the value, let's enter create a YouTube video on llama 3.3. Let's remove this comma and let's save this. Then back to the canvas. Let's add our AI agent. Let's change the prompt source to define below. Let's simply add our query. And what I
[19:44] want this agent to do is to perform additional research that's specific to formulating a YouTube video. So under add options, let's add a system message, go to expressions, let's open this up and let's enter role. And for the role, I'll enter your role is to perform in depth research based on the provided topic. This information should be sufficient to create a YouTube video on it, whether it's a tutorial news or reaction video. Write a complete report on the research that you perform. Let's run this and we
[20:15] can see our agent is performing research and looking at the results. We can see our research results over here. Now the next thing I want to do is to generate the script outline as well as title ideas. Now we could add everything to the single agent, but a longer context window could mean worse results. So sometimes it's better to split this responsibility. So I'll create a new node. Let's go to advanced AI and we don't need an agent. All we need for this is a basic allyl mchain. Let's rename this to script outline for
[20:45] the prompt source. Let's click on define below. Let's grab the value from the previous agent and let's add a new prompt. And in the system prompt, let's enter your role is to come up with a YouTube video script based on the provided query. This video can be news or a tutorial based on the research. Respond with the following structure. So we just want to get the look back, the intro and the body outline. Great. Now let's go back to the canvas. Let's add a model to this node or grab the open AI chat model. GPT40 mini should
[21:17] be fine for this. Then it's actually add another node. Let's go to advanced AI. Let's select basic allyl mchain. I'll literally name this to title generator. Let's click on add prompt and in the system prompt, let's enter your role is to come up with high click through rate titles based on the description of the video. Of course, it's changed the prompt source to define below. Let's execute the previous nodes just to get some values back and let's grab the text generated by the script outline agent
[21:47] and let's add our AI model. So we'll go to open AI. We can leave it on GPT40 mini and we should be able to run this node as well, giving us our YouTube title ideas. Finally, let's add everything together by adding this set node. Let's add a new field. We can just call this one response and for the value, let's create an expression and let's say possible titles and let's add the text from the title generator and just below
[22:18] this, let's add script outline and let's grab the script outline like so. All right, let's close this pop up. I'm just going to rename this mode to format response. We can save this flow and we can test it end to end. So our agent is performing its research. It's generating the script outline. It's creating our title ideas and let's have a look at the response. And yes, that's everything we want. Now let's go back to our personal assistant. So we'll pick on Max. Let's add a new tool. Call N8N workflow tool. Let's call
[22:51] this YouTube agent and let's also rename this node to YouTube agent. For the description, we can enter. Call this tool when asked to come up with a YouTube video idea based on the research topic and from our workflows, let's select YouTube research. Great. So what we can do now is activate this workflow. We can just click on got it and telegram will now constantly be listening for messages from N8N. Let's try this. So let's enter what happened on day five of 12 days of open AI. And if we go back to
[23:23] N8N, we can go to our research assistant as I am expecting this assistant to run. We can see in this workflow trigger that this query was passed in. So open AI is 12 days of open AI event day five activities and going back to telegram, we can indeed see that the correct information is coming back. So now we can ask a follow up question like let's create a YouTube news video on this. Let's go back to N8N. Let's go to YouTube research. Let's click on executions. And
[23:53] indeed we can see that the YouTube research assistant is now running. Let's have a look at the execute workflow trigger. We can see this detailed query being passed in as well as all the information needed for our YouTube video like the titles as well as the script outline. Now again, I am a big believer in adding these tools to the system prompt to get the best possible results. So in this agent, let's go to our system message and under tools, let's add the YouTube agent tool. What I noticed with my own agent is that it will sometimes
[24:25] make changes to the script. So our assistant will take the script that was generated by the original YouTube agent and then for some reason summarize it or make changes to it. But what I really want is just to grab the original content from that YouTube agent and pass it straight back to the user. Therefore in this tool, I can say use this tool when asked to plan or create YouTube videos. When using this tool, be sure to return the results in full back to the user. Do not edit the response in any way.
[24:56] As the tool will return the exact response that the user expected. I just want to thank repo cloud for making this video possible. And if you would like to learn more about using N8N to automate your life, then check out my N8N beginner series over here. I'll see you in the next one. Bye bye.