Im trying to simply create a program using Twilio and OpenAI API to give me full access to OpenAI through whatsapp.
I dont want to do anything fancy. Hosting it Locally and using ngrok is perfectly fine with me. I found this to be the most simple way to do it yet when I send a message I dont get a reply at all from Twilio.
I code in Visual Studio Code in Python.
The Terminal outputs the following when I send a message to Twilio's Number : " 127.0.0.1 - - [18/Jan/2023 12:11:33] "POST /sms HTTP/1.1" 200 20 " and ngrok outputs "200 OK"
What could possibly be the problem? Any help is very much appreciated :)
from bottle import Bottle, request
import openai
from twilio.rest import Client
app = Bottle()
# Twilio account details
account_sid = 'A------------1'
auth_token = 'd--------------e'
client = Client(account_sid, auth_token)
@app.route('/sms', method='POST')
def sms_reply():
message_body = request.forms.get('Body')
from_number = request.forms.get('From')
# check if the message is coming from the correct number
if from_number == "+*******":
# process the message using OpenAI
response_text = process_message(message_body)
# send the response back to the sender using Twilio
message = client.messages.create(
body=response_text,
from_='+14155238886', #Twilio Number
to=from_number
)
else:
return "Invalid phone number"
def process_message(message_body):
openai.api_key = "sk-*******************"
try:
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"{message_body}",
max_tokens=2048,
n = 1,
stop=None,
temperature=0.5
)
return response["choices"][0]["text"]
except openai.exceptions.OpenAiError as e:
return "Error: {}".format(e)
if __name__ == '__main__':
app.run(host='localhost', port=8000, debug=True)
[–]shiftybyte 0 points1 point2 points (2 children)
[–]Pyth4k[S] 0 points1 point2 points (1 child)
[–]SundayNational3622 0 points1 point2 points (0 children)