<<<<<<< HEAD
=======
A WhatsApp chatbot for StackSave - a DeFi savings platform built on Base Sepolia that enables users to save, stake, and grow their money through smart contracts, all managed via simple WhatsApp messages.
- Natural Language Processing: Uses external LLMs (OpenAI, Anthropic, or custom) for intelligent intent detection
- DeFi Operations: Deposit, withdraw, stake, and unstake funds on Base Sepolia
- Simple WhatsApp Interface: Manage your DeFi savings as easily as chatting with a friend
- Multi-LLM Support: Compatible with OpenAI, Anthropic Claude, or custom LLM endpoints
- Automatic Session Management: Persistent WhatsApp authentication
src/
├── config/ # Configuration and environment variables
├── handlers/ # Message handling logic
├── services/
│ ├── llmService.ts # LLM integration for intent detection
│ └── defiService.ts # Blockchain/DeFi operations
├── types/ # TypeScript type definitions
└── index.ts # Main bot entry point
- Node.js 18+ and npm
- WhatsApp account
- Base Sepolia testnet setup
- LLM API key (OpenAI, Anthropic, or custom endpoint)
- Smart contract deployed on Base Sepolia
-
Clone and navigate to the project:
cd chatbot -
Install dependencies:
npm install
-
Configure environment variables:
cp .env.example .env
-
Edit
.envfile with your credentials:# LLM Configuration LLM_PROVIDER=openai # or 'anthropic' or 'custom' OPENAI_API_KEY=your_openai_api_key # Base Sepolia Configuration BASE_SEPOLIA_RPC_URL=https://sepolia.base.org PRIVATE_KEY=your_private_key STAKING_CONTRACT_ADDRESS=your_contract_address # Bot Configuration BOT_NAME=StackSave Bot
npm run devnpm run build
npm start-
Run the bot:
npm run dev
-
A QR code will appear in your terminal
-
Open WhatsApp on your phone and scan the QR code:
- Go to Settings > Linked Devices > Link a Device
- Scan the QR code displayed in your terminal
-
Once authenticated, the bot will start listening for messages
Users can interact with the bot using natural language. Here are some examples:
- "What's my balance?"
- "How much do I have?"
- "Check my account"
- "Deposit 100 USDC"
- "I want to save 50 dollars"
- "Add 25 to my account"
- "Withdraw 50 USDC"
- "Take out 25"
- "I need 30 dollars"
- "Stake 100 USDC"
- "Put 50 in staking"
- "I want to stake 75"
- "Unstake 50 USDC"
- "Remove 25 from staking"
- "Stop staking 30"
- "Help"
- "What can you do?"
- "How does this work?"
LLM_PROVIDER=openai
OPENAI_API_KEY=sk-...LLM_PROVIDER=anthropic
ANTHROPIC_API_KEY=sk-ant-...LLM_PROVIDER=custom
CUSTOM_LLM_URL=http://localhost:8000/api/chatYour custom endpoint should accept POST requests with:
{
"system": "system prompt",
"message": "user message"
}And return:
{
"response": "LLM response text"
}The bot expects your staking contract to have these functions:
function deposit() public payable
function withdraw(uint256 amount) public
function stake(uint256 amount) public
function unstake(uint256 amount) public
function balanceOf(address account) public view returns (uint256)
function stakedBalanceOf(address account) public view returns (uint256)Update src/services/defiService.ts with your contract's actual ABI.
- Private Keys: Never commit your
.envfile or private keys to version control - User Wallets: The current demo derives wallets from phone numbers - this is NOT secure for production
- Production Recommendations:
- Use proper key management (HSM, KMS)
- Implement user authentication
- Use account abstraction for better UX
- Add rate limiting
- Implement transaction confirmations
- Add admin controls
- config/: Environment configuration and validation
- handlers/: WhatsApp message processing logic
- services/llmService.ts: LLM integration for natural language understanding
- services/defiService.ts: Blockchain interaction layer
- types/: TypeScript interfaces and enums
- index.ts: Main bot initialization and event handling
- Make sure no other WhatsApp Web sessions are active
- Delete the
whatsapp-sessionfolder and try again - Check your internet connection
- Clear the session folder:
rm -rf whatsapp-session - Restart the bot and scan the QR code again
- Verify your API key is correct
- Check your API quota/limits
- Ensure the LLM_PROVIDER matches your configured key
- Verify your RPC URL is correct
- Check your private key has the correct format
- Ensure your contract address is deployed on Base Sepolia
- Verify you have testnet ETH for gas fees
-
Add intent type to
src/types/index.ts:export enum IntentType { // ... existing intents NEW_INTENT = 'new_intent', }
-
Update LLM prompt in
src/services/llmService.ts -
Add handler in
src/handlers/messageHandler.ts:case IntentType.NEW_INTENT: responseMessage = await this.handleNewIntent(phoneNumber); break;
You can test intent detection without blockchain operations by setting mock values in the DeFi service.
ISC
Contributions are welcome! Please feel free to submit pull requests or open issues.
For issues and questions, please open a GitHub issue.
Built with ❤️ for StackSave - Making DeFi saving as easy as chatting
edc4d18 (Initial import of chatbot project)