Demonstration project that creates a Telegram bot that connects to Open AI.
This repository contains various demonstration projects related to the workshop titled "Revolucionando Telegram: Creando bots inteligentes con OpenAI". Each project demonstrates a different aspect of creating intelligent Telegram bots using OpenAI models and Python libraries.
Are you tired of reading long messages or listening to endless voice notes on Telegram? Imagine having a bot that could transcribe them quickly with minimal errors and provide you with summaries of the most important points of the conversation. Well, it's possible, and in this talk, we'll show you how from start to finish!
In this session, we will develop a Telegram bot step by step using the latest OpenAI models and Python libraries for Telegram. You don't need to be an AI expert; we will guide you every step of the way, providing practical tips to make the most of these models.
If you want to learn how to take your Telegram bots to the next level, this is the talk for you. Don't miss the opportunity to be part of this exciting evolution in artificial intelligence and messaging! We look forward to seeing you!
- Python 3.9
- Telegram Bot API token (can be obtained from the BotFather)
- OpenAI API key (can be obtained from OpenAI)
-
Basic Telegram Bot (Telebot Demo) - This demo serves as an introductory guide to building a basic Telegram bot. It lays the foundation for understanding how to create more advanced bots that leverage OpenAI models. You will learn how to set up a bot, handle user interactions, and respond to messages using pyTelegramBotAPI Python library.
Telebot demo features
- Helps to show the basic elements of a Telegram bot build with Python.
-
OpenAI demo - This demo serves to see how we can build an intelligent model making use of OpenAI's GPT models and Langchain.
OpenAI demo features
- Open AI interaction.
- Convenient and user-friendly Telegram bot interface.
-
Voice demo - This demo serves to see how we can interact both with OpenAI GPT models and Whisper.
Voice demo features
- Transcribe audio files (up to 25MB) received through Telegram in source language.
- Support for various audio formats (m4a, mp3, mp4, mpeg, mpga, wav, webm).
- Accurate transcription results using the Whisper open-source model.
- Interaction with OpenAI.
- Convenient and user-friendly Telegram bot interface.
telebot_demo.py: The main Python script containing the bot's logic.openai_demo.py: The main Python script containing the bot's logic that conversates using OpenAIvoice_demo.py: The main Python script containing the bot's logic that handles voice messages.
Ensuring you have the correct Python version is crucial for compatibility.
-
Open Terminal or Command Prompt:
- Windows: Press
Win + R, typecmd, and hit Enter. - macOS/Linux: Open the Terminal app.
- Windows: Press
-
Check Python Version:
- Type
python --versionorpython3 --versionand press Enter. - This command will display your installed Python version (e.g., Python 3.9).
- Type
If your Python version is less than 3.9, you must update it. The update process can vary significantly between operating systems, visit the Python Downloads Page for official release information.
To set up our environment, we will be using Conda. If you haven't installed Conda already, we suggest doing so as it simplifies package management and deployment. Conda can be installed as part of the Anaconda or Miniconda distribution. For installation instructions, please refer to the official Conda documentation.
To activate an Anaconda environment in your project, follow these steps:
- Open a terminal or command prompt.
- Navigate to the root directory of your project.
- Create the Anaconda environment by running the following command:
conda env create -f environment.yml- Once the environment is created, activate it using the following command:
conda activate <environment-name>By following these steps, you'll be able to activate your Anaconda environment and ensure that all the necessary packages are available for your project.
Before obtaining your API keys, it's important to set up a way to securely store and manage these keys. In this project, we use environment variables to handle sensitive information like API keys. You will need to create a .env file in the root directory of your project to store your tokens and keys. Add the following variables to this file:
BOT_TOKEN=<'your-telegram-bot-token'>
OPENAI_API_KEY=<'your-open-ai-api-key'>You can refer to the
.env.templatefile in the project as a guide for creating your.envfile.
- BOT_TOKEN: The bot token of your bot.
- OPENAI_API_KEY: The open AI API key.
Ensure to replace <your-telegram-bot-token> and <your-open-ai-api-key> with the actual values you will obtain following the instructions in the next sections.
It's crucial to keep these environment variables secure. Do not share your .env file publicly or commit it to version control. We will use python-dotenv to load these environment variables into your project.
To start building your own Telegram bot, the first step is to obtain an API token, which is a unique identifier for your bot. This token allows your bot to communicate with the Telegram API. Here’s a step-by-step guide:
-
Access BotFather: Open the Telegram app, log in and search for the BotFather, which is the official bot for creating other bots on Telegram.
-
Start a conversation: Once you find the BotFather, start a conversation with it by clicking the “Start” button.
-
Create a new bot: Type
/newbotand send it as a message to the BotFather. This command initiates the process of creating a new bot. -
Set a name for your bot: You need to set a unique username for your bot. This username must end in bot (e.g., mytestbot or example_bot).
Once the username is approved, the BotFather will provide you with your bot's API token. This token is a long string of letters and numbers. Keep it secure and do not share it with others, you will need this token when you are setting up your bot in your code.
Since you're looking to integrate OpenAI's capabilities into your application, you'll need an API key. This key serves as a unique identifier and token for authentication, allowing you to make requests to OpenAI's API. Here is how you can obtain it:
-
Visit OpenAI: Go to OpenAI Platform.
-
Sign up or log in: Create an account if you don't have one, or log in if you do.
-
Navigate to the API section: Once logged in, find the section related to API keys, typically in the dashboard or under a tab like “API” or “Developer”.
-
Create a new API Key: Look for an option to create a new API key, often labeled “Create new secret key”.
Once you submit the key name, the platform will generate a new API key. Copy it and keep it secure.
To test any of the demos locally, follow these steps:
- Open a terminal or command prompt and navigate to the
srcfolder. - In the terminal, execute the following command to start the bot:
python -m demos.<your-demo-bot-file>This file is responsible for running the bot. 3. Once the bot is running, open the Telegram app on your device. 4. Search for your bot by its username or the name you provided during the bot creation process. 5. Open a chat with the bot and interact with it!
Coming soon!
If you'd like to contribute to this repository by adding more demo projects or improving existing ones, feel free to fork the repository, make your changes, and submit a pull request. We welcome contributions from the community!
- Fork the repository.
- Create a new branch.
- Make your changes.
- Test your changes.
- Commit your changes and push them to your fork.
- Submit a pull request explaining your changes.
This project is provided as-is with no warranty or guarantee of its performance or results. Use at your own risk.