No response when automating Twitter direct messages to new followers using JavaScript, Twit and Twitter API (v2)

Viewed 20

I'm using the JavaScript package Twit to interact with the Twitter API (v2). I'm aiming to have it so that when I get a new Twitter follower, a direct message (DM) is automatically sent to that follower (message: "thanks for following!" etc..). I have adapted a script I found online and included the appropriate external files necessary to run this as an app on Heroku (see code below).

My 'main.js' is the important script. In it, I load Twit and a config file (containing authorisation token info [ommitted from post]). I then open a stream tracking my Twitter account (account name omitted from my post). I have generated a message (var Generate message), and then I'm sending a message (const SendMessage) when I get a new follower.

I have the app built successfully on Heroku, though when I check the logs nothing is showing when I test the app (by following my account from another account). There is no error message at all. My app is not registering a new follower and so isn't sending the message. With no error message I am stuck at the moment.

I should note that I am comfortable using Heroku (so that side is fine) but I am a Javascript beginner. I'm hoping someone with more javascript experience (or knowledge of the Twitter API) will be able to shed some light on the matter.

I've included my code below.

main.js

console.log("bot starting");
console.log();

const Twit = require('twit');
const config = require('./config.js');

console.log(config);
console.log();

var T = new Twit(config);

const stream = T.stream('statuses/filter', { track: '@test' });

var GenerateMessage = name => {
    const days = [
        "Thanks for following!",
    ];
    return msg;
};

const SendMessage = user => {
    const { screen_name, name } = user.source;

    const obj = {
        screen_name,
        text: GenerateMessage(name)
    };
    if (screen_name != my_user_name) {
        console.log("New Follower");
        setTimeout(() => {
            T.post("direct_messages/new", obj)
                .catch(err => {
                    console.error("error", err.stack);
                })
                .then(result => {
                    console.log(`Message sent successfully To ${screen_name}`);
                });
        }, timeout);
    }
};

config.js

module.exports = {
    consumer_key: "***",
    consumer_secret: "***",
    access_token: "***",
    access_token_secret: "***",
}

Procfile

worker: node index.js

package.json

{
    "dependencies": {
        "npm": "^8.18.0",
        "twit": "^2.2.11"
    }
}

Thanks!

0 Answers
Related