How to set-up and build an SMS Ai Agent (free template)

Rory Ridgers builds an SMS AI agent using Twilio, NAN automation, Google Sheets, and OpenAI. The agent batches incoming texts, queries a contact list, retrieves emails, and replies via SMS. He walks through each step and offers free templates from Commonus.
Chapters
What's up guys, in this video I want to show you the most effective way that you can create an SMS agent, which is obviously just an agent that you can chat with via text just on your phone or on your computer, wherever it is you text people from. So the main way that we're going to be doing this is using something called Twilio. And if you haven't used Twilio before, it's basically just a way that you can connect up existing numbers or buy new numbers and do things like make outbound calls, receive incoming calls, and also send and receive text messages. So to begin with, I'm going to show you inside of NAN here the scenario they have, and then we're going to go over to Twilio and I'll show you how to either set up your own number or buy a new number and then hook it up to NAN. So to begin with, like I said, this is a scenario in NAN, and for this we're just going to be building a simple agent that can send and receive, uh, emails for us via Gmail. So basically what we're looking at here is a webhook first of all. So when we receive a text, it's going to trigger this webhook, and then this little part that we have here, basically what we're doing is we're having it add that message to a Google Sheet, and then we're going to do some stuff with it. The reason we do that is is because when you text people, you're most likely not just going to send one text, or you may just send one text, but sometimes you're going to send multiple texts. So for example, you might message someone, say, hey, has this been done? Also, can you do this? And while you're at it, can you do this as well? That kind of thing. Might be sending multiple messages in one go but as separate messages, and we don't want that to trigger this scenario, uh, 3 different times and give us 3 different responses for each because it might need the context of the messages before. So basically what we're doing here is when a message comes in, we add it to a Google Sheet and then we wait 10 seconds to see if any more messages come in and then add those to a Google Sheet. And then we just go with, let's say 3 messages came through in those 10 seconds. They're probably all to do with each other and they're probably, um, all in context of each other. So we wait those 10 seconds, add all these new messages to a Google Sheet, and then we just use the messages from the Google Sheet as the input for our agent. I'll show you what I mean when we actually work through it, if that doesn't make sense. So the first node is gonna be appending Google Sheet and really, really simple in here. We just take the output from the webhook, which is going to be a message, and we put that into the message column. I'll show you the Google Sheet really quick. Really simple, it's literally just a message column, that's all that's in it. So the message that comes through in the webhook, which is going to be our text message, we're going to put that into that column and it will fill out this column here. And then we have a wait node after that, and I've just set it to 10 seconds, but of course you can customize this as you like. You can do a little bit longer, a little bit less, but I recommend around 10 seconds. And then what we're going to do is we're going to read Google Sheet, because what's going to happen here is let's say 3 scenarios run. Let's say you send off 3 messages, it's going to start 3 scenarios because the way that NAM works, every time the trigger comes through, it will just start a new instance of that scenario, if that makes sense. So if you send off 3 text messages, even if they're right after each other, it's going to trigger the webhook 3 times, which is in turn going to make 3 instances of this same scenario all running simultaneously. So what we do is we append the sheet, wait 10 seconds, and then we get the data from that sheet and then this code block here. If you don't know how to write these code blocks, by the way, don't worry, neither do I. I just asked ChatGPT to, um, write all these sort of code functions for me. Basically what this is doing is it's going to just pass through the data from here. So let's say there's 3 messages in there, it's going to pass through those 3 messages, and it's also going to pass through the phone number that we were sending the message from that we need to send the message back to. And then we just have another Google Sheet node that's just going to delete all of the messages that are in that sheet. And basically what's happening here is, as I said, let's say it's 3 messages come through, it's gonna start 3 instances of this same scenario. They're gonna be running simultaneously, but they're gonna be a little bit behind each other 'cause the first message came through, triggered that first scenario. And then let's say second later, another message came through, triggered that second scenario. Second later, uh, another message came through and triggered that third scenario. So they're all gonna be running simultaneously, but they're gonna be a second behind each other. So what happens here is let's say this one runs first, it pens a Google Sheet with these new messages and it waits 10 seconds and then the next one runs a second after, adds the message and then the next one runs a second after. But then when that first one has finished up with the 10 seconds, it's gonna go through these. The one before that will still have a second left and the one before that will still have 2 seconds left. But that first scenario is gonna finish this 10 seconds first, but the Google Sheet is gonna be updated with all 3 messages because those other 2 scenarios have already appended Google Sheet, if that makes sense. And then this first scenario is gonna read that Google Sheet, get those 3 messages that have been put in, by all of the scenarios and it's gonna filter them down and it's just gonna delete all of them. So when it deletes all of them, by the time that the second and the third scenario get to the read sheet, they would've already been deleted, which means the scenario is just gonna stop and it's only gonna continue with the first one where we have all 3 messages. So you're not gonna get 3 different replies, you're only gonna get one reply, but it's gonna be to all of those 3 different messages. This will make more sense when we run through it if it doesn't make sense to you now. And then we just have a really simple AI agent. I just did this kind of as an example. It's gonna be able to send messages for us via Gmail, then also get emails from our inbox. And then it also has a Google Sheet note that just has a bunch of names with email addresses attached. So when we say to it, let's say we message the agent on SMS and say something like, send an email to Rory saying what's up, it's gonna look through the Google Sheet, it's gonna find my email address from that list, and it's gonna use the Send Gmail tool to send over an email. So if you've used N8N before, this agent will look really, really simple to you, but I'm just gonna show you inside the prompt here. Your job is to reply to one or multiple message in here We give it the output from this code step, which is just gonna be, let's say it was 3 messages. It's gonna be all those 3 messages from the Google Sheet basically crammed into one variable. We can just pass to our AI agent. So it's just gonna be a list of the messages. Uh, you'll receive an array of multiple messages raised to general inquiries or also Gmail functions, and then just told it the 2 tools that it has. So it has the send email tool and to get the email address of the person that we wanna send the email to, just use the Google Sheets tool. To return all of that data and get a list of names of emails. I'm going to show you this spreadsheet quickly just so you can see what I'm actually talking about. So names down this one and then emails down this column, that's just going to return to the agent. It's going to match up the names with the email address. So as I said, you can say something to it like, um, send an email to Rory rather than send an email to Rory@autonomously-ai.com. It's a bit of a mouthful, doesn't really sound that natural. Uh, so we just told it how to use those tools and then to get info on emails, use the Get Emails tool, which will return the last 50 emails and find what the user was looking for. So these tools, really, really simple. It's literally just a Read Google Sheets tool and then a Gmail, uh, Get Emails tool. We set the limit to 20, so it's just going to get the last 20 emails from our inbox. And then a Gmail Send Message, and we've just left this all up to the agent because it's going to define who it's going to from that Google Sheets knowledge base. It can define the subject for us, and then it can also define the actual message content for us. And then we just have a Twilio step, and what this is going to do is it's going to send a text back to that same number that it received a text from. So if we obviously message in the agent, then our number is going to go through and it's going to send a message back to our number with whatever the response from our agent was. So now you've got a general idea of how this system works. I'm going to show you inside Twilio, and then we're going to run through this system and we'll have a look at the output and also have a look at the execution to see exactly what's going on. So in Twilio You get started, it's free to start. You get £10 of usage for free, and with this you just sort of top up your account and then it takes money out of your account every time you do an action like send a text or make a call. So in this case we're sending texts, and it's really, really cheap to send the text. I think it's off the top of my head something like 0.008 pence to send the text. So really, really cheap. You don't really have to worry too much about the cost with this. If you want to buy a number, so you don't have a number, you want to buy a number, if you go to buy a number And then you can set the location. So it's United States for me. I'm just going to do, uh, United Kingdom because I'm from there. So United Kingdom, there we go. And then it's going to show me all of these different numbers here. And for this, we need to have the SMS field in. So it shows us the different capabilities of each of these numbers. And you can see a lot of these are voice and fax. We need one that's SMS. So, okay, there's not one here. Uh, if we turn these filters off and so we only want SMS, hit search. Okay, perfect. There you go. One like this. If we was gonna buy it, we just hit buy. Tells us the capabilities and then we go next. And of course if you're a business buying this, you'd hit business. But for me, just for this demo, I'm gonna hit individual. Then we hit next. And now you'll need to get an approved bundle. You can see I've already got one here, but if you're gonna make a new one, go to create a regulatory bundle. And then in here, this does take a minute, but it's really, really simple stuff. You basically just put in some information. So it's going to be your country, the type of phone number— we've done mobile— hit next, and it takes you through this whole thing here. And you just basically got to answer these questions. It's really, really simple. You also have to provide some form of a valid ID, like a driving license or a passport. So we're just going to say direct customer and then individual and click continue. Okay, then it's just going to ask us like our first name, our last name, our email address, then the phone number of the individual. And for that phone number of the individual, you you would put in the phone number that you're looking to buy. So this one, you put that in there, and then it just walks you through the rest of it. I don't have to go through it with you, it's really, really simple stuff. It just asks you questions, you have to reply, and then it will send you over an email saying your regulatory bundle is pending. Then it says it takes up to like 72 hours or something, or 48 hours, but for me it literally took 5 minutes to do it. Um, so just kind of wait for an email to say that it's confirmed, and then it will be added to your account. And you can see it pops up like this. And then you'd obviously just put that in there. So now that you've got your number, you can go to Phone Numbers, Manage Active Numbers, and then this is how we're going to hook it up to NAN so we can actually, uh, receive text. We can set up a webhook, so when we get a text to this number, it's going to trigger our NAN scenario. So what you're going to do is you go to Configure, and you can see we have Voice Configuration. Don't need to worry too much about this for what we're trying to do. We want to scroll down to Messaging Configuration, and then down here we have a— when a message comes in, we set it to webhook. And in the URL in here, there is going to be like a Twilio URL in here. You just get rid of that and then you will put in your webhook URL from NAN. So I've done my production URL, probably going to change it for test URL when we actually test this out. Um, but if we come over to NAN, you just put in obviously a webhook node and then you just copy this URL over, paste it into there, and hit save configuration. For this actually, I'm going to change it to test URL so we can test this out together. So if I paste that in there hit Save Configuration. Okay, if we come back over to an— now we're going to test this out and have a look at sort of the execution and all the variables that are going on here. Okay, so, okay, so if we hit Test Workflow, and then let's message our SMS agent here. So I'm just going to say something simple like, hey. If we send that off, and then you can see— if I make this a little bit smaller, get out of the way— you can see that's triggered now. It's going to add that to Google Sheets, it's going to wait 10 seconds, If we look at our Google Sheet, you can see message, hey. And then it's going to wait 10 seconds, it's going to read the sheet, get that message back, and it's going to pass it through, delete it, send it over to our agent, which is going to reply. And if we come over, you can see, obviously I've got a trial Twilio account, I've just done the free version, you can pay for it if you like. But for this demo, I'm just on a free account. So it says sent from your Twilio trial account. See, if you wanted it to not say that, you would just upgrade Uh, then you can see it says, hello, how can I assist you today? Perfect. And then if we go to our spreadsheet, the message has been deleted, ready for new messages to come in. So perfect. If we look what's come through here, this is what comes through in our webhook. So if we scroll down, it should say somewhere the body. So yeah, body is hey. And then in the Google Sheet, we just drag in that body variable. If I go to schema, uh, body, we just drag in that body variable, which is hey. Perfect. And then the wait node, obviously it's just a wait node, it just waits 10 seconds. And then read Google Sheet, really simple, it's just going to read the entire sheet because the only values that are in there should be the current messages. So it just reads the entire sheet, it's got that one message here. And then this node here, what it does is it passes through the number that this is sent from, so that'll be my personal number because I sent a message to the number that we just bought. So you can see from there is my number, and then the messages, it says row 2, hey, and then total messages is 1. And the reason we have total messages. I'll show you in a sec, but we get these 3 different variables. So it's from this number, these were the messages, and then this is the amount of messages. And obviously if there was more messages here, let's say there were 4 or 5 messages that we sent in sequence and they all got added to the Google Sheet and they all got passed through here, it will still just be one item. The messages variable would just contain all of the, uh, different messages that we got from the Google Sheet. And then the reason we have total messages here is simply because in the delete sheet node We start from row number 2 because of course row number 1 is gonna be the header. So we start from row number 2 always, and then number of rows to delete is just gonna be the number of messages that are in there. So it was one message, only one message in there. So then we drag in that variable into here and it's gonna be 1. So it's just gonna delete one, uh, row from here, which is just gonna be that one message. Perfect. And then we send that over to our agent. So we drag in this messages variable. And you can see it's just row 2, hey. And then obviously this didn't have to use any of the tools this time, so it's just outputted hello, how can I assist you today. And then in the Twilio node, we've done 2 is going to be the number that we pass through from here. And by the way, if you're wondering why we pass through this number even though it comes from the webhook node, uh, somewhere if I can find it, uh, so yeah, it comes from, yeah, so from node here. For some reason on any, and sometimes if you have like a data manipulation nodes like that code node, sometimes it can mess things up. So if I was to use this variable, sometimes it's undefined, and obviously that's pretty unreliable. You don't want it to be undefined sometimes, but it's just because those data manipulation nodes, sometimes it kind of messes up the order of it and again gets a little bit confused on which variable's which, and it can't define the variable. So that's why sometimes if that ever happens to you, just define the variable where you can, and then you can use it later on like in this example. And then message is obviously going to be the output from our AI agent, then send that back. Hello, how can I assist you today? So perfect. Now what we're going to do is we're going to try and send a sequence of messages and actually get it to use some of these tools. So to begin with, we're going to change the test URL from production URL. So let's click production URL, copy that, come back over to NAN, and then just paste that into there. Perfect. Save configuration. Before we run this through though, I want to show you quickly how you can get the free templates for this. So all of my free templates is on a platform that I built myself called Commonus, and this platform is open to anyone to put their own templates on there. So you can see a couple of people have already started to put their templates on there. You can go on here, look at all their profiles and their links and their bios, everything like that, all the templates they've posted. But all of my templates are on here. So if you want to grab any of the templates, you can either type it up or you can just find it in one of these sections here. This one is probably going to be in the other section, so you'd find it in here. Or again, you can just type it up, or you can come to my profile and go to templates and look for it in there. But if you wanted it, you just click view details. I want this template, and then the template is going to appear here. This was a relevance AI one, that's why it's a link. But obviously, of course, with NAN, it's gonna be a file and you can just download it from there and it'll show you some other templates that are similar to this one as well. By the way, while I'm on here, if you guys wanna share your templates for absolutely free and have all the features that you might need to be able to share your NAN or your Make.com or your Relevance AI templates, then just come over. It's completely free to sign up and you can start making templates straight away and hopefully you can get on the leaderboard and people can see you. Anyway, now that we have the production URL set up in Twilio, what we're gonna do is we're going to open our messages here. And then let's say a couple of things to it. Let's say something like, just to test this out, I got this email from LinkedIn recently just about me appearing in searches. You know what it's like with LinkedIn, you get hundreds of emails a day from them. So we're just going to quickly say to the agent something like, uh, hey, did I get any emails from LinkedIn recently? And then we're going to send 2 messages here and see how it reacts to it. So, hey, do I get any emails from LinkedIn recently? If I did email Rory Bridges over the content. Perfect. So, hey, did I get any emails from LinkedIn recently? If I did, email Rory Bridges over the content. And then we're going to wait for this to send us an email and also reply here in SMS. Okay, perfect. So as you can see, I got this email saying, hi Rory, I noticed a recent email from LinkedIn regarding your profile appearing in 19 searches. If you need any other content or further information from these LinkedIn emails, please let me know. Best regards. And if we come over to messages, uh, you have received an email from LinkedIn recently with the subject, your profile appeared in 19 searches. I've also sent an email to Rory regarding the content from the LinkedIn emails. If you need any more assistance, please let me know. So absolutely perfect. That's exactly what we wanted. And I managed to have both of these texts and sort of actually look at them the same, even though they were two different inputs, and just do whatever it needs to do in one go rather than doing it in two and it being all weird and mismatched. We come over to N8N, you can see the two executions here. So this was the first one, and then this was the second one. When it loads, this was the second one. So if we look at the first one, basically what happened is that first message came through. So that first message was, hey, did I get any emails from LinkedIn recently? That came through here. As you can see, if we scroll down the body, hey, did I get any emails from LinkedIn recently? Added it to the Google Sheet, and then it waited 10 seconds. And then obviously about a second after, we sent another message, which was— if we have a look at this, scroll down to body— if I did email Rory Ridgers over the content, and then again I added that to the Google Sheet, and then it waited for 10 seconds. But while this was waiting, this probably hit 8 or 9 seconds, and then this one, the first one, would have finished running. So it quickly read the sheet, passed through that data, as you can see. So we got the 2 messages there. It quickly read the sheet, passed through that data, and then deleted the content from that sheet. So by the time this one had finished executing the wait node, when it read the Google Sheet, there was nothing in there. As you can see, there was no data in there, so it just stopped the scenario. This way we aren't getting these sort of multiple replies, as I mentioned. It's just adding it to a Google Sheet, which is sort of acting as like a short-term memory for it. And then we're having these two scenarios. This one takes a little bit longer, so by the time it reads the Google Sheet, it's already been deleted by this first one, and that second one will just stop running. And then this one goes through uses the AI agent. As you can see, it used the OpenAI model a couple times, used the memory a couple times, and then it would have used the get all messages, the Google Sheets, uh, sort of knowledge base type thing, and then the send email. And then if we have a look at this output, exactly what we got in our text message, and then Twilio sent a text back to that same number with just the output. And of course we got the email as well. So absolutely perfect. Okay guys, so that is it for the video. I hope you learned something. Something I didn't mention at the beginning of the video is that if you did want to work for me so we can implement some AI agent systems into your business, then there's a link in the description. You go ahead and book up a free call with me and we can talk about some ways to integrate AI into your business. And of course, if you want to grab the free templates, I showed you how to do that so you can get started with this straight away. I don't want to bore you with an outro. If you've got any video suggestions you'd like to see, just let me know. If you don't, I'll see you in the next video.
More from Rory

Dynamic Workflows are crazy. This is how to actually use them (beginner's guide)










