ChatGPT, Forced Moderator For Do_Not_Reply Inbox

Esemianczuk
4 min readJun 16, 2023

--

LLMs can be introduced into a web application in a variety of ways, and before I began implementing it as a core solution for any large problem, I decided first that it would be entertaining to integrate it in an entirely useless way, as a moderator for a Do_Not_Reply Inbox.

I was in the process of developing my web application for gravel-focused cycling route creation, https://sherpa-map.com, and I discovered the need for a login, something I had never programmed before. While adding all of the typical, useful, items expected in a sign-up, and heavily leveraging ChatGPT in the development process, it’s so useful I didn’t even bother installing Postman, I was struck with a hilarious idea, why not integrate ChatGPT API as a service that checks the inbox for the do_not_reply@sherpa-map.com and replies in a snarky, sarcastic manner?

In order to implement this in a Node JS backend that interfaces with Microsoft Graph to send/reply/receive emails a few key pieces were necessary:

  1. A function that listens and sends replies.
// Function to listen for replies to the "do_not_reply" mailbox
async function listenForReplies() {
const client = graph.Client.init({
defaultVersion: 'v1.0',
authProvider: (done) => {
credential.getToken('https://graph.microsoft.com/.default')
.then((tokenResponse) => {
const accessToken = tokenResponse.token;
done(null, accessToken);
})
.catch((error) => {
done(error, null);
});
},
});
try {
// Fetch unread messages in the "do_not_reply" mailbox
const messages = await client.api(`/users/${userID}/messages`).filter("isRead eq false").get();
if (messages.value && messages.value.length > 0) {
for (const message of messages.value) {
// Send a snarky reply to the sender
await sendSnarkyReply(client, message, message.from.emailAddress.address);

// Mark the message as read
await client.api(`/users/${userID}/messages/${message.id}`).patch({ isRead: true });
}
}
} catch (error) {
console.error('Error listening for replies:', error);
}

2. A main function with a never-ending loop that checks the inbox every minute by activating the “listenForReplies()” function.

async function main() {

while (true) {
await listenForReplies();
// Check for new emails every 60 seconds
await new Promise(resolve => setTimeout(resolve, 60 * 1000));
}
}

main();

3. The “sendSnarkyReply()” function which formats and sends the response to the user.

// Helper function to send a snarky reply
async function sendSnarkyReply(client, originalMessage, recipientEmail) {
const snarkyResponse = await generateSnarkyResponse(originalMessage);

const message = {
subject: 'RE: ' + originalMessage.subject,
body: {
contentType: 'HTML',
content: snarkyResponse,
},
toRecipients: [
{
emailAddress: {
address: recipientEmail,
},
},
],
};

try {
await client.api(`/users/${userID}/sendMail`).post({ message });
console.log('Snarky email sent successfully!');
} catch (error) {
console.error('Error sending snarky email:', error);
}
}

4. Lastly, the good stuff, the “snarkyResponse()” function which interprets the unread email from the loop process, sends the request to OpenAI, and returns the response.

async function generateSnarkyResponse(message) {

const apiUrl = 'https://api.openai.com/v1/chat/completions';

const emailBody = message.body.content; // or message.body.content, depending on what you want to use


try {
const response = await axios.post(apiUrl, {
model: 'gpt-3.5-turbo', // Specify the model to use
messages: [
{
"role": "system",
"content": "You are a witty, sarcastic, and snarky AI trained to humorously reply to emails sent to a do_not_reply inbox. Use your humor to create a playful response while reminding the sender that they shouldn't have emailed this inbox in the first place."
},
{
"role": "user",
"content": emailBody
}
],

max_tokens: 250, // Adjust the desired length of the generated response
temperature: 0.7, // Adjust the level of creativity in the generated response
// n: 3, // Number of responses to generate for each prompt
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
});


const { choices } = response.data;

const reply = response.data.choices[0].message.content;


// Add the disclaimer text with <br> tags for line breaks and <strong> tags for bold text
const disclaimer = '<br><br><strong>Disclaimer: This is an OpenAI bot, instructed to act a bit snarky as people don\'t normally email a do-not-reply inbox. This reply was generated by an AI, not a human being.</strong>';
// Add the bot's signature
const signature = '<br><br>--<br><strong>Snarky Bot</strong><br>Do-Not-Reply Representative<br>Sherpa LLC<br><a href="https://www.sherpa-map.com">www.sherpa-map.com</a>';

const replyWithDisclaimerAndSignature = reply + disclaimer + signature;


return replyWithDisclaimerAndSignature;
} catch (error) {
console.error('Error generating snarky response:', error);
console.log('Error response data:', error.response.data); // Add this line
return 'Oops, something went wrong!';
}
}

There may be a lot going on here, but it basically boils down to forwarding the email contents to OpenAI, utilizing the role prompt “You are a witty, sarcastic, and snarky AI trained to humorously reply to emails sent to a do_not_reply inbox. Use your humor to create a playful response while reminding the sender that they shouldn’t have emailed this inbox in the first place.” Appending a few additional details, such as a disclaimer and its own signature.

Here’s an example of a test verification email I am replying to:

Verification email which will be read by ChatGPT

Here is an example of a response:

Response from ChatGPT with a snarky attitude

What tops off the entertainment value of this is the fact it’s not limited to a one-time response, each reply to an email sends the entire email string, so it’s entirely possible to have a conversation!

--

--