Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need Help with God Awful Twitter API #70

Open
mvansmith opened this issue Apr 6, 2023 · 0 comments
Open

Need Help with God Awful Twitter API #70

mvansmith opened this issue Apr 6, 2023 · 0 comments

Comments

@mvansmith
Copy link

This twitter api shit sucks. been troubleshooting for hours. bot doesnt work. Just literally trying to take a screenshot and upload to twitter with a tweet caption.

Here is my current code:
const fs = require('fs/promises');
const path = require('path');
const screenshot = require('screenshot-desktop');
const sharp = require('sharp'); // image resizer
const Twit = require('twit'); // twitter

const screenshotPath = path.join(__dirname, 'screenshot.png');

const TwitterObject = new Twit({
consumer_key: 'my key',
consumer_secret: 'my key',
access_token: 'my key',
access_token_secret: 'my key',
timeout_ms: 5 * 1000,
strictSSL: true,
app_only_auth: true,
base_url: 'https://api.twitter.com/2', // second version API
});

(async () => {
try {
console.log('Starting tweet process');

const screenshotBuffer = await screenshot({ format: 'png' });
const { width, height } = await sharp(screenshotBuffer).metadata();
console.log(`Screenshot captured with width ${width} and height ${height}`);

const resizedBuffer = await sharp(screenshotBuffer)
  .resize({ width: 1200, height: 1200 })
  .toBuffer();
console.log('Screenshot resized and saved');

await fs.writeFile(screenshotPath, resizedBuffer);
const imageData = await fs.readFile(screenshotPath, { encoding: 'base64' });



TwitterObject.post('media/upload', { media_data: imageData }, async (err, data, response) => {
  if (err) {
    console.error(`Error uploading media: ${err.message}`);
    return;
  }

  console.log(`Media uploaded with ID ${data.media_id_string}`);

  const mediaId = data.media_id_string;
  const tweetText =
    'Spy';

  TwitterObject.post(
    'tweets',
    {
      status: tweetText,
      attachments: { media_ids: [mediaId] },
    },
    async (err, data, response) => {
      if (err) {
        console.error(`Error posting tweet: ${err.message}`);
        console.error('Twitter error:', err.twitterReply.errors);
        return;
      }
    
      console.log('Tweet posted successfully!');
      console.log('Tweet result:', data);
      console.log('Data:', data); // Log the entire data object
    }
  );        
});

} catch (error) {
console.error(Error: ${error.message});
}
})();
And this is what returns in the terminal.: S C:\users\mattr\desktop\twitter-bot> node twitter-bot
Starting tweet process
Screenshot captured with width 3440 and height 1440
Screenshot resized and saved
Media uploaded with ID undefined
Tweet posted successfully!
Tweet result:
Data:

I hate this stupid damn new twitter api. They suck. If anyone can figure this issue out as to why there is no tweet result, data and media upload with id undefined. My god you will be a lifesaver. Good luck because it's impossible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant