← back to Dear Bubbe Admin

backend/auto-poster.js

103 lines

const fs = require('fs').promises;
const path = require('path');
const { postTweet } = require('/root/Projects/dear-bubbe-nextjs/lib/twitter-v2-official.js');

// Format conversation for posting
function formatForSocial(data) {
  const intros = [
    "Bubbe tells it like it is:",
    "Bubbe's got no filter:",
    "Bubbe pulls no punches:",
    "Bubbe serves reality checks:",
    "Bubbe's straight talk:",
    "Bubbe's unfiltered truth:",
    "Bubbe's no-nonsense advice:",
    "Bubbe's savage advice:",
    "Bubbe drops truth bombs:",
    "Bubbe keeps it real:"
  ];
  
  const intro = intros[Math.floor(Math.random() * intros.length)];
  
  // Anonymize user message
  let userMsg = data.userMessage || '';
  userMsg = userMsg.replace(/\b[A-Z][a-z]+\s+[A-Z][a-z]+\b/g, '[NAME]');
  userMsg = userMsg.replace(/\b[A-Z][a-z]+\b/g, (match) => {
    if (!['I', 'The', 'My', 'What', 'When', 'Where', 'Why', 'How'].includes(match)) {
      return '[NAME]';
    }
    return match;
  });
  
  // Truncate messages for Twitter
  const maxUserLength = 80;
  const maxBubbeLength = 150;
  
  if (userMsg.length > maxUserLength) {
    userMsg = userMsg.substring(0, maxUserLength) + '...';
  }
  
  let bubbeMsg = data.bubbeResponse || '';
  if (bubbeMsg.length > maxBubbeLength) {
    bubbeMsg = bubbeMsg.substring(0, maxBubbeLength) + '...';
  }
  
  const hashtags = '#BubbeAI #Bubbe #NoFilter #JewishGrandma';
  const cta = 'Visit Bubbe.AI';
  
  const fullPost = `${intro}\n\n"${userMsg}"\n\n"${bubbeMsg}" ${hashtags}\n\n${cta}`;
  
  // Ensure it fits in Twitter's limit
  if (fullPost.length > 280) {
    const shortened = `${intro}\n\n"${bubbeMsg.substring(0, 120)}..." ${hashtags}\n\n${cta}`;
    return shortened;
  }
  
  return fullPost;
}

// Post immediately to Twitter
async function postImmediately(data) {
  try {
    const text = formatForSocial(data);
    console.log('📤 Posting to Twitter immediately...');
    
    const result = await postTweet(text);
    
    if (result.success) {
      // Log successful post
      const logsPath = '/root/Projects/dear-bubbe-admin/logs/immediate-posts.json';
      let logs = [];
      try {
        const existing = await fs.readFile(logsPath, 'utf8');
        logs = JSON.parse(existing);
      } catch (e) {
        // File doesn't exist yet
      }
      
      logs.push({
        timestamp: new Date().toISOString(),
        platform: 'twitter',
        status: 'posted',
        tweetId: result.tweetId,
        url: `https://x.com/DearBubbe/status/${result.tweetId}`,
        text: text,
        originalData: data
      });
      
      await fs.writeFile(logsPath, JSON.stringify(logs, null, 2));
      console.log('✅ Posted successfully to Twitter!');
      console.log('URL:', `https://x.com/DearBubbe/status/${result.tweetId}`);
      
      return { success: true, tweetId: result.tweetId };
    } else {
      console.error('❌ Failed to post:', result.error);
      return { success: false, error: result.error };
    }
  } catch (error) {
    console.error('❌ Error posting:', error);
    return { success: false, error: error.message };
  }
}

module.exports = { postImmediately, formatForSocial };