so we finally got the bot working on my phone right now and in this video I'm going to show you exactly how you can set up your own WhatsApp AI bot using pure python completely for free so throughout this video I'm going to walk you through this repository that you can clone over here as well so if you want to follow along make sure you clone this all of the information and all the code that we will be using is already in here as well as all of the steps for the documentation I'm going to
walk you through this in video format showing you everything that you need to know but this also here for reference uh in a text format now there are some requirements uh that you need in order to follow along and first of all it's good if you have some basic understanding of python so you actually know what we're doing and you can follow along so my setup uh that I got going on is here within fi code but after cloning all of the code in the repository you should have a python at environment that you can
run so you can start testing so with that out of the way we can get into it and uh then the next step is first we need a meta developer account So Meta formally Facebook owns WhatsApp and we have to start from their developer portal to create an application and then configure it for WhatsApp so if you don't already have one follow step one follow the link over here and create your meta developer account okay then second we need to create a business app so you can follow the documentation over here to see how that
works but let me show you that and give you an example so let me refresh this come over here if I come over to meta for developers and go to my apps I can see I have two apps over here so the python rbnb bot is the one that will be using for for for this demo I'm also experimenting with uh Bots that I'm building with botpress that is for another video but you can come over here and click on the create app and then here you have to select other then next then over here
we can select business next and then you fill in your app contact email and select the name for it so this is the test Bol and then over here you want to select your meta developer business account that you've just created in step one so make sure to select that over here and then you can create the app now the next thing that we have to do is add WhatsApp to it so scroll down go to WhatsApp and click on setup and then you should see that WhatsApp is added over here to your application now
that is setting up the initial bot and with that out of the way we can continue to Step One getting to our phone numbers if you go to WhatsApp and then API setup you can see some information over here and first of all we can see your test phone number here and also your phone number ID and this one is important because this is the number that we will be using to actually send text messages to and if you come over here and look at the official documentation you can see that everyone uh let's see
starts with a phone number so with a test phone number so if you add WhatsApp product to your app it does the following things so you will be prompted to create a MAA business account but you will also get this free test number which is very nice so we can test with it and you can add five recipient phone numbers to this application so one of that will be your own WhatsApp number that you can use to interact but you can add four more for testing purposes all right so what we should then do is
let's see come back to the documentation and you should now add your own phone number as well so like I've said this is one of those five numbers that you can add and you do that by going to the two over here and then select a phone number so my number is already here which I will black out for you but then you can to go to manage phone number list and you can add a phone number so put in your country code put in your number and then store it and from there you can
then select this and before you can add that number so so once you fill it in you also get a code via WhatsApp so don't check your text messages Teck your check your actual WhatsApp app and there you should have the code for verification that you have to put in all right so that concludes step one we now have the WhatsApp business app going on we have a test number and we've added your own number for testing purposes all right and then we can continue to step two which is sending messages with the API so
for this we first go back to the app overview and here we can do two things so first of all we can send a test message here straight from the app dashboard so if you come over here and you you fill in your own number and then click Send message that is one way to verify if everything is working so you can do that but that is pretty straightforward and eventually we want to do this with python of course so um while you can test this out yourself to see if it works I recommend you
to immediately go to the python function that we have created as well so let's have a look at that and then we're trying to simulate what's going on over here with python so that's really the first step and for that we go to the start folder and then we go to the WhatsApp quick start and here you can see a send WhatsApp message function that we are going to use and here you can see how we can call that function but before we can do that we have to fill in some of the information that
our application needs in order to make this verification and first of all we need a version a phone number ID and we need an access token so the next step that you're going to do is let me come back to the example over here you're going to create a EnV file based on the example Dov file within the project folder over here so what you do is you create a new copy so let's see we take this one and we copy it and you call this one EnV which I already have so I don't have
to do that one and in here are all of my keys but you are going to update this one and we're going to start with the access token and the access token can be accessed over here from the app dashboard API setup temporary access token now please note that this only lasts for 24 hours so if you copy and paste that put it in here if you come back tomorrow you'll have to update that again within the instruction instuctions over here I explain how you can create a token that lasts for longer for 60 days
or even longer so if you want to do that then follow these instructions over here it's pretty straightforward but you can figure that out eventually you'll have another like very long token that you then put in here and it will work the same okay so that's step one so let's come back what do we need as well that is the version number and that is already filled in so that is version 18 so that is currently the latest version of the WhatsApp API that we can use then we have the phone number ID and we
have your phone number so coming back so the recip recipient uh WhatsApp ID that should be your number and you should put that in here with the country code included so I have an example over here that is what you should put in here so if then this for example would be your number you come over here you store that in here then final thing for now is the phone number ID and that is your WhatsApp test number so coming back to the API setup phone number ID you copy that you come back phone number
ID plug it in there all right and once all of that information is filled in we have everything that we need in order to run this first function where we are going to simulate the hello world test message but before we can do that make sure you have an active python environment with the requirements that's dxt installed because for this test already we need to python do n library to load all of the information from the EnV file so if you have that up and running let's load this up and see if everything works okay
no errors then we can store this function in memory and then now we're going to actually send the template message to our own number so let's run this see what's happened what's happening we get a 200 status message which say which says that is everything is okay and now we're going to monitor our own WhatsApp uh application and wait for the test message the hello world template message to arrive now this can take anywhere from 60 to 120 seconds based on my experiments but make sure you get that before we continue to The Next Step
because this is really essential to configure that we have that connection going on all right there it is we got the message so everything is set up properly now and then the next step which is very important and this took me way too long to figure out is we have to reply back to the bot why is that if you don't reply WhatsApp is only able to send template messages like this using type template and for example this hello world and it doesn't work if you want to send a custom message which is the next
step that we're going to do so simply reply with anything just say hi and it won't say anything back but what we can do right now is now since we have that connection your number is now verified and we can can now and we can now continue so I have a get Tex text message over here and a new send message function as well and what we can now do and we have the text in here says hello this is a text message let's send this and then if everything works well you should get this
test message within WhatsApp app as well all right and then for all you Geeks out there if you want to take this even further there's also some code to send these messages async So eventually when you put this into production this could be interesting for now I'm not going to dive into this but it's here for your reference that almost covers uh or completes step two but there's two more things that we have to do we first of all need your app ID and your app secret which we will use in later steps so we're
going to update these within your EnV file so this is the example make sure it's in the actual EnV file so that is going to be your app ID and your app secret now where do you get that information you come back to your app dashboard here you can see app ID copy paste that over there and then your secret can be found by going to app settings basic here again you have your app ID and app secret so click on show copy that information and then paste that in here so that should conclude all
of the information so we have ID secret your phone number the version and the access token either the 24hour one or the one created uh for a system user and that concludes step two now and if you've been following along up until this point then please make sure to hit that like button and also subscribe to the channel so you make sure that you stay tuned for future AI videos all right and then let's get into step step number three which is configuring web hooks to receive messages and now let me also state that this
is the hardest part of the tutorial but if you figure this out if you follow these steps then you will have all the building blocks you need so what we are essentially going to do here we just saw that we now have a function that we can use to send a directed message towards a number right but what we want of course is we want to create some kind of a trigger where if the app receives a message something is triggered so whenever I send a text message like hey and I ask a question to
the bot we want the bot to take action right so how do we configure something to listen for that well we do that through what's called a web hoop so the first thing that we have to do right now is we're going to run our application so we come back to our project we go to run.py make sure that you have a python environment active and make sure that you have all the requirements installed if you don't have that yet run pip install all requirements.txt to get it all up and running and what we can
then do is we can come over here within FS code and say run python file now if you're using any other IDE or python editor um it should be a similar process even through the terminal your app should be running and this is the information that you should see over here so running on and then an IP address and then Port 8,000 so the next thing that we have to do is we have to use enr and enr is a tool that we can use to simulate a a server essentially so we're running this app
locally and it's running on Local Host but it must be able to listen to WhatsApp messages so whenever a message is sent to Whatsapp that that ping that information that web hook should be sent somewhere and if that's on Local Host and the meaning offline we can't get to that so enrock is a tool that we can use to bypass that for testing and development purposes where later you would transition this to a real server um that's running all the time and has access to the internet and for those of you that might have already
worked with angro before this is important because this is also where I ran into some issues and this took me another two hours of debugging you need a static enro domain and that means you have to sign up for an account and claim your free domain because if you don't use it with an account and without the authentication it will not work because uh meta validates your enr domain and certificate and if you just for example with with Brew uh install enr and then run it doesn't work you need to follow these steps so if
you're new to enr just sign up for a free account download the enr agent and then go to the authentication and all of these steps that are listed over here if you check out this documentation you can follow the steps here as well and it's a little bit more Visual and eventually they also say over here you go to the dashboard how you uh authenticate it you have to run some commands with an authentication token within your terminal and it should be pretty straightforward you should set that up okay and then that is where you
should create your uh freedom domain so that's also a URL over here how do I claim my free static domain if you go to your enr profile log in go to Cloud Edge domains if you don't have one you can create one for free so here you can see how that works and then you can also copy and store that so that's very important because once that is all up and running we can now start enr and you can do that by using the following command so it's enrock HTTP we run this on Port 8000
you do d d domain and then this is where you copy and paste your free enr domain so let me show you what that will look like for my one I can run this over here and here you can see it's now running and we should monitor this because soon we will start to make connections with this and the logs will show up over here all right so this is now running and those are all of the steps over here now next integrate with WhatsApp now we have to go back again to our meta app
dashboard come over here to Whatsapp then go to configuration and here we can see that we can set up a web hook so I already have one but you should come over here and edit it so first of all the Callback URL let's see that is over here so this is where you take your enr free domain take the full URL it should probably end with apppp if you use the free one and I make sure you use for/ web hooks because that is how it's configured within the flask app if we come over to
let's see where is it to the app and then to the fuse you can see over here that this is we have forward SL web hook so make sure that that is configured correctly and then next you can enter a verification token now this is completely up to you what to put in here it doesn't matter you could put 1 2 3 4 5 whatever the only uh important thing is that you store this verification token because we need it later so put something in and then come back to your EnV file I'll take the
example and make sure that you put that verify token and updated so if you do 1 2 3 4 5 make sure to store that there as well all right so if your flask app is running you should see the terminal running over here and your enr is also running make sure both are on Port 8000 and this is why I said this is the hardest part there are a lot of moving parts that have to work together over here and if you mess up one thing this will not work but if you did everything
correct up until this point you can hit verify and save and it should pass that check mark and you can have a look at the enrock looks over here and that should work mine is already set up so I can continue you to the next page and then the next step is go back to the configuration configuration page and click manage so that's over here web hook fields and here we can identify subscriptions that we want to subscribe to and the only one that you need although you can update to more um subscriptions but for
now we only do messages so make sure to checkbox this one over here hit save continue continue or done to make sure that you are subscribed to messages so what does that mean this does now mean that our application is listening to incoming messages and if that happens it will trigger or send a some information to this URL which is right now configured with our flask application and tald through enr so are you still with me I can understand if you're new to all of this it can be quite a lot to understand how all
of this comes together but yeah this is building web applications and now one final check that you can do if everything is up and running you can come over here to the messages and you can click on test so let's see what that is doing see over here we can see post web hook and we have status message okay and you should also get a message on your phone in uppercase all right and now the final t test that we have to do is we have to test this using our own phone so again make
sure that everything is up and running enr is running then go to the conversation where you were just receiving the messages from and also receive the hello world message from and say something like hi there and now the bot should immediately reply back to you with what you just said but then in uppercase so we've essentially created a shouting WhatsApp B beautiful right no but in all seriousness if you got this this connection up and running this was really the hardest part now actually the fun part begins so good job if you've been following along
all right and that brings us to step four which is understanding web hook security so in this read me file I've outlined some uh tips and some insights on how the mainly the security dopy is set up within this application and really I think this took me the longest to figure out how to properly set this up but it's already all here for you so I'm not going to go into all of the specifics really in this video because that would be too much it's also quite boring but just know that what's going on over
here is it's quite it's quite cool stuff it basically um it is also uh required to interact with these web hooks because there is a verification process that is required from The Meta WhatsApp business business API and and this takes care of all of that so we've created some some decorators in here mainly the signature required which then validates the signature based on the app secret and if we come over here to the fuse and look at the endpoint so whenever something something is sent to the for/ web hook endpoint the add signature required decorator
is passed on here and this just makes sure that our application is is safe and that the message that are sent to our application are verified and then we have another verification process in here as well which uses the verifi token also required from the um the API and like I've said if you want to learn more about that we have the verification requests and then validating and then validating payloads you can reference all of the sources and learn more about that but this is a really nice and clean setup to work with that and
now real quick if you want to learn more about artificial intelligence working with data and building applications like this then what you can do is you can check out my free group data Alchemy link will be in the description and in the first comment below this video in here is a community with almost 5,000 members we share ideas on data artificial intelligence and we also have the classroom with various free courses so that's all really exciting now if you're already a little bit further in your career and you want to learn how to sell and
monetize for example building WhatsApp AI Bots like this and how you can do that as a freelancer then we also got data freelancer which I will also leave a link for in the description so if that sounds interesting and you want to learn more than just what's here on YouTube make sure to check out those links right now all right and then step five is to learn more about the API and and build your app so if you want to do more with this I highly recommend going to the uh WhatsApp Cloud API and just
look at what you can do over here so there's actually a lot more uh that you can do in terms of sending messages sending polls sending sending images so you can go over here to get started and that will cover most of what we did already but here we have message templates for example so the hello world one that we sent as a test experiment that is something that you could put in there so really the API is suitable to create really Advanced even I would say message flows and sequences that that you can build
up so if you're interested in that definitely look into the API documentation um I will definitely continue this journey diving into this more but that concludes it for now and now we'll get to the final part for this tutorial and that brings us to step six which is integrating AI into the application which really is what you came here for right that is in the title building AI WhatsApp BS and now we only have a shouting robot which is not very useful so how do we integrate this with for example open AI well I got
you covered and everything is already worked out basically so let me show you what you can do with this and now we can get a little bit more into the code itself to see what's going on but mainly there's one file WhatsApp utilities and here you can see everything that's going on when a message comes in so this is a function to send a message here is our some to process a message but really here is what's happening when a new message is coming in so process the WhatsApp message and right now we use a
very simple function generate response that just returns that response in uppercase so that's what you've been seeing you send the message to the bot and it replies back in uppercase but now what we can do if I comment this out and I import my open AI surface and put generate response in there and then let me see let me uncommon that we now have an AI integration so what's going on here behind the scenes well let's actually have a look at generate response from the open AI surface and I already created an entire video on
how I set up this process specifically so I'm going to link that within this video so make sure if you want also want to integrate that and understand that then you should check out that video because this uses the openai assistance API with some data added to that mainly the rbnb uh fhq that we created so let me actually show you we have the data and that assistant has access to all of this information turning it into an Airbnb bot that can answer qu all the questions basically that are in here so now let's come
back over here and run this again so like I said it's as simple as changing this function over here to generate the response to integrate your custom functions now let me say that one more time so this is a very like straightforward templated boiler plate that you can use to interact with the WhatsApp API but what's happening here inside this generate response function this is going to make it user user specific and use case specific so here you can put all the logic that you want to take a message that is sent to whatspp app
so that is the body that's coming in and we take the message body out of that and then you have to integrate Implement some custom functions to take care of that and to handle and to process that and ultimately send the message so I hope that is clear so for now let's save this and come over here to our run python file again and spin this up once more so make sure that right now that everything is up and running again that enr is still running and if you want to do this yourself follow along
you have to update the open AI uh API key environment variable as well and also your open AI assistance ID so you need an ID uh an assistant as well again if you don't have that check out the other video it's pretty easy to set up okay but what we can do right now is we can start to ask questions like what's the wifi password and now since it has to to communicate with the open AI API it usually takes two to 3 seconds to process this information and boom there we go the Wi-Fi password
is and now let's see what else is there what's the PIN for the lookbook so what's the PIN so let's ask that question and see how it will respond and boom there we go we have the pin is 7469 and now we literally have built a WhatsApp Airbnb bot that is using information from this document that we have over here so so it can now help us with all the questions and all the questions and answers that are in here you could do literally the same for any other assistant or AI integration you don't even
have to use open AI you can use Open Source One or other models just go in the surface and see what's going on in the generate response function over here and you can create your custom one now and if you want to learn more about all the codes that is going on over here so for example there's a lot going on in the WhatsApp utilities we have some configuration for the flask app we have some fuse which are also flask specific on how to handle the messages verification and the web hook then I recommend first
of all going to the GI page and then go to app and then read about this project structure over here which is using the flask Factory pattern I know that we didn't dive a lot into all of the code because also there is really lot going on so I wanted to cover this high level and then look at it and then see for yourself what pieces of code or functions you don't uh understand and then maybe dive into that so with all of this code if you don't understand it what what I recommend really put
it in the chat GPT and ask to uh ask it to explain it for you ask it what's going on um it can seem like a lot but it's it's a really simple flask web application in the end even though it was a lot of work to put all of this together but understanding how all of the pieces work together should be pretty St straightforward uh especially if you followed this video up until this point and now the only things basically left to put this into production into a real application is adding a real phone
number not a test number and putting this on a real server but that's for another video so make sure to subscribe so you can stay tuned for that and now if you want to learn more about integrating AI within your applications could be WhatsApp but could also be something entirely different then make sure to check out this video over here