Use the OpenAI API to generate replies
You can use the OpenAI API to automatically generate SMS messages and even images.
In this how to, we explain how to reply to incoming messages with generated content from OpenAI.
In both cases, we use triggers to respond to incoming messages.
In both cases, you need an account with OpenAI to follow this guide. An account is free and includes credits to test your triggers.
We start with generating text replies. Click here to jump to generating image replies.
ChatGPT generated SMS messages
Start by creating a new JavaScript trigger and copy and paste the following code:
STR_DEBUGFILE = "C:\\ProgramData\\Auron\\Auron Omni\\Log\\CHATGPT.txt"
B_ENABLE_DEBUG = true
// ========================================================================
// Function: ProcessMessageEx
// ------------------------------------------------------------------------
// ProcessMessageEx trigger function to process incoming messages
// ========================================================================
function ProcessMessageEx(objMessageIn, objMessageDB, dctContext)
{
objHttp = new ActiveXObject("MSXML2.ServerXMLHTTP")
sReflect = objMessageIn.Body
sPrompt = 'From now on all your replies are rooted in the spiritual and very' +
' indirect. Your replies cannot exceed 140 characters. Please reflect on' +
' the following: ' + sReflect
sPost = '{' +
'"model": "gpt-3.5-turbo", ' +
'"messages": [{"role": "user", "content": "' + sPrompt + '"}], ' +
'"temperature": 0.7 ' +
'}'
objHttp.open('POST', 'https://api.openai.com/v1/chat/completions', false)
objHttp.setRequestHeader('Content-Type', 'application/json')
objHttp.setRequestHeader('Authorization', 'Bearer [YOUR_API_SECRET]')
objHttp.send(sPost)
sData = objHttp.responseText
obj = eval("(" + sData + ")")
sReply = obj.choices[0].message.content
objReply = objMessageDB.Create("SMS")
objReply.ToAddress = objMessageIn.FromAddress
objReply.Body = sReply
objMessageDB.Save(objReply)
}
This trigger code uses the OpenAI API. Replace ‘[YOUR_API_SECRET]’ with the API key from your OpenAI dashboard.
To generate the reply, the trigger appends the SMS text to a crafted prompt. Message length matters for SMS. Keep replies below 160 characters to avoid multipart messages. ChatGPT is not reliable at counting characters, so include a margin in your prompt.
After receiving a reply, the trigger creates a new SMS message with the generated text. It sends the message to the original sender.
And that is it. You can now interact with ChatGPT through text messages.
ChatGPT generated images
Create a new JavaScript trigger:
STR_DEBUGFILE = "C:\\ProgramData\\Auron\\Auron Omni\\Log\\IMAGEGPT.txt"
B_ENABLE_DEBUG = true
// ========================================================================
// Function: ProcessMessageEx
// ------------------------------------------------------------------------
// ProcessMessageEx trigger function to process incoming messages
// ========================================================================
function ProcessMessageEx(objMessageIn, objMessageDB, dctContext)
{
objHttp = new ActiveXObject("MSXML2.ServerXMLHTTP")
sPost = '{' +
' "prompt": "' + objMessageIn.BodyPlainText + '",' +
' "n": 1,' +
' "size": "512x512"' +
'}'
objHttp.open('POST', 'https://api.openai.com/v1/images/generations', false)
objHttp.setRequestHeader('Content-Type', 'application/json')
objHttp.setRequestHeader('Authorization', 'Bearer [YOUR_API_SECRET]')
objHttp.send(sPost)
sData = objHttp.responseText
obj = eval("(" + sData + ")")
sUrl = obj.data[0].url
objHttp = new ActiveXObject("MSXML2.XMLHTTP")
objStream = new ActiveXObject("ADODB.Stream")
objHttp.Open("GET", sUrl, false)
objHttp.Send()
if (objHttp.Status == 200)
{
objStream.Type = 1 //binary
objStream.Open()
objStream.Write(objHttp.ResponseBody)
objStream.SaveToFile("c:\\temp\\image.png", 2) //overwrite
objStream.Close()
}
objReply = objMessageDB.Create("EMAIL")
objReply.ToAddress = objMessageIn.FromAddress
objReply.Subject = "Your image"
objReply.BodyPlainText = "Your image"
objReply.AddAttachment("c:\\temp\\image.png")
objMessageDB.Save(objReply)
}
This trigger code also uses the OpenAI API. Replace ‘[YOUR_API_SECRET]’ with your API key.
In this case, the trigger generates a reply in two steps. First, it forwards the prompt from the email to the OpenAI API.
OpenAI responds with a URL to the generated image. The trigger then makes a second HTTP request to download the image and save it to a temporary location.
It is safe to overwrite the previous image because triggers run sequentially.
This example uses email, but you can easily adapt it to use WhatsApp or RCS.
