need the Telegram app, so download that first. Once you've 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 "/newbot" and then the BotFather is going to ask you, "Alright, a new bot. How are we going to call it? Please choose a name for your bot. " So I'm going to say "AI Advantage9001". Good, now let's choose a username for your bot. It must end in "bot" like this, for example, "Tetrisbot" or "Tetris_bot". So we'll say "AI_Advantage_9001_bot". For example, Tetrisbot or Tetris_bot. So, we'll say AI Advantage9001_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. Also, what you'll need here is the 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 gotta 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 presets that make your life really simple in here. So obviously, with the OpenAI package to make ChatGPT work, and then also with 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 the 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, and 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. Alright, so 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 "requirements" file in here. 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 "pip3 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 BotFather, it links to our new bot. So just 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 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 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 their 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 at its lowest $3. 99 a month, 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! The 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, 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 case 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's video. So, let's move to the second script here. Zero two simple chat bot does the same thing as previously,
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 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 gonna 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 Ctrl+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 is also extremely funny and very confident and likes to throw people a bit. Alright, 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 GPT-3 API and the newer GPT-4 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 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 dot OGG format, which is just an alternative to MP3. And Whisper wants MP3s, which you're probably familiar with. And that's right here at the top. We had to import the movie Pi package, which allows us to transform the OGG 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 'Explain this code step by step to a beginner. ' Alright, 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 for Whisper. In this case, it's 'Write me an essay about Borat. ' And there you go. 'Borat Sagdiyev, 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, MoviePy was writing audio/voice_message. 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 will 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 2 out of 3 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 to the bot, and it speaks back. If that's something you're interested in, leave a comment below. Otherwise, share your ideas. What other tutorials would you 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!