Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active June 3, 2021 21:36
Show Gist options
  • Save doorbash/dba41e2c4e0ce65a0d08709f9276ee50 to your computer and use it in GitHub Desktop.
Save doorbash/dba41e2c4e0ce65a0d08709f9276ee50 to your computer and use it in GitHub Desktop.
const TelegramBot = require('node-telegram-bot-api');
const Twitter = require('twitter');
const sqlite3 = require('sqlite3').verbose();
var client = new Twitter({
consumer_key: '**********',
consumer_secret: '**********',
access_token_key: '**********',
access_token_secret: '**********'
});
const telegramBotToken = '**********';
const chatId = 0
let db = new sqlite3.Database('./data.db');
db.run(`CREATE TABLE IF NOT EXISTS twitter_users (
id_str TEXT NOT NULL PRIMARY KEY,
screen_name TEXT NOT NULL,
name TEXT,
location TEXT,
description TEXT,
protected BOOLEAN,
followers_count INTEGER,
friends_count INTEGER,
profile_image_url_https TEXT
);`);
db.run(`CREATE TABLE IF NOT EXISTS twitter_tweets (
id_str TEXT NOT NULL PRIMARY KEY,
user_id_str TEXT NOT NULL,
text TEXT
);`);
const bot = new TelegramBot(telegramBotToken, { polling: true });
setInterval(() => {
db.each("SELECT * FROM twitter_users", (err, row) => {
client.get("users/show", { user_id: row.id_str }, (error, user, response) => {
if (error) {
console.log(error)
return
}
let profile_pic = user.profile_image_url_https.substring(0, user.profile_image_url_https.lastIndexOf("_")) + "_400x400." + user.profile_image_url_https.substring(user.profile_image_url_https.lastIndexOf(".") + 1)
if (row.screen_name != user.screen_name) {
bot.sendMessage(chatId, row.screen_name + "'s screen_name changed from:\n\n" + row.screen_name + "\n\nto\n\n" + user.screen_name)
}
if (row.name != user.name) {
bot.sendMessage(chatId, row.screen_name + "'s name changed from:\n\n" + row.name + "\n\nto\n\n" + user.name)
}
if (row.description != user.description) {
bot.sendMessage(chatId, row.screen_name + "'s description changed from:\n\n" + row.description + "\n\nto\n\n" + user.description)
}
if (row.location != user.location) {
bot.sendMessage(chatId, row.screen_name + "'s location changed from:\n\n" + row.location + "\n\nto\n\n" + user.location)
}
if (row.protected != user.protected) {
bot.sendMessage(chatId, row.screen_name + "'s protected changed from:\n\n" + row.protected + "\n\nto\n\n" + user.protected)
}
// if (row.followers_count != user.followers_count) {
// bot.sendMessage(chatId, row.screen_name + "'s followers_count changed from:\n\n" + row.followers_count + "\n\nto\n\n" + user.followers_count)
// }
// if (row.friends_count != user.friends_count) {
// bot.sendMessage(chatId, row.screen_name + "'s friends_count changed from:\n\n" + row.friends_count + "\n\nto\n\n" + user.friends_count)
// }
if (row.profile_image_url_https != profile_pic) {
bot.sendMessage(chatId, row.screen_name + "'s profile_pic changed to:\n\n" + profile_pic)
}
db.run("UPDATE twitter_users set screen_name = ?, name = ?, location = ?, description = ?, protected = ?, followers_count = ?, friends_count = ?, profile_image_url_https = ? where id_str = ?",
[user.screen_name, user.name, user.location, user.description, user.protected, user.followers_count, user.friends_count, profile_pic, user.id_str],
err => {
if (err) {
console.log(err)
}
})
})
client.get("statuses/user_timeline", { user_id: row.id_str, exclude_replies: true, include_rts: false }, (error, tweets, response) => {
if (error) {
console.log(error)
return
}
tweets.reverse();
for (var i in tweets) {
let tweet = tweets[i]
// console.log(tweet)
// if (tweet.text.startsWith("RT ") || tweet.text.startsWith("@")) continue
db.each("SELECT COUNT(*) as c from twitter_tweets where id_str = ?", [tweet.id_str], (err, row) => {
if (err) {
console.log(err)
return
}
if (row.c > 0) return
client.get("statuses/show", { id: tweet.id_str, tweet_mode: "extended" }, (error2, tweet2, response2) => {
if (error2) {
console.log(error2)
return
}
db.run("INSERT INTO twitter_tweets VALUES (?, ?, ?)",
[tweet2.id_str, tweet2.user.id_str, tweet2.full_text],
err => {
if (err) {
if (err.code != "SQLITE_CONSTRAINT") {
console.log(err.message)
}
} else {
bot.sendMessage(chatId, tweet2.full_text + "\n\n" + tweet2.user.screen_name)
}
}
)
})
})
}
})
});
}, 20 * 1000)
bot.onText(/\/add (.+)/, (msg, match) => {
if (msg.chat.id != chatId) {
bot.sendMessage(msg.chat.id, "Unauthorized!")
return
}
let twitter_username = match[1]
client.get("users/show", { screen_name: twitter_username }, (error, user, response) => {
if (error) {
bot.sendMessage(chatId, error)
return
}
let profile_pic = user.profile_image_url_https.substring(0, user.profile_image_url_https.lastIndexOf("_")) + "_400x400." + user.profile_image_url_https.substring(user.profile_image_url_https.lastIndexOf(".") + 1)
db.run("INSERT INTO twitter_users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[user.id_str, user.screen_name, user.name, user.location, user.description, user.protected, user.followers_count, user.friends_count, profile_pic],
err => {
if (err) {
if (err.code == "SQLITE_CONSTRAINT") {
bot.sendMessage(chatId, "Duplicate!")
} else {
bot.sendMessage(chatId, err.message)
}
} else {
bot.sendMessage(chatId, "OK\n\n" + profile_pic)
}
}
)
})
});
bot.onText(/\/del (.+)/, (msg, match) => {
if (msg.chat.id != chatId) {
bot.sendMessage(msg.chat.id, "Unauthorized!")
return
}
let twitter_username = match[1]
client.get("users/show", { screen_name: twitter_username }, (error, user, response) => {
if (error) {
bot.sendMessage(chatId, error)
return
}
db.run("DELETE FROM twitter_users WHERE id_str = ?", [user.id_str], err => {
if (err) {
bot.sendMessage(chatId, err.message)
} else {
db.run("DELETE FROM twitter_tweets WHERE user_id_str = ?", [user.id_str], err => {
if (err) {
bot.sendMessage(chatId, err.message)
} else {
bot.sendMessage(chatId, "OK");
}
});
}
});
});
});
bot.onText(/\/get (.+)/, (msg, match) => {
if (msg.chat.id != chatId) {
bot.sendMessage(msg.chat.id, "Unauthorized!")
return
}
let twitter_username = match[1].toLowerCase()
client.get("users/show", { screen_name: twitter_username }, (error, user, response) => {
if (error) {
console.log(error)
return
}
let profile_pic = user.profile_image_url_https.substring(0, user.profile_image_url_https.lastIndexOf("_")) + "_400x400." + user.profile_image_url_https.substring(user.profile_image_url_https.lastIndexOf(".") + 1)
let success = () => {
bot.sendMessage(chatId, `${user.screen_name} (${user.name}) ${user.protected ? "๐Ÿ”’" : ""}
${user.description}
${user.location}
followers: ${user.followers_count}
following: ${user.friends_count}
${profile_pic}
`)
}
db.run("INSERT INTO twitter_users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[user.id_str, user.screen_name, user.name, user.location, user.description, user.protected, user.followers_count, user.friends_count, profile_pic],
err => {
if (err) {
if (err.code == "SQLITE_CONSTRAINT") {
db.run("UPDATE twitter_users set screen_name = ?, name = ?, location = ?, description = ?, protected = ?, followers_count = ?, friends_count = ?, profile_image_url_https = ? where id_str = ?",
[user.screen_name, user.name, user.location, user.description, user.protected, user.followers_count, user.friends_count, profile_pic, user.id_str],
err => {
if (err) {
console.log(err)
} else {
success()
}
})
} else {
console.log(err)
}
} else {
success()
}
}
)
})
});
bot.onText(/\/list/, (msg, match) => {
if (msg.chat.id != chatId) {
bot.sendMessage(msg.chat.id, "Unauthorized!")
return
}
db.each("SELECT * FROM twitter_users", (err, user) => {
let profile_pic = user.profile_image_url_https
bot.sendMessage(chatId, `${user.screen_name} (${user.name}) ${user.protected ? "๐Ÿ”’" : ""}
${user.description}
${user.location}
followers: ${user.followers_count}
following: ${user.friends_count}
${profile_pic}
`)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment