twit icon indicating copy to clipboard operation
twit copied to clipboard

Tweet from a JS file

Open richardstweets opened this issue 3 years ago • 11 comments

Does anyone know how I tweet from another JS File ?

My index file looks likes this:

console.log('Starting');

var Twit = require('twit');

var config = require('./config'); var T = new Twit(config);

var tweet = {
	status:'Hello World',
}

T.post('statuses/update', tweet, tweeted);

function tweeted(err, data, response) {
	if (err) {
		console.log("Something went wrong!");
	} else {
		console.log("It worked!");
	}

I'm new to this and all I want to do is say tweet from another js file called tweets that say has a list of tweets in it like the below:

'This is a test tweet' 'This is a test tweet 2' 'This is a test tweet 3'

and I want it to tweet them from top to bottom and also with an interval say like every hour.

Can anyone help with this ?

Thanks

richardstweets avatar Nov 19 '22 17:11 richardstweets

something like this, but this is quick and crude psuedocode

setInterval(function(){ node nameOfscript.js one two three four five },60000)

//inside nameofscript.js const myArgs = process.argv.slice(2); console.log('myArgs: ', myArgs); myArgs[0] = one . . myArgs[4] = five

cparello avatar Nov 20 '22 00:11 cparello

Hey thanks. but i think i'm doing something wrong.

my index file:

console.log('Starting');

const Twit = require('twit');

const config = require('./config'); const T = new Twit(config);

setInterval(function(){

node tweets.js one two three four five },60000)

T.post('statuses/update', tweet, tweeted);

function tweeted(err, data, response) {
	if (err) {
		console.log("Something went wrong!");
	} else {
		console.log("It worked!");
	}

}

My tweets file:

const myArgs = process.argv.slice(2); console.log('myArgs: ', myArgs); myArgs[0] = one . . myArgs[4] = five

'This is a test tweet' 'This is a test tweet 2' 'This is a test tweet 3'

richardstweets avatar Nov 20 '22 18:11 richardstweets

try something like this just to understand how command line args work, then copy all of your tweeting code into testTweet.js. your setinterval should be in index.js and your tweet code should be in the second file. you can also spawn a child process. google node child process

//index.js

let testTweet = 'this is a test'; node tweets.js + ' ' + testTweet;

//tweetTest.js

console.log('Starting');

const myArgs = process.argv.slice(2); let testTweet = myArgs[0]; console.log('testTweet : ', testTweet );

cparello avatar Nov 20 '22 19:11 cparello

I seem to get an error again, do i need to remove something from the index file ?

Index.js file:

console.log('Starting');

const Twit = require('twit');

const config = require('./config'); const T = new Twit(config);

let testTweet = 'this is a test';

node tweets.js + ' ' + testTweet

T.post('statuses/update', tweet, tweeted);

function tweeted(err, data, response) {
	if (err) {
		console.log("Something went wrong!");
	} else {
		console.log("It worked!");
	}

}

tweet.js file:

const myArgs = process.argv.slice(2); let testTweet = myArgs[0]; console.log('testTweet : ', testTweet );

richardstweets avatar Nov 20 '22 19:11 richardstweets

what error?

cparello avatar Nov 20 '22 19:11 cparello

C:\Users\richa\Documents\TwitBot>node index.js C:\Users\richa\Documents\TwitBot\index.js:9 node tweets.js + ' ' + testTweet ^^^^^^

SyntaxError: Unexpected identifier ←[90m at Object.compileFunction (node:vm:360:18)←[39m ←[90m at wrapSafe (node:internal/modules/cjs/loader:1088:15)←[39m ←[90m at Module._compile (node:internal/modules/cjs/loader:1123:27)←[39m ←[90m at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)←[39m ←[90m at Module.load (node:internal/modules/cjs/loader:1037:32)←[39m ←[90m at Module._load (node:internal/modules/cjs/loader:878:12)←[39m ←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m ←[90m at node:internal/main/run_main_module:23:47←[39m

richardstweets avatar Nov 20 '22 20:11 richardstweets

oh duh, i forgot you have to use child process

const { spawn } = require('node:child_process'); const bat = spawn('node', ['tweets.js', 'testTweet']);

bat.stdout.on('data', (data) => { console.log(data.toString()); });

bat.stderr.on('data', (data) => { console.error(data.toString()); });

bat.on('exit', (code) => { console.log(Child exited with code ${code}); });

cparello avatar Nov 20 '22 21:11 cparello

Thank man, so should both my files look like this below ?

Index.js:

console.log('Starting');

const Twit = require('twit');

const config = require('./config'); const T = new Twit(config);

const { spawn } = require('node:child_process'); const bat = spawn('node', ['tweets.js', 'testTweet']);

bat.stdout.on('data', (data) => { console.log(data.toString()); });

bat.stderr.on('data', (data) => { console.error(data.toString()); });

bat.on('exit', (code) => { console.log(Child exited with code ${code}); });

let testTweet = 'this is a test';

node tweets.js + ' ' + testTweet

T.post('statuses/update', tweet, tweeted);

function tweeted(err, data, response) { if (err) { console.log("Something went wrong!"); } else { console.log("It worked!"); }

}

tweets.js:

const myArgs = process.argv.slice(2); let testTweet = myArgs[0]; console.log('testTweet : ', testTweet );

richardstweets avatar Nov 20 '22 22:11 richardstweets

I would do it like this, make index call tweets.js and have all the tweet code in tweets.js, then inside index loop or interval to repeat

index.js:

console.log('Starting');

const { spawn } = require('node:child_process');

//then i would do the setInterval here so it calls this every x secs let testTweet = 'this is a test'; const bat = spawn('node', ['tweets.js', testTweet]);

bat.stdout.on('data', (data) => { console.log(data.toString()); });

bat.stderr.on('data', (data) => { console.error(data.toString()); });

bat.on('exit', (code) => { console.log(Child exited with code ${code}); });

tweets.js:

const Twit = require('twit');

const config = require('./config'); const T = new Twit(config);

const myArgs = process.argv.slice(2); let testTweet = myArgs[0]; console.log('testTweet : ', testTweet );

// i forget how to pass testTweet into T.post but you need to do that here T.post('statuses/update', tweet, tweeted);

function tweeted(err, data, response) { if (err) { console.log("Something went wrong!"); } else { console.log("It worked!"); }

}

cparello avatar Nov 21 '22 07:11 cparello

this guy has a video series

https://www.youtube.com/playlist?list=PLRqwX-V7Uu6atTSxoRiVnSuOn6JHnq2yV

cparello avatar Nov 21 '22 07:11 cparello

This should work. const Twit = require('twit');

// Replace these with your own API keys const T = new Twit({ consumer_key: 'your consumer key', consumer_secret: 'your consumer secret', access_token: 'your access token', access_token_secret: 'your access token secret' });

// This function will be called every minute to tweet a message function tweet() { // Generate a random message const message = 'Hello, world! #Nodejs #TwitterBot';

// Post the message T.post('statuses/update', { status: message }, (err, data, response) => { if (err) { console.error(err); } else { console.log(Tweeted: ${message}); } }); }

// Call the function every minute setInterval(tweet, 60 * 1000);

smrgrg avatar Jan 06 '23 06:01 smrgrg