Benjamin blog
  • Firebase cloud functions
  • Connect to aws database
  • connect-to-raspberry-pi-np-hdmi
  • Get your logs from your mobile to your desktop
  • Twitter bot
  • Crontab
  • Delete your git history
  • How I hacked my sons Duplo train to go faster using my voice
  • Finetune GTPT-3
  • Create a localhost tunnel
  • Paste clipboard to vim through ssh in Windows
  • Predictions of the future
  • Prisma
  • Connect to Raspberry Pi no HDMI output
Powered by GitBook
On this page
  • Creating the bot
  • Programming the bot
  • Create a new Tweet
  • Follow an account
  • Like a tweet
  • Retweet a tweet

Twitter bot

Firstly you need to figure out what profile the Twitter bot will be connected to. Log in with the twitter account you want to use. If you don't want it connected to your personal then simply create a new account

Creating the bot

To create a Twitter bot go to https://developer.twitter.com/

Now click Create new app. Follow the instructions.

Remember to give the correct permission for what you want to do and also give the account a profile image and description.

Now go to Keys and tokens and get the API Key and Secret and the Access Token and Secret

This you will be using to connect nodejs to your account through a npm library called Twitter.

Programming the bot

To program the bot you can use this code

var Twitter = require("twitter");
require("dotenv").config();

var client = new Twitter({
  consumer_key: process.env.consumer_key,
  consumer_secret: process.env.consumer_secret,
  access_token_key: process.env.access_token_key,
  access_token_secret: process.env.access_token_secret,
});

client.get("search/tweets", { q: "sunshine" }).then((tweets) => {
  tweets.statuses.forEach((tweet) => {
    console.log(tweet.text);
  });
});

This just show how to search for tweets but you can do lots more. Checkout the documentation here: https://www.npmjs.com/package/twitter

Here is a link for the Twitter api: https://developer.twitter.com/en/docs/twitter-api/v1

Create a new Tweet

const statusText = client
	.post("statuses/update", {
		status: "this is a reply to a tweet @account_name",
		in_reply_to_status_id: tweetToReplyToId,
	})
	.then((result) => {
		console.log(result);
	})
	.catch((error) => {
		console.log(error);
	});

in_reply_to_status_id is because i am replying to a tweet. Remember to add the persons account name in the update

Follow an account

client
    .post("friendships/create", { screen_name: account })
    .then((result) => {
        console.log(result);
    })
    .catch((error) => {
        console.log(error);
    });

Like a tweet

 client
     .post("favorites/create", { id: tweetId })
     .then((result) => {
         console.log(result);
     })
     .catch((error) => {
         console.log(error);
     });

Retweet a tweet

client
	.post("statuses/retweet", { id: tweetId })
	.then((result) => {
        console.log(result);
    })
	.catch((error) => {
        console.log(error);
    });
PreviousGet your logs from your mobile to your desktopNextCrontab

Last updated 3 years ago