Add A Voice Chatbot to Your Phone (Telegram)

LARRUZ shows how to build a ChatGPT-powered Telegram bot that handles voice messages. The bot converts Telegram voice files to MP3 using MoviePy, transcribes them with OpenAI Whisper, and sends the text to ChatGPT. You'll set up API keys, run Python scripts in VS Code, and test text and voice interactions.
Chapters
Today we'll be building a custom ChatGPT chatbot that works on your iPhone. And not just that, it will take voice input as we will be integrating the Whisper API. And as per usual, no matter your technical level, this tutorial is going to make the custom chatbot right on your phone, accessible to everyone. So here's the plan for today. We'll be building three versions of this, all right? The first one is an extremely simple version. Literally everybody can copy paste this and make this happen because we'll be using Telegram, which makes it very simple to integrate this bot. Secondly, we'll make a slightly more complicated version also in Telegram, which remembers your last messages. This is where it starts getting interesting. And the third one is the most interesting part of the video here, because it's going to be able to accept your voice messages. That's right, you can pick up your phone, record voice messages and you're going to get a reply from ChatGPT. Impossible. And not just basic ChatGPT. You're going to be able to send set it up so it has a personality that you pick in advance. And all of this can literally be done with no coding experience whatsoever. But I would say this, if you're completely new and never touched code before, I recommend you check out my last video where I taught you how to install the app that allows you to run the code. And there I really start from the ground up and show you how to use the ChatGPT API as a beginner. Because today we'll be building up on our tutorials, skipping the two basic steps of installing VS code and getting your OpenAI API key, which is the still incredibly easy. So everybody should be able to get that done. Alright, it's time to get into it. We'll start on the phone. So for all the free scripts you'll need the Telegram app. So download that first. Once you got your account set up, you simply go to the search and look for botfather. Alright, now you open a new chat with this verified account. And I did this before and in here it couldn't be any simpler. All you do is type slash new bot and then the bot father is going to ask you. All right, a new bot, how are we going to call it? Please choose a name for your bottom. Going to say AI advantage 9001. Good. Now let's choose a username for your bot. It must end in bot like this for example, Tetris bot or Tetris bot. So we'll say AI Advantage 9001 bot. Perfect. And it Replies with our API key. All you need to do is copy this message and bring it over to your laptop, because this is one of the two things we need to make it work. We need the Telegram API key and we need the OpenAI API key. All right, and also what you'll need here is the page Python script that I'm providing to you. In the description below, there's a Dropbox link which makes this really intuitive for anybody who hasn't dealt with code before. But I also put this in my GitHub repo for you more advanced users. All you got to do is download this folder and then all you need to do is take that folder and drag it onto Visual Studio code. Click yes on this and then you'll want to open the very first script. 01 dummy chatbot. So this one is named Dummy because it only takes one command and it doesn't remember. But it's a perfect starting point to explain this. So let's get into it. Okay, so here's the message from my iPhone, and all I need to do is copy this API key over here, just like that. And then inside of the script, all you need to do is go to Telegram API token, and inside of the quotation marks you post your Telegram API key. Makes sense. Now the next step here is Copy pasting your OpenAI API key. We went over this in the last video, but just as a reminder, under this URL you can always create a new API key. And yet again we copy paste it into the quotation marks right after it says OpenAI API key. Okay, before we go any further, I want to give you a quick and super simple explanation on what's going on in this code. Okay, so we just talked about the two API keys. This is just a way to link to the app. So the Telegram API token allows you to link directly into the chat inside your Telegram app on your phone, and the OpenAI API key allows you to interact with ChatGPT above here. As you might already know, these are the different packages that we're going to be using. As a beginner, you can also think about these as pro presets that make your life really simple in here. So obviously we have the OpenAI package to make ChatGPT work, and then also we have the Telegram package, which makes it really simple to connect to custom chats within your phone. Okay, down here we have a function, and this is just a basic way of interfacing with a chat inside of your phone. If you want more details on all of this, you can Always find them in the Telegram API documentation. But all you need to know is that this function allows you to send messages inside of Telegram. And this part is the ChatGPT integration. As I showed you in the last video, we're using the GPT 3.5 turbo model here because it's the cheapest and it's still excellent. Right after content, you get to define the personality of your bot. So here we're saying, say that you're a dummy chatbot and add a silly phrase. Okay, this part down here, you don't really need to understand. It's just how the Telegram package functions. Roughly speaking, every few seconds it starts polling and looking for new messages, and if it gets those, it starts interacting and you get the results you want, which is a text message communication with the ChatGPT API here. All right, so we, we're almost ready to go here. All we need to do is go to terminal, new terminal. And now you might have noticed that there's this 4 file in here, requirements. And this file is here just to make it super simple to install all the packages. And again, you don't need to understand what's going on inside of the terminal here. All you need to do is type PIP install R requirements. Txt. If you're on a Mac, it might be PIP free install. Both of those will be in the description below. And then when you hit enter, it automatically installs all the packages, including Telegram and OpenAI. And let's just switch up this part so it says you're a helpful assistant that always responds with a joke. Now we define the personality of ChatGPT and we're ready to run this script. So just go up here, press play, and now the script is running on your computer. Just remember that if you close this, it's not going to work anymore because it's running from here. That's it. Our chatbot is running on this machine right now. So now if I open up my phone. So inside of the newest message with the bot Father, it links to our new bot. So just click click the link at the top here and at the bottom, you can start the chat with the brand new chatbot. Let's just say, write me an essay about Borat. Why did the tomato turn red? Because it saw the salad dressing. Okay? One of the standard ChatGPT jokes. And it does that because we said you're a helpful assistant that always responds with a joke. So we forced it to respond with a joke. So you can set this up in any way. Just remember that this doesn't Remember your previous messages. This is just the most basic stuff setup, but it works okay and as we're developing custom apps today, you might need a hosting provider to host your ideas once they're done. That's why today I partnered with a website you should know about. Hostinger. They make website hosting easy and provide everything you need to create a stunning website. With Hostinger you'll get a free domain with the shared and cloud hosting plans, 24. 7 customer support and a 30 day money back guarantee. They're highly rated on Trustpilot, Google and a WordPress beginner so you know you're in good hands. You can choose from shared cloud and VPS hosting plans. The latest ones are the ones that can support your apps and help you launch them online. With the VPS you can get full root access, a dedicated IP address and much more. All that you need to successfully host your application. The VPS plans start for as low as 3.99amonth and with my discount code AI Advantage, you'll get an additional 10% discount for VPs and all other plans. So don't forget to use it. Their VPS comes with an easy setup where you can select your preferred server location, operating system and even control panel. And if you're looking for something even easier to check out their shared hosting plans. They're optimized for WordPress and come with a one click WordPress installer together with lightspeed web server technology and advanced cache solutions. You can always choose from their website builder that comes with designer made templates and requires no programming knowledge to create a website. And with 24. 7 support from their team, you'll never feel lost. Over 1.5 million website owners trust Hostinger and with their 30 day money back guarantee you can try them out risk free. So if you're looking to start or improve your online presence, give Hostinger a try. Click the link in the description below to get started. And now let's get back to today today's video so let's move to the second script here. 02 simple chatbot does the same thing as previously, but now you'll get to interact with it on an ongoing basis. Again, you need to update the API keys in here first for this to work. You can just copy paste them from the first script and as you can see this code is a little longer here and that's because we have this reply functionality in here. Other than that again you don't really need to understand this and we already installed all the packages so again all we need to do here is press play and the script is going to start running on our local machine here. Because we use the Telegram API key that links to this chat, we can go back in here and do another request. Let's just do the same. Write Minnesota say about Borat. Aha. And here I notice another joke. So this means our first script is still running. So you have two options, honestly, the simplest one is if you just close VS code completely and reopen it. But you could also go in here and inside the terminal, you press Control C on a Mac enter, and that's going to make it stop. But just remember, you can always just close the app, reopen it, press play on the second script, and we're back in the game again. Write me an essay about Borat. I really want that essay. Wait a few seconds. This is natural. ChatGPT takes a little bit of time. And there it goes. Isn't this just perfect? Look at that. The message always starts with bot because that's what we hard coded in here. If you look down here, you're going to see the bold formatting and the bot text. If you want to remove or alter that, you can simply take this part out and we're going to remove that. But the message starts with bot here by default. And then the ChatGPT responds, Borat is a fictional character created and portrayed by the comedian Sacha Baron Cohen. Yada, yada, yada. And then we get the rest of the essay. So again, it worked. And then if I follow up here with why is he so popular? Question mark. As the script also remembers the chat history, you're gonna see that this answer is contextual and it talks about. Borat has gained a massive following and become an iconic character because of his unique comedic style, AKA it remembers that we meant Borat without mentioning it in the last message. This is getting pretty cool, right? They're nice, but the next script is really what this is all about. Because if you want to show ChatGPT on the phone to some people, one of the coolest things you can do, trust me on this, is make this third script work. And it's really not that hard. It looks a little more complicated, but again, you can just copy paste all of this and learn it later. I do want to say one extra thing about understanding this code here, because I feel like it's important, and that is you can always take this function, copy it into GPT4 and ask, what does this do? And it's going to tell you. And you always have the ability to follow up with more questions, and that's how you learn. But for me personally, my favorite favorite way is just making things work somehow, getting excited from the results and then learning how all the details work. So you can do the same here. Just open up the third script and again we're going to need our OpenAI API key up here, the Telegram API key. Oh, and of course, if we go into the terminal again, we'll have to press Control C to stop the last script from running, because otherwise the second script bot would still be active on our phone. And here we have Super Telegram GPT, a helpful Telegram bot who. Who is also extremely funny and very confident. And you like to troll people a bit. All right, so just like before, this function allows us to send messages to Telegram and gets us the ChatGPT response. What is completely new though, is this following function. So this is what you need to send voice messages to the Telegram API. And it's not just that. Within here hides one thing that I need you to be aware of, and that is the so called whisper API. Whoa, a new thing. What is that? Very simple. OpenAI offers multiple APIs. One of them is the ChatGPT API. And besides the older GPT3 API and the newer GPT4 API, they also have a completely different product called Whisper, which is all about speech to text. That means it takes the words from a sound file and it turns them into a transcript, and then it takes the transcript and sends it to ChatGPT. Because ChatGPT doesn't take voice, it only takes text. Right? So again, we allow the Telegram chatbot to accept your voice, then we transform the voice into text, and then we send that text to chatgpt and get a text reply. And that is it. Kind of. I left out one thing which is not essential to understand, but I want to mention it anyway, and that is when you record voice messages inside of Telegram, they come in this Ogg format, which is just an alternative to MP3. And Whisper wants MP3s, which you're probably familiar with. And that's why here at the top, we had to import the moviepie package, which allows us to transform the ogg which to MP3. So really it's your voice to the Telegram voice function, which turns it into Ogg. Then we transform that into MP3. That MP3 file of your voice message goes to the whisper API, which turns it into text, and then we send the text to the ChatGPT API, which returns a text response. Okay, I hope that makes sense. And if you want to go even deeper Again, open up the Telegram documentation. Or ideally, use GPT4 to dissect this line by line and use the prompt expl. Explain this code step by step to a beginner. All right, enough talk. Let's do this. Let's press play on the script and let's see if this works. Write me an essay about Borat. And here it's set up to give you a message that lets you know that it received the voice message. I also included the line that shows you exactly what the voice message got translated to through Whisper. In this case, it's perfect. Write me an essay about Borat. And there you go. Borat Sagadiev, portrayed by Sacha Baron Cohen, is a fictional character that has taken the world by storm. Perfect. We made it work. How could I be more like Borat? And again, it says, I've received a voice message. Please give me a second to respond. How could I be more like Borat? And the cool thing is that if you look at your computer in this terminal, you'll see all the actions that are happening. So A second ago, Moviepie was writing audio in VoiceMessage MP3. And also, as you'll see in the folder here above, it saves the different voice messages that you sent to this bot. And there you go. A bunch of recommendations on how could I be more like Borat? While Borat's antics might be entertaining to watch, it's important to remember that his behavior can often be inappropriate and offensive. Yeah, fair enough, but that's why he's funny. Anyway, so that works. Isn't that incredible? But given I have to keep my computer open or run this on my desktop at home, which is my usual workflow. But now the obvious question presents itself. How do you go further than this? How can you make this run 24? 7? So this telegram bot is operational all the time independently of you running VS code on your computer at home? Well, but I fear that goes beyond the scope of this tutorial. You would need to host this python script on a service like aws, which is just not beginner friendly enough for me to include it in this video. But I'll include a link in the description below to another tutorial that explains this really well. But hey, if you want me to create a tutorial like that, just leave a comment below. I can do that too. And with that, I think we got episode two out of three in this mini series that shows you how to work with the ChatGPT API. I'm currently thinking of making a third episode. And the idea there is showing you how to get text to speech back from the bot. So you speak the robot and it speaks back. If that's something you're interested in, leave a comment below. Otherwise, share your ideas, what other tutorials you would like to see, and otherwise, I hope you have a lot of fun with this little telegram custom bot. And all I would say is show it to your friends, show it to your family. This is something that 99% of ChatGPT users will never achieve, so go ahead and use it to your advantage and check out the next video. I'll see you there.










