Skip to content

Instantly share code, notes, and snippets.

@jcserracampos
Last active Aug 16, 2019
Embed
What would you like to do?
Bot de twitter em nodejs
CONSUMER_KEY=
CONSUMER_SECRET=
ACCESS_TOKEN=
ACCESS_TOKEN_SECRET=
var Twit = require('twit');
require('dotenv').config();
/* Instancie o bot com as chaves no arquivo .env */
const Bot = new Twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
timeout_ms: 60 * 1000,
});
console.log('Este bot está rodando...');
/* BotInit() : Para iniciar o bot */
function BotInit() {
var query = {
q: "#guerramemeal",
result_type: "recent"
}
Bot.get('search/tweets', query, BotGotLatestTweet);
function BotGotLatestTweet (error, data, response) {
if (error) {
console.log('Bot não pôde achar o último tweet, : ' + error);
}
else {
var id = {
id : data.statuses[0].id_str
}
Bot.post('statuses/retweet/:id', id, BotRetweeted);
function BotRetweeted(error, response) {
if (error) {
console.log('Bot não pode retweetar, : ' + error);
}
else {
console.log('Bot retweetou : ' + id.id);
}
}
}
}
}
/* Configure um intervalo de 30 minutos (em microsegundos) */
setInterval(BotRetweet, 30*60*1000);
/* Inicialize o bot Bot */
BotInit();
{
"name": "guerramemeal",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "6.0.0",
"twit": "2.2.10"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment