Using CHATGPT api and python.
Over the past couple of days, I have become obsessed with the idea of making a program that when given a prompt such as; “email Chris detailing that I have fixed the projector in the upstairs meeting room, make it sound professional and wordy” it would automatically email Chris with an Ai generated response.
So far I have got it to send an email with Ai generated text but I have to manually set the destination email address.
I want it to automatically see that I want to send the email to Chris and pull his email from either my contacts or a global work address list, maybe even prompt me if it can’t find a match or, if there are multiple matches prompts me.
Perhaps later down the road i can add a voice to text feature…
I will paste the code below, I am still working on it but at least it emails 🙂 some passwords are omitted 😉
import os
import openai
import requests
import json
import re
import smtplib, ssl
#----------------------------------START INPUTS
inp=str(input("\nEnter Email Prompt: "))
emSender="***********************************"
appPassword="**************************"
emReceiver=str(input("""\nWho are you sending the Email to?
If you are sending the email to multiple people please seperate the addresses with a comma: """))
#----------------------------------END INPUTS
#----------------------------------START HTTPS REQUEST
headers = {
# Already added when you pass json=
# 'Content-Type': 'application/json',
'Authorization': '******************************************************************',
}
json_data = {
'model': 'text-davinci-003',
'prompt': inp,
'temperature': 0,
'max_tokens': 1000,
}
response = requests.post('https://api.openai.com/v1/completions', headers=headers, json=json_data)
#----------------------------------END HTTPS REQUEST
#----------------------------------START EXTRACTING DATA FROM JSON
x=response.json()
choices=x['choices']
text=choices[0]['text']
text=re.findall(r"(.*\n)", text) #REMOVING FROM NAME
text=''.join(text) #TURNING IT BACK INTO A STRING WITHOUT FROM NAME
if "Subject: " not in text:
text="Subject: N/A\n"+text
subject=re.findall(r"Subject: (.*?\n)", text)
subject=''.join(subject)
text=text.replace("Subject: ","")
text=text.replace(subject, "")
emailtext=f"""Subject:" {subject}
To: {''.join(emReceiver)}
{text}"\nRobert"""
context=ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(emSender, appPassword)
server.sendmail(emSender, emReceiver, emailtext)
# print(text)
# print(x)
# print(emailtext)