[英]How to combine two scripts to work together with Twitter Bot
我有这两个单独的脚本,我想将它们结合起来与 twitter 机器人一起工作。 该机器人的目的是从一个文件夹(下图)中发送一张随机图像以及来自下面列表中的一个随机词(word1、word2 等),并且这两个脚本按照它们应该的方式单独工作。
var Twit = require('twit');
var config = require('./config.js');
var T = new Twit(config);
tweeter();
function tweeter() {
let word = ['word1', 'word2', 'word3', 'word4'];
console.log(word[Math.floor(Math.random() * word.length)]);
var tweet =word[Math.floor(Math.random() * word.length)];
// Post that tweet!
T.post('statuses/update', { status: tweet }, tweeted);
// Callback for when the tweet is sent
function tweeted(err, data, response) {
if (err) {
console.log(err);
} else {
console.log('Success: ' + data.text);
//console.log(response);
}
};
}
setInterval( function(){
tweeter();
}, 20000 );
dir = 'images'
const fs = require( 'fs' ),
path = require( 'path' ),
Twit = require( 'twit' ),
config = require( path.join( __dirname, 'config.js' ) );
const T = new Twit( config );
function randomFromArray( images ){
/* Helper function for picking a random item from an array. */
return images[Math.floor( Math.random() * images.length )];
}
function tweetRandomImage(){
/* First, read the content of the images folder. */
fs.readdir( __dirname + '/images', function( err, files ) {
if ( err ){
console.log( 'error:', err );
return;
}
else{
let images = [];
files.forEach( function( f ) {
images.push( f );
} );
/* Then pick a random image. */
console.log( 'opening an image...' );
const imagePath = path.join( __dirname, '/images/' + randomFromArray( images ) ),
b64content = fs.readFileSync( imagePath, { encoding: 'base64' } );
/* Upload the image to Twitter. */
console.log( 'uploading an image...', imagePath );
T.post( 'media/upload', { media_data: b64content }, function ( err, data, response ) {
if ( err ){
console.log( 'error:', err );
}
else{
console.log( 'image uploaded, now tweeting it...' );
/* And finally, post a tweet with the image. */
T.post( 'statuses/update', {
media_ids: new Array( data.media_id_string )
},
function( err, data, response) {
if (err){
console.log( 'error:', err );
}
else{
console.log( 'posted an image!' );
}
}
);
}
} );
}
} );
}
setInterval( function(){
tweetRandomImage();
}, 20000 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.