5.1 KiB
EndayWithOpenAI
Introduction
This project is created to fulfill one of the requirements for the selection stage of OPREC LAB SEIC ITPLN 👨🏻💻 ⚡ Firstly, I would like to express my sincere gratitude to all my friends who have provided support and offered constructive criticism and suggestions.
In this project, I implemented Chat OpenAI with a Telegram bot. The main objective of this project is to create a bot that can communicate with users through messages and provide responses based on user requests.
At the beginning of the source code, several modules and libraries are imported such as os, openai, dotenv, aiogram, and keep_alive. Then, environment variables are retrieved using the os.getenv() method, and the Telegram bot token is stored in the tgBot_token variable. The OpenAI API key is also retrieved and stored in the openAi_token variable.
Next, message handlers are defined using the dp.message_handler() decorator. There are message handlers for the /start, /command, /help, and /stop commands. The welcome function is used to provide a welcome message to the user and introduce the purpose of this project. The left function provides a list of available commands. The documentation function gives instructions and tactics for getting better responses. The gpt function is the default message handler used to send the user's prompt to the OpenAI model and generate a response that is sent back to the user.
At the end of the source code, the keep_alive() function is called to keep the bot active, and the program starts monitoring incoming messages using the executor.start_polling(dp) method.
Thus, this source code allows the development of a Telegram bot that uses the OpenAI GPT model to engage in conversations with users.
How To Install
Prerequisites
- Python 3.7. or higher. Download Here
-
Install Modul/Framework/Library
pip install ospip install openaipip install python-dotenvpip install aiogramand this optional :
pip install keep_alive
-
Clone The Repository
- Open a terminal (Command Prompt on Windows or Terminal on macOS/Linux).
- Navigate to the directory where you want to save the cloned repository (e.g., cd /path/to/folder).
- Type the git clone command followed by the copied repository URL
- Copy this : and press Enter.
git clone https://github.com/dyzulk/chatbot-telegram-with-openai.git
- Wait for a moment until the cloning process is complete. Once finished, you will have a local copy of the repository in the specified directory.
- By following these steps, you have successfully cloned the project from the GitHub repository to your local machine.
Or Download This Release
Change The API Token
tgBot_token=API_TOKEN_BOT
openAi_token=API_TOKEN_OPENAI
To replace them with valid API tokens, you need to obtain the tokens from the respective services. For example, if you want to use the Telegram Bot API token, you need to register your bot with BotFather and obtain a valid bot token from there. Similarly, if you want to use the OpenAI API token, you need to have an OpenAI account and obtain a valid API token from your account's settings panel.
Once you have the valid tokens, replace "API_TOKEN_BOT" with the provided Telegram bot token and replace "API_TOKEN_OPENAI" with the provided OpenAI token. Make sure to remove the double quotation marks around the tokens to ensure they are recognized as valid strings in the code.
By making these replacements, you will set the values of the tgBot_token and openAi_token variables according to the required API tokens for your application.
If the .env file does not exist, create a duplicate of the .env.example file then change the name to .env
Change reply message
You can change reply message on
async def welcome(message: types.Message):
await message.reply(
"FILL_CHAT_MESSAGE_HERE"
)
Or message with a separate chat bubble
async def welcome(message: types.Message):
await message.reply(
"FILL_CHAT_MESSAGE_HERE"
)
await message.reply(
"FILL_CHAT_MESSAGE_HERE"
)
Support Us
You can support me on the SociaBuzz platform! Your support would be greatly appreciated, but even starring this project would be really helpful!
