Inputs and Replies
When your patch is run in chat, it can access input text, attached media files, and any messages being replied to. This guide explains how to work with these inputs effectively.
Accessing Inputs
Every patch run includes a Input object that contains any input provided when running the patch. This includes both text and media attachments.
function main() { // Access the primary text input const text = sup.input.text;
// Access the first attached image const image = sup.input.image;
// Access all text inputs as array const allText = sup.input.texts;
// Access all media by type const images = sup.input.images; const audio = sup.input.audios; const videos = sup.input.videos; const files = sup.input.files;
// Get everything combined const allContent = sup.input.combined;
return ` Text: ${text} Image: ${image} All Text: ${allText} Images: ${JSON.stringify(images)} Audio: ${JSON.stringify(audio)} `;}Input Properties
The Input object has convenient getters for accessing the first item of each type (text, image, audio, video) as well as arrays containing all items of each type (texts, images, audios, videos, files).
Additional files that aren’t images, audio, or video are available in the files array.
Working with Replies
When your patch is run in reply to another message, you can access information about the replied-to message through sup.reply.
function main() { // Check if this is a reply if (sup.reply) { // Get the replied-to message content const replyText = sup.reply.body;
// Get who wrote the message const author = sup.reply.author; console.log(`Replying to ${author.username}`);
// Access the replied-to message's inputs const replyInputs = sup.reply.input; const replyImage = replyInputs.image;
// Access the replied-to message's patch results const replyResults = sup.reply.result; }}Reply Properties
The sup.reply object contains:
body: Text content of the replied-to messageauthor: TheSupUserwho wrote the messageinput:SupInputcontaining any text/media from the original messageresult:SupInputcontaining any output from patches run in the original message
Common Patterns
Here are some common patterns for working with inputs and replies:
Processing Text Input
function main() { // Get user's input text const text = sup.input.text; if (!text) { return "Please provide some text!"; }
// Process the text return processText(text);}Working with Images
function main() { // Get attached image const image = sup.input.image; if (!image) { return "Please attach an image!"; }
// Edit the image const cursor = image.edit(); cursor.resize(800, 600); cursor.grayscale();
return cursor.render();}Handling Replies
function main() { if (!sup.reply) { return "Please use this patch in reply to a message!"; }
// Compare input with replied message const similarity = compareSimilarity(sup.input.text, sup.reply.body);
return `Messages are ${similarity}% similar`;}Processing Multiple Inputs
function main() { // Process all attached images const processedImages = sup.input.images.map((image) => { const cursor = image.edit(); cursor.resize(800, 600); return cursor.render(); });
return processedImages;}Best Practices
- Verify that required text or media is present. Return helpful messages when inputs are missing.
- Consider whether your patch should process one or many inputs. Use the array properties (texts, images, etc.) when handling multiple inputs.