Welcome to the Telegram ChatBot for Fun repository! This is a simple Telegram chatbot designed for a single Telegram group of friends to have fun. The bot uses the OpenAI GPT-3.5 Turbo model to generate creative and context-aware responses. This README provides information on setting up and using the bot.
Disclaimer: a big part of the code and text in this repo are generated by ChatGPT 😆
-
Contextual Responses: The bot generates responses based on the context of the conversation within the group, creating engaging and relevant replies.
-
Mention Detection: Mention the bot in the group chat, and it will respond with witty and fun replies.
-
Database Integration: Messages are stored in a PostgreSQL database to maintain a conversation history.
Before running the bot, you need to set up the following environment variables:
DB_CONNECTION_STRING
: The connection string for your PostgreSQL database.GROUP_ID
: The ID of the Telegram group where the bot will be active.BOT_TOKEN
: The Telegram bot token.CHAT_GPT_API_KEY
: Your OpenAI API key.
Follow these steps to set up and run the bot:
-
Clone the repository:
git clone https://github.com/your-username/telegram-chat-bot.git
-
Install the required dependencies:
pip3 install -r requirements.txt
-
Set up the required environment variables in your environment or a
.env
file (an example of a.env
file is here.env.local
).-
DB_CONNECTION_STRING
: The connection string for your PostgreSQL database. To run PostgreSQL locally using Docker, you can use the following command:docker run --name bazinga-pg -e POSTGRES_USER=bot -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bazinga_chat_bot -p 5432:5432 -d postgres:13
Make sure to update the
DB_CONNECTION_STRING
in your.env
file to point to this PostgreSQL instance -
CHAT_GPT_API_KEY
: To obtain a ChatGPT API key for using OpenAI's GPT-3.5 model, you can follow the instructions provided by OpenAI on their platform. Set the obtained ChatGPT API key as the value for this environment variable in your.env
file. -
BOT_TOKEN
: To create a Telegram bot, follow the instructions here. Once you have a bot token, set it as the value for this environment variable in your.env
file. -
GROUP_ID
: To obtain your Telegram group ID, you can use the following steps- Follow the solution from one of these articles:
- The hard way
- Add detailed message console logging to
handle_message
(log at leastmessage.chat_id
). - Add your bot to the group.
- Catch the group id in logs.
- Add detailed message console logging to
-
-
Run the
main.py
script to start the bot:python3 main.py
Here's how to interact with the bot in your Telegram group:
- Mention the bot's username in the group chat to trigger a response from the bot.
- The bot will generate creative and context-aware responses based on the previous messages in the conversation (context is the last 20 messages, look for the
CONTEXT_LEN
constant).
The bot uses a PostgreSQL database to store and manage messages. The messages
table in the database stores the following information:
id
: Message IDmsg_timestamp
: Timestamp of the messageuser_name
: Name of the user who sent the messagetext
: Message text
The database will keep only the last 20 (CONTEXT_LEN
) messages, all previous messages will be removed automatically as new messages come.
I encourage you to fork and customize the bot's behavior and responses by modifying the code in main.py
. Feel free to adjust the context length, add new response rules, change the bot's behavior to suit your group's preferences, or add multi-group support.
This project is licensed under the MIT License - see the LICENSE file for details.
This project makes use of the OpenAI GPT-3.5 Turbo model for generating creative responses.
Please use this bot responsibly and for entertainment purposes only. Do not engage in any malicious or harmful activities. Ensure that your usage complies with Telegram's terms of service.
Note: When making your code public, take precautions to handle sensitive data and API keys securely.