How to get a notification when a tweet is deleted via the twitter API?

519 Views Asked by At

How can I get a notice when a tweet was deleted using the twitter API?

In version 1.0 of the API, I was able to get the notification in a stream using this:

var Twit = require("twit");

var T = new Twit({
    consumer_key: "555",
    consumer_secret: "555",
    access_token: "555",
    access_token_secret: "555",
    timeout_ms: 60 * 1000,
    strictSSL: true
});

var userIds = [ "123", "456" ];

var stream = T.stream("statuses/filter", { follow: userIds.join(",") });

stream.on("delete", (x) => console.log("Tweet was deleted", x));

However, without notice. The deleted events stopped being streamed.

So now I'm trying to do it with v2 of the twitter API like this:

const BEARER_TOKEN = "555";

const { ETwitterStreamEvent, TweetStream, TwitterApi, ETwitterApiError } = require("twitter-api-v2");

const appClient = new TwitterApi(BEARER_TOKEN);

const stream = await appClient.v2.getStream("tweets/compliance/stream", { partition: 1 });

stream.on(ETwitterStreamEvent.Data, (x) => console.log("Got data", x));

The call to getStream() throws the following error:

data: {
    client_id: '555',
    detail: 'When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.',
    registration_url: 'https://developer.twitter.com/en/docs/projects/overview',
    title: 'Client Forbidden',
    required_enrollment: 'Standard Basic',
    reason: 'client-not-enrolled',
    type: 'https://api.twitter.com/2/problems/client-forbidden'
}

I also tried using an app only login such as this:

const TWITTER_CLIENT = new TwitterApi({
    appKey: CONSUMER_KEY,
    appSecret: CONSUMER_SECRET,
    accessToken: ACCESS_TOKEN,
    accessSecret: ACCESS_TOKEN_SECRET
});

var appClient = await TWITTER_CLIENT.appLogin();

That throws the same error as above.

Using 2.0's getStream() with /tweets/search/stream/ does return an event a tweet is created, but not when it is deleted. It also has a limited query with only 5 rules per stream and rules are only 512 characters in length. Which won't cover all the screen names I currently track in the 1.0 version of the API.

I also tried using compliance jobs, but it takes a very long time and ends up returning an empty array anyways instead of any info about the tweet ids I provided:

var job = await appClient.v2.sendComplianceJob({
    type: "tweets",
    ids:[
// the ids are not from my dev account or from 
// a account that authed my app
        "555", // id of tweet I deleted
        "123", // id of tweet I deleted
        "456", // id of tweet I didn't delete
    ]
});

// takes 5-10 minutes to say its complete
var jobResults = await appClient.v2.complianceJobResult(job.data);

// echos: jobResults: []
console.log("jobResults", jobResults);

How can I get a stream event of when a tweet is deleted (of any specific user I choose) using the v2 API of twitter?

1

There are 1 best solutions below

3
On BEST ANSWER

Unfortunately, Twitter have deprecated User deletes a Tweet event type.

The Only other option is to save all tweets for the accounts you are tracking on your database then compare them to current tweets using lookup API.

but you can only check 100 tweets on every request, so you will need to make a job that loops to check every 100 tweet, then to inform you if a tweet was deleted.

ids: A comma separated list of Tweet IDs. Up to 100 are allowed in a single request. Make sure to not include a space between commas and fields.

source: https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets#tab0