Skip to content

Instantly share code, notes, and snippets.

@lxhom
Created April 29, 2021 16:39
Show Gist options
  • Save lxhom/7e70a576a2bfe45e87762c8ebc23ae2c to your computer and use it in GitHub Desktop.
Save lxhom/7e70a576a2bfe45e87762c8ebc23ae2c to your computer and use it in GitHub Desktop.
multi-purpose twitter bot
// before using this code: replace all instances of [#...] with your keys/data/whatever and read comments starting with //#
// you can either host this on deta.sh or locally.
// to host it on deta.sh:
// [edit this code]; npm init; npm i twit axios deta express login-with-twitter; deta login; deta create .; deta deploy; deta cron set "1 minute"
const Twit = require('twit');
const axios = require("axios");
const {App} = require('deta'); //# remove this if youre running it locally
const express = require('express');
const lwt = require("login-with-twitter");
const app = App(express()); //# or const app = express() if youre running it locally
// twitter oauth
const tw = new lwt({
consumerKey: '[#yourkey]',
consumerSecret: '[#yourkey]',
callbackUrl: 'https://[#your deta url]/c'
});
app.get("/c", (req, res) => {
res.send(`
<script>
location.href = location.href.replace("/c?", "/gen?") + "&token_secret=" + localStorage.tokenSecret;
</script>
`);
});
app.get("/gen", (req, res) => {
tw.callback({
oauth_token: req.query.oauth_token,
oauth_verifier: req.query.oauth_verifier
}, req.query.token_secret, (err, user) => {
if (err) {
throw err;
}
res.send(`
<script>
location.href = "https://twitter.com/messages/compose?recipient_id=[#your twitter id]&text=" + "${escape(JSON.stringify(user))}";
</script>
`);
});
});
app.get("/", (req, res) => {
tw.login((err, tokenSecret, url) => {
if (err) {
throw err;
}
res.send(`
<script>
localStorage.tokenSecret = "${tokenSecret}";
location.href = "${url}";
</script>
`);
});
});
// twitter profile pic changer
pPicChanger = async (folder, at, ats) => {
try {
console.log("Updating photos for", folder);
let T = new Twit({
consumer_key: '[#yourkey]',
consumer_secret: '[#yourkey]',
access_token: at,
access_token_secret: ats
});
let images = fs.readdirSync(folder);
let number = (new Date).getMinutes() % images.length;
console.log(await T.post("account/update_profile_image", {image: fs.readFileSync(folder + "/" + images[number]).toString("base64")}));
} catch (e) {
console.log("Update for", folder, "failed:", e);
}
};
// quote tweeter
let quoteTweeter = async (quotesUrl, intervalMinutes, at, ats) => {
if ((new Date).getMinutes() % intervalMinutes === 0) { try {
// quotes cant be loaded from github for some fucking reason
let res = await axios.get(quotesUrl)
let lines = res.data.split("\n");
console.log(2)
let T = new Twit({
consumer_key: '[#yourkey]',
consumer_secret: '[#yourkey]',
access_token: at,
access_token_secret: ats
});
let r = await T.post("statuses/update", {status: lines[Math.floor(Math.random() * lines.length)].replace(/\@/g, "@\uFE0F").replace(/ /g, "\n")});
console.log(r)
} catch (e) {console.log(e)}}
}
// cron function (insert your commands here)
app.lib.cron(async () => { //# replace app.lib.cron with setInterval when youre running it locally
//# e.g. await quoteTweeter(...) or await pPicChanger(...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment