Build AI WhatsApp Bots with Pure Python

Oct 16, 2022 | Educational

Creating a WhatsApp bot can seem daunting, especially if you’re new to programming. However, with the right tools and a little guidance, it can turn into an exciting adventure! This article will take you step by step through the process of building a WhatsApp bot using the Meta Cloud API with pure Python and Flask.

Prerequisites

Get Started

Before diving in, let’s get everything lined up:

Step 1: Select Phone Numbers

Ensure WhatsApp is added to your app. You will start with a test number that can reach 5 users. Here’s how:

  • Locate the test number in the API setup.
  • Add your own WhatsApp number and verify it via the code received.

Step 2: Send Messages with the API

Once you’ve set up your number, it’s time to send that first message:

  • Obtain a 24-hour access token from the API access section.
  • Transform the provided curl command into a Python function.
  • Create and update a .env file based on example.env.
  • Expect a Hello World message after a brief delay!

To maintain access beyond 24 hours, follow the guide to create a system user and generate a token. Be diligent about permissions to avoid errors.

Step 3: Configure Webhooks to Receive Messages

This phase is often the trickiest, as webhooks need meticulous configuration. Here’s your roadmap:

  • Start your app: Ensure Python is installed and run run.py.
  • Launch ngrok: This allows you to expose your localhost to the internet. Follow these steps:
    • Sign up for ngrok and get your authtoken from the ngrok dashboard.
    • Run the command: ngrok http 8000 --domain your-domain.ngrok-free.app.
  • Integrate WhatsApp: Enter the ngrok URL into the Callback URL field in your Meta App Dashboard.

Step 4: Understanding Webhook Security

Webhook security involves verifying that requests are genuinely from WhatsApp. Here’s a brief overview:

  • Verification requests trigger a GET request that includes a hub.verify_token. Match this against your stored value.
  • Respond with the hub.challenge value to validate the request.
  • Secure payloads using SHA256 hashing to ensure integrity.

Step 5: Learn about the API and Build Your App

Take some time to review the developer documentation and learn how to enhance your app further. Here’s the documentation.

Step 6: Integrate AI into the Application

To make your bot stand out, integrate AI:

  • Establish your AI’s generate_response() function.
  • Set up the OpenAI API and configure your OPENAI_API_KEY.
  • Modify your application to leverage AI in responses, enhancing user interaction.

Step 7: Add a Phone Number

Once you’re ready, connect a production phone number:

  • Manage your WhatsApp accounts via the Overview page.
  • Options for test numbers include buying a new SIM, using virtual numbers, or dedicated devices for development.

Choose wisely based on your needs!

Troubleshooting

If you encounter issues:

  • No Messages Received: Ensure your app is correctly set up to listen for webhooks and ngrok is running.
  • Invalid Tokens: Double-check the generated tokens and stored values.
  • Permission Error: Verify you have configured all necessary permissions.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox