Skip to content

Commit

Permalink
Enhance logging and configuration for WhatsApp integration
Browse files Browse the repository at this point in the history
- Added logging for processed responses and character count in classifier.
- Implemented truncation for results exceeding 2499 characters.
- Expanded .env.example with additional configuration options for WhatsApp bot.
- Updated README.md for clearer setup instructions and usage guidelines.
  • Loading branch information
Luisotee committed Dec 17, 2024
1 parent 1a0a711 commit b8b5393
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
11 changes: 11 additions & 0 deletions apps/ai_api/eda_ai_api/api/routes/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def process_llm_response(message: str, response: str) -> str:
processed = llm.complete(
RESPONSE_PROCESSOR_TEMPLATE.format(original_message=message, response=response)
)
logger.info(f"Processed response: {processed.text}")
return processed.text


Expand Down Expand Up @@ -183,6 +184,16 @@ async def classifier_route(
else:
final_result = str(result)

# Truncate if result exceeds character limit
if len(final_result) > 2499:
logger.warning(
f"Result exceeded 2499 characters (was {len(final_result)}). Truncating..."
)
final_result = final_result[:2499]

# Log both result and character count
logger.info(f"Final result ({len(final_result)} chars): {final_result}")

await zep.add_conversation(session_id, combined_message, final_result)
return ClassifierResponse(result=final_result, session_id=session_id)

Expand Down
35 changes: 34 additions & 1 deletion apps/whatsapp/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
TRIGGER_SECRET_KEY=
TRIGGER_API_URL=
MONGODB_URI=
PORT=4000
PUPPETEER_EXECUTABLE_PATH="/snap/bin/chromium"

# Whatsapp

# This is how the bot will prefix its messages when answering to commands
# or when replying to itself (e.g. when you run the bot in your own personal whatsapp account)
# Note: must be different from CMD_PREFIX and cannot be empty
BOT_PREFIX="*[BOT]:*"

# This is how the user should prefix their messages when issuing commands to the bot
CMD_PREFIX="!"

# The assistant's name. Call it whatever you want.
BOT_NAME="Sydney"

# Accepted values are "true", "dms_only", "groups_only" or "false"
ENABLE_REACTIONS="true"

# The bot will only reply to these users. Leave this commented to allow everyone to use the bot.
# See the readme.md file to learn how this works.
#ALLOWED_USERS="" # Example: "5511999999999,14155551111" where 55 is the country code, 11 is the area code, and the rest is the phone number.

# The bot will ignore these users. Leave this commented to allow everyone to use the bot.
# See the readme.md file to learn how this works.
#BLOCKED_USERS="" # Example: "5511999999999,14155551111" where 55 is the country code, 11 is the area code, and the rest is the phone number.

# The "Too many unread messages..." warning when the bot starts.
IGNORE_MESSAGES_WARNING="false" # Accepted values are "true" or "false"

# Change to your liking
QUEUED_REACTION="🔁"
WORKING_REACTION="⚙️"
DONE_REACTION="✅"
ERROR_REACTION="⚠️"
48 changes: 43 additions & 5 deletions apps/whatsapp/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
# ai-whatsapp
# Whatsapp

A WhatsApp integration module for the Earth Defenders Assistant platform using Baileys library.

## Requirements

- [Bun](https://bun.sh) v1.1.24 or higher
- Node.js environment
- WhatsApp account with an active phone number

## Environment Setup

1. Create a `.env` file in the root directory by copying `.env.example`:
```bash
cp .env.example .env
```

2. Configure the environment variables in your `.env` file.

## Installation

To install dependencies:

1. Go into the repo's main folder `./earth-defenders-assistant` and use:
```bash
bun install
```

To run:

2. Then use:
```bash
bun run index.ts
bun dev:whatsapp
```

This project was created using `bun init` in bun v1.1.24. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
## First-time Setup

1. When you first run the application, a QR code will appear in your terminal
2. Scan this QR code with your WhatsApp mobile app
3. Wait for the authentication process to complete
4. The bot will be ready to receive and process messages once connected

## Usage

- Use the configured `CMD_PREFIX` (default: "!") to send commands to the bot
- The bot will respond with messages prefixed with the configured `BOT_PREFIX`
- Reactions can be enabled/disabled using the `ENABLE_REACTIONS` setting

## Project Structure

This project is part of the Earth Defenders Assistant platform and uses:
- `@whiskeysockets/baileys` for WhatsApp integration
- Trigger.dev for workflow automation

For more information about the Earth Defenders Assistant platform and its components, please refer to the main project documentation.

0 comments on commit b8b5393

Please sign in to comment.