How to Connect Claude Code to n8n (Telegram Alerts) β
Claude Code TutorialsFreshπ
2025-10-14
Integration Flow β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β INTEGRATION ARCHITECTURE β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β USER REQUEST β
β β β
β βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β βββββΊ β β βββββΊ β β β
β β Telegram β β Slack β β Discord β β
β β β βββββ β β βββββ β β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β β β β β
β ββββββββββββββββββββββββββ΄ββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββ β
β β RESULT β β
β βββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββIntegration Points β
- Core concepts explained
- Step-by-step implementation
- Practical examples
Transcript β
[00:00] Claude code hooks are incredibly powerful and I think a lot of people aren't using it to its full potential. If you're new to hooks, basically what Claude code allows you to do is to run custom code during certain events in the conversation. One very popular use case is to play a sound whenever the conversation ends. So we could implement a stop event and we can then enter some shell command. For example, we could play an audio file whenever the conversation ends, like maybe a little
[00:30] ding sound. But in this video, I'll show you how to take this to a whole new level by integrating Claude code with a process automation tool like N8N. In this example, we can ask the agent to go and make a change for us and we'll receive a notification on our phones when the agent is complete. Now, of course, this doesn't have to stop there. We could even do things like email the results to our Gmail account. We could post a message on Slack. The sky really is the limit here. You'll learn how to
[01:01] build this simple workflow in N8. And we'll go through everything step by step, including the integration with Telegram. Then you'll also learn the fundamentals of setting up and using hooks in Claude code. Sounds good? Let's jump into it. Now, first you might wonder why a solution like this is even relevant. If you've been using aenti coding for a while, you'll know that certain processes can take a very long time to implement, especially if you're using specdriven development using something like specit where the agent runs through an entire implementation
[01:33] plan. And completing all those tasks can take something like 10 or 20 or 30 minutes. Now, you might be tempted to just get up and walk around a bit while the agent does its thing. But did you ever come back to your desk just to see that the agent hasn't done anything and is actually waiting on you for a tool call? Or you might simply be busy with other work and you have no way of knowing whether the agent is still busy or maybe completed its work a long time ago. So with this solution, we can send an instruction and will receive a
[02:03] notification on our machines as well as our mobile phones. Or of course, you can set N8 up to send a notification to Discord or even a Slack channel. By combining cloud code with N8N, you can definitely supercharge your agentic coding workflows. So let's get started. All right, cool. Let's first get a basic understanding of how hooks work in clawed code and afterwards we'll have a look at setting up nin and integrating our workflows with clawed code. So the first thing we want to do is open up
[02:35] claw code. So I've just got a blank project and cool. So to set up new hooks simply enter the command hooks. Then let's press enter. And now we can see all of these different hooks that are available. Each of these events are executed during the conversation flow. So to get an understanding of all the available events and when they're actually triggered, I'll leave a link in the description of this video to this page. So here we have this hook event overview section which shows you all the
[03:06] available events and when they're actually called during the conversation. For this video, we'll actually implement two events. Stop, which runs when the agent completes its work, and notification. This event runs whenever the agent is waiting on your approval for something. This page also goes into a lot of technical detail on how to actually implement these hooks. So, what I like to do, and this is something I teach in all of my other videos as well, is when you find any useful information related to what you're trying to do,
[03:37] then copy that documentation and then back in your project, create a new folder called docs. and then simply create a new file for that documentation. Like this one I'll just call hooks.md. And let's paste in everything that we just copied. This is not a critical step, but it is useful if you ever wanted to reference this documentation when chatting to our agent. So let's select stop. Then let's add a new hook. And now we can provide a shell command. This could be PowerShell commands or
[04:09] bash commands. It's really up to you. But honestly, the easiest solution I found is to write Python scripts. I'll show you in a second. For the command, I'll simply write Python 3. Then I'll simply add some nonsense like ABC and we'll press enter. Then let's select project settings. And that's it. We can actually press escape to go back to the claude code chat. Now what happened is we now have this claude folder along
[04:39] with the settings file. When we open this file, we can see our stop event followed by this command. This means that when the conversation ends, this stop event will be triggered and cloud code will try to execute the command Python 3 ABC, which of course is going to fail. We want to hook up an actual Python file to this. So what we can do is in the claude folder, let's add a new subfolder called hooks. And now within hooks, let's add that Python file. I'll
[05:09] just call it send notification.pby. This also means that we want to now fix this value to link to the actual script. So what we have to do is press front slash which is referring to the root of the project. Within the root we have a subfolder called claude /hooks/ send notification.py. Cool. So let's actually test this. Then let's restart cloud code just to make sure the hook actually updates and let's
[05:41] send something like hey of course the script isn't doing anything at the moment and all right the agent actually said there's an issue with the hook. It says the file can't be found and the agent proactively tried to find the root cause and it said there's a typo in the file name and of course there is. So this should actually be called send notification. All right, let's try this one more time then. So, let's run Claude. Let's say hey. And hopefully the issue is now
[06:12] sorted. We get a response back without any error messages. So, sorry about that, but I think it was still good to see what an error message would look like if you did something wrong. And of course, you can always lean on the agent to help you resolve those issues. So, now that this event is calling this Python script, what can we actually do with it? I've seen these hooks being used for all sorts of things. If you wanted to write some code that will actually interrogate something and then stop the process from executing, for instance, we found some security
[06:43] vulnerability, all you have to do is run the print command. So, we could write a message like stop you forgot to subscribe. And for a second argument, pass in file, which is equal to CIS. Standard error. You'll just have to import cis at the top of the file like so. So this will basically return a message back to the agent. But we also want to tell the agent to actually terminate or prevent the process from continuing. So what we could do is say sis.exit
[07:14] with an exit code of two. Zero will mean that the message will be returned to the agent, but it's safe to continue. But with exit code of two, the process will actually terminate. This process is really useful if you're using something like the pre-tool use event which will run before any tools are invoked. This includes making changes to files. So the content of the files that the agent is trying to edit will be passed to the script. You can see if there's any sensitive information in the new content and if there is write the suitable
[07:46] message and exit with return code too. But of course this is not what we want to do. We want to grab the agent's message and send it off to N8N. So, how do we grab that agent message? There's nothing telling us here what information is actually being passed into the script. Well, the easiest solution I found was to actually use Claude code to assist with this. So, what we can do is simply grab our Python script and then say something like, "Hey, please can you help me figure out what input is being passed into this Python script?" This is
[08:18] being run as part of a claude code event, but I have no idea what the available content is. Biscuit adds some logic to this Python script that will write all the available input into a log file. All right, so Claude code just changed this file. It wrote some logic to grab everything that is receiving and then writing it out to this hook input log file. And in fact while I was talking the stop event was triggered and it created this hook input file. So if
[08:50] we open this we can see a lot of information including environment variables and more importantly towards the end of the file we get this standard in content. Cloud code is basically passing all of its inputs via standard in. So we can see this session ID for the conversation, a transcript path which will play a very big role in a minute and some other information as well. Now this transcript path is really interesting. If I navigate to that folder on my machine and open a
[09:21] transcript file, it actually looks something like this. This might seem overwhelming, but this actually contains all the messages from that conversation. When we scroll to the side, we can see the thinking steps, the text that was generated, like I'll help you figure out what input is being passed. We'll even see things like the tool inputs and everything else. This transcript is incredibly useful for extracting information from the conversation and using it in our workflow. So now that we have this incredibly valuable
[09:52] information, we can ask Claude code to take it a step further. So we can say, "Thank you for that. Please have a look at the log file. In the log file, you'll find a link to a transcript. Please go ahead and read the transcript file to get familiar with it. What we want is the last message that was sent by the assistant. So, grab the last message from the assistant and store it in a variable in our Python script. We'll need to use this later on. All right, let's send this. This is just such a
[10:22] useful little tip. If you don't know what's being passed into a script, simply ask cloud code to dump everything into a log file. So as we can see here, it's actually trying to read that transcript file. So I'll simply approve this. And great, the agent is saying it can see the transcript file. It's a JSON L file where each line is a separate JSON object. I can see multiple assistant messages. Let me modify the Python script to extract the last message and store it in a variable.
[10:52] That's exactly what I want. So, let's say yes. And I'm just going to approve future edits going forward. Okay, cool. So, in summary, the agent extracted the last message and it's storing the value in this last assistant message variable. If we go to the code, I can see the variable over here. And now the code is going to transcript file and it's checking if the entry is an assistant message and eventually it's extracting what we need and passing it to our variable. Cool. Now that we have our
[11:23] agent's last message, we want to send this to NATN so that we can do some very cool stuff with it like send notifications to our phone. Now, of course, for this we need an N instance. Now, of course, you can run N in many different ways. You could run N locally on your own machine, but for a process like this, I would recommend running N in the cloud. That way, it's always available and it can be accessed from anywhere. And integrating N8N with Telegram locally is quite a challenge.
[11:53] In order to set up N online, we do have a few options. You could use their official hosted service which will cost you about β¬20 per month which is not cheap. Look, to be fair, Nate ends hosted service gives you a lot of additional functionality that you won't get with the free version. So, I do recommend giving the free trial period a spin to see if it all works for you. But here's an even better deal. If you go to hostinger.com/leon, you'll end up on this page. You can
[12:23] basically one-click install N8N to your own VPS. And Hostinger is currently running a Black Friday special which gives you up to 64% off on your hosting fees. When I scroll down, you can spin up a KMV1 server for only $5 a month. But I prefer the KMV2 package which is only $649 because of the Black Friday deal. And this gives you a lot of horsepower for your N8N instance. So simply click on choose plan. And of course you can
[12:55] select your term. You will get the best deal on the 24month plan, but you can try it out for only one month, which is still only $10 a month, which is way cheaper than the N8 cloud instance. or you could do 12 months at only $849 or of course my favorite is 24 months. So scrolling down, click on applications and select N8N. This will oneclick install N8N for you. Now under summary, you can get an additional discount by
[13:26] clicking on have a coupon code and entering Leon. When you click apply, this will give you an additional 10% off, which means you're only paying $5.84. 84 cents for your init in instance. This is insane. So, simply click on continue and follow the check out process. You will be asked to enter a root password and maybe one or two other things. But afterwards, you'll see a dashboard similar to this. Now, we don't really care about any of this. What we want to do is click on manage
[13:56] app. And this will take us to our init. So, what we can do is simply complete this form and click on next. Then I'm simply going to skip this form. And I definitely want my free license key. So simply enter your email and click on send me a free license key. Then simply copy that key from your email. And back in N8, go to your settings, click on enter activation key, paste in the key, and click on activate. And that's it. We
[14:27] can now go back to the dashboard and create our N workflows. Let's create a new workflow. I'll rename this to claw code. If you want a complete tutorial on using N8N, then I'll link to my full N master class in the description of this video. So, this is really just a crash course. Every workflow needs to start with a trigger node. When we click on this, we can see all the available triggers. Like we can trigger this manually on a schedule, on a web call. And if we're building AI agents, we can
[14:58] use something like on chat message. For this though, we're going to integrate this workflow with Telegram. So, let's go to Telegram and let's select on message. This means this workflow will be triggered whenever we receive a Telegram message. Of course, we're going to replace this note with a web call in the future, but we do need to test our Telegram integration and extract certain information from this Telegram trigger. Other credentials. Let's create a new credential. And this is asking us for an
[15:30] access token. In order to set up these bots in Telegram, we need to use Telegram's botfather. So you can simply Google it which should take you to this page. Now you can choose whether you want to use botfather in the web or open it in the app. I prefer to use the desktop app. After opening botfather, we can enter front slash and select new bot. Now we have to give our bot a name. So we can call it something like cla code assistant. And now we have to give it a unique name ending with the word
[16:00] bot. I'll just call this claude code YouTube tutorial_bot. Then let's send this. Cool. We now have our new bot. And we can copy our token. So I'm going to copy this and add it to N8N. Let's save this. And if everything was done correctly, you should see this connection successful message. Cool. Let's close this and let's go back to bot father for a second. What I'm also going to do is actually set an image for this bot. So I'll select set user pick
[16:33] and now I can select my agent which was claude code YouTube tutorial. And now I can send a message to use as a profile pick. Then I'm simply going to copy this image and send it to botfather. And done. Our image was set. Now in order to access this bot we can click on this link generated by botfather. So now we can see the chat with this assistant and this little message on the side over here. Now what we need to do in N8N is click on execute workflow and this is
[17:04] now waiting for a message from telegram. So back in Telegram, let's press start. And we can see this note was successfully triggered. And if we look at all the details, we can see this chat ID which represents this unique conversation with the bot as well as the text that was sent. For us, it was just the word start. So let's add another action and let's add Telegram. And let's look for an action called send a text message. We've already set up our
[17:35] credentials. So we can simply select it from here. And now we need to pass in a chat ID. This is why we use the telegram trigger node. It's so that we can grab this unique chat ID over here. But we don't want to use a dynamic value. Instead, we'll simply hardcode it to that very same chat. Then for the text, let's simply say hey. And let's execute this step. We now get that notification from our bot. And if I open this, we can indeed see that message from N8N. Now we
[18:07] can also remove this bottom section by going to additional fields. Let's select the pend attribution and disable it. So let's change the text to something like hey, how are you doing? Let's execute the step. And of course, we get our notification and we can see our message. Hey, how are you doing? Now, what we want to do is actually replace this message from a hard-coded value to whatever Claude codes final message was. So, how do we do that? Well, first we're
[18:37] going to remove the telegram trigger and we can press tab or click on plus to add more nodes. Then for the trigger, let's select on webu call. This effectively exposes this workflow as an API endpoint so that we can call it from the outside world. Let's change the method to post and I'm going to change the path to claw code. Optionally, we can set authentication. I'm not going to bother with that for this tutorial. And this is actually it. Now, at this point, we
[19:08] actually don't know what the body is going to look like. So, let's actually do this. Let's attach the web hook to our Telegram node. Then, under test URL, let's copy this URL. And back in cloud code, let's take this a step further. Great. Now that we have the last assistant message, we need to pass the message to an N8 web hook. Here is the URL to the webbook. It's a post method. And in the body, simply pass in a
[19:39] property called message along with the last assistant message. And then I'm simply going to paste in that web hook from N8N. And let's send this. If you're familiar with Python, this really isn't that complex to implement yourself, but it's simply using the requests package to call the n URL and passing it a payload with one property called message. So, we are getting this message saying that no module named request is available. So, we can just do a pip
[20:09] install ourselves or ask the agent to resolve it for us. Hey, we're getting a message from our hook saying that no module named requests is available. Please can you resolve this Python error? Let's send this. And of course, the agent is simply going to pip install requests. So let's approve this. All right. So the agent seems to have resolved this issue. And now the event is failing for a different reason. It's saying that our NN endpoint isn't available, which is expected. We need to run execute workflow. And now this
[20:41] workflow is indeed waiting for that web per call. So let's try this again by saying hey. And hopefully this will pass the agent's response to N8N. In fact, as I was chatting, I can see this notification. And this is still using the hard-coded message, but does confirm that Claude code was able to successfully call the N8N web hook. Let's have a look at what we received from the web hook. We can see this body object which contains our message and the value from the actual agent. So all
[21:12] we have to do now is double click on the telegram node and let's replace our hard-coded text with the message from cla code. Then let's save this workflow. Let's activate it. This simply means we don't have to manually click on execute workflow. Edit in will automatically listen to any request from claw code. Also let's open our webbook. Let's go to production URL and copy this URL. Then in our script, I'm simply going to manually fix this. So I'm going to replace this web per call with a
[21:44] production URL. And now this means I don't have to manually click on execute workflow. Everything should just work. Let's try it. So let's say please create an index of HTML file with big bold letters saying claude code hooks tutorial. Let's send this. And look at that. The moment the change completed, I received my telegram message and it shows all the details provided by the assistant. How cool is that? Now, there is one more improvement
[22:16] I'd like to make. If we run claude code and we didn't set auto edits on, that's going to ask our approval before running any tools. So, we could say, please change the background of the homepage to black. Okay, let's send this. Now the process stopped because the agent is asking our approval to run this code. But of course we didn't receive any notification. So if this was a long running process and I actually walked away from my PC, the process would have stopped without me knowing about it. So
[22:47] what we can do is add one more hook to our settings file. So we can actually copy the entire stop event. Then let's add a comma and paste in everything that we just copied. Then we'll replace stop with notification. Everything else stays the same. We'll call the exact same script whenever we have to approve commands. And again, if you were wondering where I got the name from for this event, simply have a look at the claude code hooks documentation. All right, I'm actually just going to
[23:17] restart Claude Code. And let's try that again. Please change the background of the homepage to black. Let's send this. And now that event will be triggered just before the tool call. And look at that. The agent is asking our approval to run this tool. And we got our notification from Telegram. I hope you found this video useful. If you did, hit the like button and subscribe to my channel for more Claude Code content. Also, let me know what other Claude Code features I should cover and check out this other video over here.
[23:49] I'll see you in the next one. Bye-bye.