gpt4 book ai didi

python - 将 OpenAI API 与聊天机器人的 WhatsApp Business API 集成

转载 作者:行者123 更新时间:2023-12-02 22:45:10 27 4
gpt4 key购买 nike

我们正在使用 OpenAI API 为 WhatsApp 构建一个聊天机器人,旨在能够回答向它提出的任何问题。但是,我们在尝试将 OpenAI API 与 WhatsApp Business API 集成时遇到了一些问题。这是我们用于此集成的代码:

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()

能否请您帮助我们了解我们做错了什么并提出解决问题的解决方案?

最佳答案

我也在做同一个项目,我不完全明白问题出在哪里,为了帮助你,我可以推荐我用来构建 Whatsapp Chatbot 的库:Twillio。

关于python - 将 OpenAI API 与聊天机器人的 WhatsApp Business API 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74913458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com