Commands

Command Usage

/img Command

export const imgCommand: CommandHandler = {
  name: 'img',
  description: 'Generate an image using DALL-E'
};

Usage Examples

/img A cute cat playing with a ball of yarn
/img A futuristic cityscape at sunset
/img An abstract painting of emotions

Parameters

  • prompt: Image description (3-1000 characters)

  • Optional style enhancements automatically applied

Response

  • Processing message

  • Generated image

  • AI-generated caption

  • Error message (if applicable)

Function Calling

Natural Language Triggers

  • "Generate an image of..."

  • "Create a picture showing..."

  • "Show me an illustration of..."

  • "Make an image depicting..."

Implementation

case "generate_image":
  return await generateAndSendImage(
    agent,
    chatId,
    prompt,
    messageId,
    userId
  );

Command Handler

Validation

if (!imagePrompt.trim()) {
  return {
    text: "Please provide a prompt!\n\nExample: /img A cute cat playing with a ball of yarn",
    success: false
  };
}

if (imagePrompt.length < 3) {
  return {
    text: "❌ Prompt too short. Please provide a more detailed description.",
    success: false
  };
}

if (imagePrompt.length > 1000) {
  return {
    text: "❌ Prompt too long. Please keep it under 1000 characters.",
    success: false
  };
}

Processing

// Send processing message
const processingMessage = await sendProcessingMessage(
  'image',
  userId,
  chatId,
  agent
);

// Generate image and caption in parallel
const [imageUrl, caption] = await Promise.all([
  generateImage(imagePrompt),
  generateImageCaption(imagePrompt, agent)
]);

Response Handling

// Download image
const imageResponse = await fetch(imageUrl);
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());

// Send to chat
await sendTelegramPhotoMessage(
  agent,
  chatId,
  imageBuffer,
  caption
);

// Store the post
await storePost({
  type: 'image',
  content: caption,
  timestamp: Math.floor(Date.now() / 1000),
  prompt: imagePrompt,
  imageUrl
});

Error Handling

User Errors

  • Empty prompt

  • Prompt too short/long

  • Content policy violations

  • Rate limiting

System Errors

  • API failures

  • Storage errors

  • Network issues

  • Buffer handling

Error Messages

return {
  text: error instanceof Error 
    ? error.message 
    : "❌ Failed to generate image. Please try again later.",
  success: false
};

Best Practices

Prompt Writing

  • Be specific and detailed

  • Avoid prohibited content

  • Use clear descriptions

  • Consider style elements

Command Usage

  • Respect rate limits

  • Check error messages

  • Verify image generation

  • Monitor response time

Function Integration

  • Handle all error cases

  • Implement timeouts

  • Verify storage

  • Track metrics

Last updated