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

Tweet from a JS file #579

Open
richardstweets opened this issue Nov 19, 2022 · 11 comments
Open

Tweet from a JS file #579

richardstweets opened this issue Nov 19, 2022 · 11 comments

Comments

@richardstweets
Copy link

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

@cparello
Copy link

cparello commented Nov 20, 2022

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

@richardstweets
Copy link
Author

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'

@cparello
Copy link

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 );

@richardstweets
Copy link
Author

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 );

@cparello
Copy link

what error?

@richardstweets
Copy link
Author

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

@cparello
Copy link

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});
});

@richardstweets
Copy link
Author

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 );

@cparello
Copy link

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
Copy link

@smrgrg
Copy link

smrgrg commented Jan 6, 2023

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);

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

3 participants