0

Hi team i need to fix the 12200 - Schema validation warning twilio. everything is working but i dont receive the whatsapp respond back .

enter image description here

here is the app.py code:

from helper.openai_api import text_complition
from helper.twilio_api import send_message
from twilio.twiml.messaging_response import MessagingResponse
from flask import Flask, request
from dotenv import load_dotenv
load_dotenv()

app = Flask(__name__)

@app.route('/')
def home():
    return 'All is well...'

@app.route('/twilio/receiveMessage', methods=['POST'])
def receiveMessage():
    try:
        message = request.form['Body']
        sender_id = request.form['From']

        # Placeholder code
        result = {}
        result['status'] = 1
        result['response'] = "Hi, I'm CODAI, I have received your message."
        send_message(sender_id, result['response'])
        
            
    except:
        pass
    return 'OK', 200

   

here is the openai.py code:

import os
import openai
from dotenv import load_dotenv
from twilio.twiml.messaging_response import MessagingResponse
load_dotenv()


openai.api_key = os.getenv('OPENAI_API_KEY')


def text_complition(prompt: str) -> dict:
    '''
    Call Openai API for text completion

    Parameters:
        - prompt: user query (str)

    Returns:
        - dict
    '''
    try:
        response = openai.Completion.create(
            model='text-davinci-003',
            prompt=f'Human: {prompt}\nAI: ',
            temperature=0.9,
            max_tokens=150,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0.6,
            stop=['Human:', 'AI:']
        )
        return {
            'status': 1,
            'response': response['choices'][0]['text']
        }
    except:
        return {
            'status': 0,
            'response': ''
        }
        

here is the twilio.py code:

import os
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from dotenv import load_dotenv
load_dotenv()

account_sid = os.getenv('TWILIO_ACCOUNT_SID')
auth_token = os.getenv('TWILIO_AUTH_TOKEN')
client = Client(account_sid, auth_token)


def send_message(to: str, message: str) -> None:
    '''
    Send message through Twilio's WhatsApp API.

    Parameters:
        - to(str): recipient's phone number in the format of "whatsapp:+[country code][phone number]"
        - message(str): text message to send

    Returns:
        - None
    '''

    _ = client.messages.create(
        from_=os.getenv('FROM'),
        body=message,
        to="whatsapp:" + to
    )

i need help to fix the error 12200 - Schema validation warning twilio

0 Answers0