This documentation explains how to set up and run the CRM Telegram Bot system consisting of two main components:
__main__.py- Telegram bot interfacerun_langgraph.py- CRM backend system with LangGraph
The system is a customer relationship management (CRM) bot that operates through Telegram. It consists of:
- Telegram Bot Frontend (
__main__.py): Handles user interactions via Telegram - CRM Backend (
run_langgraph.py): Manages customers, plans, sales, and products using AI agents
- Python 3.8 or higher
Install the following packages using pip:
pip install pyrogram
pip install langchain-openai
pip install langgraph
pip install sentence-transformers
pip install faiss-cpu # or faiss-gpu if you have GPU support
pip install numpy
pip install sqlite3 # Usually built-in with PythonOr install all at once:
pip install pyrogram langchain-openai langgraph sentence-transformers faiss-cpu numpyIn run_langgraph.py, you need to set your OpenAI API key:
os.environ["OPENAI_API_KEY"] = "your-openai-api-key-here"Create a file named tg_keys.py in the same directory with the following structure:
def get_api_keys():
return [
"app_name", # Your app name
12345678, # Your Telegram API ID
"your_api_hash" # Your Telegram API hash
]To get Telegram API credentials:
- Go to https://my.telegram.org/auth
- Log in with your phone number
- Go to "API Development tools"
- Create a new application to get API ID and API hash
Ensure your project has the following structure:
project_folder/
├── __main__.py
├── run_langgraph.py
├── tg_keys.py
├── data/ # Will be created automatically
├── chats.txt # Will be created automatically
└── customers_history.json # Will be created automatically
The system uses SQLite database which will be created automatically in the data/ folder when you first run the system.
To start the complete system with Telegram interface:
python __main__.pyThis will:
- Initialize the CRM backend system
- Start the Telegram bot
- Create necessary database tables
- Initialize vector search indexes
To test the CRM backend without Telegram:
python run_langgraph.pyThis will:
- Initialize the CRM system
- Run a test query
- Show that the system is ready
- Add new customers
- Search customers by any field
- Update customer information
- Remove customers
- List all customers
- Create new plans/meetings
- Search plans by any field
- Update plan information
- Delete plans
- List all plans
- Record new sales
- Search sales by any criteria
- Update sales information
- Remove sales records
- Generate sales reports
- Add new products
- Search products by any field
- Update product information
- Remove products
- List all products
- Total sales reports by period
- Sales by customer analysis
- Sales by item analysis
- Sales trend analysis
- Top performers reports
In __main__.py, update the owner ID to your Telegram user ID:
if sender_id == 7883900921: # Replace with your Telegram IDThe system uses a reference date for testing. In run_langgraph.py:
reference_date = datetime(2025, 5, 20) # Adjust as neededSend messages to the bot such as:
- "Add customer John Doe with phone 123-456-7890"
- "Show all customers"
- "Create plan meeting with John tomorrow at 3 PM"
- "Add sale laptop $1000 to John today"
- "Show sales report for this month"
Regular users can send messages which will be forwarded to the bot owner with AI analysis.
- Import Error: Make sure all dependencies are installed
- API Key Error: Verify your OpenAI API key and Telegram credentials
- Database Error: Check if the
data/folder has write permissions - Connection Error: Ensure internet connection for API calls
The system outputs debug information to console. Monitor the console output for any errors.
customers_history.json: Stores customer chat historieschats.txt: Temporary file for message passingdata/crm.db: SQLite database with all CRM data
The system uses sentence-transformers for semantic search. You can modify the model in run_langgraph.py:
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')Change the OpenAI model in run_langgraph.py:
llm = ChatOpenAI(
model="gpt-4o-mini", # Change to gpt-4, gpt-3.5-turbo, etc.
temperature=0.7,
streaming=True
)- Keep your API keys secure and never commit them to version control
- The bot owner ID acts as authentication - only this user can access CRM functions
- All data is stored locally in SQLite database
For issues or questions:
- Check the console output for error messages
- Verify all dependencies are installed correctly
- Ensure API keys are correctly configured
- Check file permissions for data storage
To extend the system:
- Add new tools in
run_langgraph.py - Modify the system prompts for different behavior
- Add new message handlers in
__main__.py - Extend database schema as needed