Computechsolution0
import os
from flask import Flask, render_template, request
from twilio.rest import Client
from gunicorn.app.base import BaseApplication
app = Flask(__name__)
@app.route("/", methods=['GET'])
def home():
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID', 'Not Set')
twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN', 'Not Set')
return render_template('index.html',
twilio_account_sid=twilio_account_sid if twilio_account_sid != 'Not Set' else None,
twilio_auth_token=twilio_auth_token if twilio_auth_token != 'Not Set' else None)
@app.route("/wa", methods=['POST'])
def wa_hello():
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
if not account_sid or not auth_token:
return "Twilio credentials are not set. Please set them in the Env Secrets tab.", 200
Created: | Last Updated:
Here's a step-by-step guide for using the WhatsApp Bot template:
Introduction
This template provides a simple WhatsApp bot using Twilio's API. The bot sends a welcome message when a user sends a message to your Twilio WhatsApp number.
Getting Started
- Click "Start with this Template" to begin using this WhatsApp bot template in Lazy.
Initial Setup
To use this template, you'll need to set up a Twilio account and configure some environment variables:
-
Sign up for a Twilio account at https://www.twilio.com if you haven't already.
-
Once logged in, navigate to the Twilio Console to find your Account SID and Auth Token.
-
In the Lazy Builder, go to the Environment Secrets tab and set the following variables:
TWILIO_ACCOUNT_SID
: Your Twilio Account SIDTWILIO_AUTH_TOKEN
: Your Twilio Auth Token
Test
After setting up the environment variables:
- Click the "Test" button in Lazy to deploy your WhatsApp bot.
Configuring Twilio
To connect your Twilio account with this bot:
-
In the Twilio Console, navigate to the WhatsApp Sandbox.
-
Set up your sandbox number if you haven't already.
-
In the WhatsApp Sandbox settings, find the "When a message comes in" webhook URL field.
-
After clicking "Test" in Lazy, you'll receive a dedicated server link. Set the webhook URL to:
https://your-lazy-app-url.com/wa
Replaceyour-lazy-app-url.com
with the actual URL provided by Lazy.
Using the App
Once your bot is set up:
-
Send a message to your Twilio WhatsApp Sandbox number.
-
You should receive a welcome message in response: "Welcome to our WhatsApp Bot!"
Customizing the Bot
To customize the bot's response:
-
In the Lazy Builder, locate the
wa_hello()
function in themain.py
file. -
Modify the
body
parameter in theclient.messages.create()
method to change the welcome message. -
Click "Test" again to deploy your changes.
Remember, this is a basic template. You can expand its functionality by adding more complex logic, integrating with other services, or handling different types of user inputs.