Skip to content

Latest commit

 

History

History
336 lines (226 loc) · 10.6 KB

README.md

File metadata and controls

336 lines (226 loc) · 10.6 KB

Fanvue Chatbot - Python Example

A simple chatbot application that integrates with the Fanvue API using OAuth 2.0 authentication.

Prerequisites

Before you begin, make sure you have:

  1. Python 3.8 or later installed on your computer

  2. A Fanvue account with API access

  3. An OAuth application created in the Fanvue Developer area

    • You'll need: Client ID, Client Secret, and Redirect URI

Step-by-Step Setup Guide

Step 1: Choose Your App Name

First, choose a name for your app. This will be used for your local domain. For example:

  • my-chatbot
  • fanvue-bot
  • test-app

Important: Replace your-app-name in all the following steps with the name you choose. Use lowercase letters, numbers, and hyphens only (no spaces).

Step 2: Create Your OAuth Application

  1. Go to the Fanvue Developer Area
  2. Click "Create New App" or "New OAuth Application"
  3. Fill in the application details:
    • App Name: Give it a name like "My Chatbot"
    • Redirect URI: https://your-app-name.dev:5001/callback (replace your-app-name with your chosen name)
    • Scopes: Select these scopes:
      • read:self - To read your own profile
      • read:chat - To read chat conversations
      • write:chat - To send messages
  4. Save your application and copy these values:
    • Client ID (looks like: abc123def456...)
    • Client Secret (looks like: xyz789secret...)

Note: We'll set up HTTPS in the next step, so you can come back and update the Redirect URI after generating the SSL certificate if needed.

Step 3: Install Python Dependencies

Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux) and navigate to this directory:

cd python

Install the required packages:

pip3 install -r requirements.txt

If pip3 doesn't work, try pip instead.

Step 4: Set Up HTTPS for Local Development

OAuth requires HTTPS redirect URIs, even in local development. We'll use mkcert to create local SSL certificates.

Install mkcert

On macOS:

brew install mkcert

On Linux:

# Ubuntu/Debian
sudo apt install libnss3-tools
# Then download mkcert from: https://github.com/FiloSottile/mkcert/releases

# Or use the install script:
curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64"
chmod +x mkcert-v*-linux-amd64
sudo mv mkcert-v*-linux-amd64 /usr/local/bin/mkcert

On Windows: Download from: https://github.com/FiloSottile/mkcert/releases

Generate SSL Certificates

  1. Install the local CA:

    mkcert -install
  2. Generate certificates (replace your-app-name with your chosen app name):

    mkcert your-app-name.dev

    This creates two files:

    • your-app-name.dev.pem (certificate)
    • your-app-name.dev-key.pem (private key)

Update Your Hosts File

Add your local domain to your hosts file:

On macOS/Linux:

echo "127.0.0.1 your-app-name.dev" | sudo tee -a /etc/hosts

On Windows:

  1. Open Notepad as Administrator
  2. Open C:\Windows\System32\drivers\etc\hosts
  3. Add this line: 127.0.0.1 your-app-name.dev
  4. Save the file

Step 5: Configure Environment Variables

  1. Copy the example environment file:

    cp .env.example .env
  2. Open the .env file in a text editor (Notepad, TextEdit, VS Code, etc.)

  3. Fill in your values (replace your-app-name with your chosen app name):

OAUTH_CLIENT_ID=your_actual_client_id_here
OAUTH_CLIENT_SECRET=your_actual_client_secret_here
OAUTH_REDIRECT_URI=https://your-app-name.dev:5001/callback
OAUTH_SCOPES=openid offline offline_access read:self read:chat write:chat
SESSION_SECRET=make-this-a-random-string-at-least-16-characters-long-12345
OAUTH_ISSUER_BASE_URL=https://auth.fanvue.com
API_BASE_URL=https://api.fanvue.com
API_VERSION=2025-06-26

Important Notes:

  • Replace your-app-name with your chosen app name in OAUTH_REDIRECT_URI
  • Replace your_actual_client_id_here with your actual Client ID from Step 2
  • Replace your_actual_client_secret_here with your actual Client Secret from Step 2
  • For SESSION_SECRET, use a random string. You can generate one at: https://randomkeygen.com/ (use the "CodeIgniter Encryption Keys" option)
  • Never share your .env file - it contains sensitive information!
  1. Update your OAuth application Redirect URI in the Fanvue Developer area to match: https://your-app-name.dev:5001/callback

Step 6: Run the Application

You'll need two terminal windows for this step.

Terminal 1: Start the Flask Application

In your terminal (in the python directory), run:

python3 app.py

Or if that doesn't work:

python app.py

You should see output like:

 * Running on http://127.0.0.1:5000
 * Debug mode: on

Keep this terminal window open.

Terminal 2: Start the HTTPS Proxy

Open a new terminal window, navigate to the python directory, and run (replace your-app-name with your chosen app name):

