Use ChatGPT / OpenAI to generate replies
You can use the ChatGPT API by OpenAI to automatically generate SMS message and even images.
In this how-to we’ll discuss how to automatically reply to incoming messages with generated content from OpenAI.
In both cases we’ll make use of triggers to respond to incoming messages.
Also, in both cases you’ll need to have an account with OpenAI to follow this how-to. Fortunately an account with OpenAI is free and you’ll automatically receive some credits to test your triggers.
We’ll start out with automatically generating text reply. 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\\SMS Server\\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. To make sure that it works please replace: ‘[YOUR_API_SECRET]’ with the API secret from your OpenAI dashboard.
To generate the reply the trigger attached the text from your SMS body to a specially crafted prompt. When sending SMS messages the resulting message length is important. Ideally the message length should stay below 160 characters to make sure that you’re not sending out multipart messages. ChatGPT is unfortunately terrible at doing math so you’ll have to apply a small margin when instructing about the length of it’s reply.
After receiving a reply from ChatGPT we create a new SMS message with the text of the reply. We’ll send it to the sender address of our incoming message.
And that’s it. You can now interact with ChatGPT directly through text messages.
ChatGPT generated images
Create a new JavaScript trigger
STR_DEBUGFILE = "C:\\ProgramData\\Auron\\SMS Server\\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 uses the OpenAI API. As with the previous example, please replace: ‘[YOUR_API_SECRET]’ with the API secret from your OpenAI dashboard.
In this case the trigger generates a reply in two steps. First it forward the prompt from the e-mail to the OpenAI API.
OpenAI responds with an URL to the generated image. You’ll have to make a second HTTP request to retrieve this image and save it in a temporary location.
In this case it’s save to overwrite the previous image since triggers always run in sequence to each other.
We’re using e-mails in this example but as you can see it’s easy to adapt this example to use WhatsApp or RCS.