Integrating OpenAI API with WhatsApp Business API for chatbot
We are building a chatbot for WhatsApp using the OpenAI API, which is intended to be able to answer any question asked to it. However, we are experiencing some issues when trying to integrate the OpenAI API with the WhatsApp Business API. Here is the code we are using for this integration:
import os import requests from flask import Flask, request import openai from Testbotgpt import generate_response app = Flask(__name__) def send_message(to, text): data = { "recipient_type": "individual", "to": "whatsapp:{}".format(to), "type": "text", "text": { "body": text } } headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer <Access Token>' } api_url = 'https://api.whatsapp.com/v1/messages' response = requests.post(api_url, json=data, headers=headers) if response.status_code != 200: raise ValueError('Error sending message: {}'.format(response.text)) @app.route('/bot', methods=['POST']) def bot(): incoming_msg = request.values.get('Body', '').lower() from_number = request.values.get('From', '') responded = False # Check if the message contains a greeting if 'hi' in incoming_msg: send_message(from_number, "Hello! How can I help you today?") responded = True elif 'bye' in incoming_msg: send_message(from_number, "Goodbye! Have a great day.") responded = True # Use the GPT model to generate a response based on the user's input else: response = generate_response(incoming_msg) send_message(from_number, response) responded = True return 'OK' if __name__ == '__main__': app.run()
Could you please help us understand what we are doing wrong and suggest a solution to fix the issue?
I am also working on the same project and I am not fully understanding what the problem is, in order to help you out, I can recommend the library that I am using for building a Whatsapp Chatbot: Twillio.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.