npx local-ssl-proxy --source 5001 --target 5000 --cert ./your-app-name.dev.pem --key ./your-app-name.dev-key.pem

You should see output like:

Proxying https://your-app-name.dev:5001 -> http://localhost:5000

Keep this terminal window open too.

Step 7: Access the Chatbot

  1. Open your web browser
  2. Go to: https://your-app-name.dev:5001 (replace your-app-name with your chosen app name)
  3. You may see a security warning - click "Advanced" and then "Proceed to your-app-name.dev" (this is normal for local development)
  4. Click "Login with Fanvue"
  5. You'll be redirected to Fanvue to authorize the application
  6. After authorizing, you'll be redirected back to the chatbot
  7. Start chatting!

How It Works

  1. Authentication: When you click "Login with Fanvue", the app redirects you to Fanvue's OAuth page
  2. Authorization: You authorize the app to access your Fanvue account
  3. Token Exchange: Fanvue sends back an authorization code, which the app exchanges for an access token
  4. Chat: The app uses the access token to send messages through the Fanvue API

Troubleshooting

"Module not found" error

  • Make sure you ran pip3 install -r requirements.txt
  • Try: pip install -r requirements.txt instead

"Invalid redirect URI" error

  • Make sure the Redirect URI in your Fanvue OAuth app settings exactly matches: https://your-app-name.dev:5001/callback (replace your-app-name with your chosen app name)
  • Check your .env file has the correct OAUTH_REDIRECT_URI with HTTPS
  • Verify the domain matches exactly (including the .dev extension and port 5001)

"Invalid client" or "Unauthorized" error

  • Double-check your OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET in the .env file
  • Make sure there are no extra spaces or quotes around the values

Port 5000 or 5001 already in use

  • Another application might be using port 5000 or 5001
  • For port 5000: You can change the port in app.py (last line: app.run(debug=True, port=5000))
  • For port 5001: Change the --source parameter in the local-ssl-proxy command
  • If you change ports, also update OAUTH_REDIRECT_URI in .env and your Fanvue OAuth app settings

Certificate errors in browser

  • Run mkcert -install to install the local CA certificate
  • Make sure you're accessing the app via https://your-app-name.dev:5001 (not http://)
  • Try clearing your browser cache and cookies

Cannot reach app at .dev domain

  • Verify the hosts file entry: cat /etc/hosts | grep your-app-name.dev (should show 127.0.0.1 your-app-name.dev)
  • Make sure both terminal windows are running (Flask app and SSL proxy)
  • Try accessing https://127.0.0.1:5001 instead

Proxy connection refused

  • Ensure the Flask app is running on port 5000 before starting the SSL proxy
  • Check that both terminal windows are still running
  • Verify the certificate files exist: ls -la your-app-name.dev*.pem

Messages not sending

  • Check that you selected the write:chat scope in your OAuth app settings
  • Make sure your .env file includes write:chat in the OAUTH_SCOPES

Security Notes

  • Never commit your .env file to version control (Git, GitHub, etc.)
  • The .env file is already in .gitignore to prevent accidental commits
  • Keep your Client Secret secure - treat it like a password
  • In production, use environment variables provided by your hosting service instead of a .env file

Webhooks

The chatbot includes a webhook endpoint at /webhook that can receive real-time notifications from Fanvue. According to the Fanvue Webhooks documentation, Fanvue supports 5 event types:

  • Message Received - When a new message arrives
  • New Follower - When someone follows a creator
  • New Subscriber - When someone subscribes to a creator
  • Purchase Received - When a purchase is made
  • Tip Received - When a tip is received

Setting Up Webhooks

  1. Deploy your application to a publicly accessible URL (required for webhooks)
  2. Navigate to Fanvue Developer Area → Your App → Webhooks tab
  3. Enter your webhook URL: https://your-domain.com/webhook
  4. Select the events you want to receive (e.g., "Message Received")
  5. Save and enable the webhook

Testing Webhooks Locally

For local development, use a tunneling tool like ngrok:

ngrok http 5001

Then use the HTTPS URL provided by ngrok in the Fanvue Webhooks UI.

The webhook endpoint automatically handles all event types and logs the received data to the console for debugging.

Next Steps

Once you have the basic chatbot working, you can:

  1. Customize the chatbot's responses in app.py
  2. Add more features like message history
  3. Deploy to a hosting service (Heroku, Railway, etc.)
  4. Add more sophisticated AI responses using OpenAI, Anthropic, or other AI services
  5. Implement webhook handlers to respond to real-time events

Getting Help

If you encounter issues:

  1. Check the troubleshooting section above
  2. Review the Fanvue API documentation: https://api.fanvue.com/docs
  3. Make sure all your environment variables are set correctly
  4. Check that your OAuth app settings match your .env file

File Structure

python/
├── app.py              # Main application file
├── requirements.txt    # Python dependencies
├── .env.example        # Example environment variables
├── .env               # Your actual environment variables (create this)
└── README.md          # This file