Class TweetService

Handles interacting with resources related to tweets.

Hierarchy

Constructors

Properties

authProxyUrl?: URL

The URL to the proxy server to use only for authentication.

userId?: string

The id of the authenticated user (if any).

Methods

  • Get the details of a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    Returns Promise<undefined | Tweet>

    The details of the tweet with the given id. If no tweet matches the given id, returns undefined.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Fetching the details of the tweet with the id '1234567890'
    rettiwt.tweet.details('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Like a tweet.

    Parameters

    • id: string

      The id of the tweet to be liked.

    Returns Promise<boolean>

    Whether liking was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Liking the Tweet with id '1234567890'
    rettiwt.tweet.like('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Get the list of tweets from a tweet list.

    Parameters

    • id: string

      The id of target list.

    • Optional count: number

      The number of tweets to fetch, must be <= 100.

    • Optional cursor: string

      The cursor to the batch of tweets to fetch.

    Returns Promise<CursoredData<Tweet>>

    The list tweets in the given list.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Fetching the most recent 100 tweets of the Twitter list with id '1234567890'
    rettiwt.tweet.list('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Remarks

    Due a bug in Twitter API, the count is ignored when no cursor is provided and defaults to 100.

  • Post a tweet.

    Parameters

    • options: TweetArgs

      The options describing the tweet to be posted. Check TweetArgs for available options.

    Returns Promise<undefined | string>

    The id of the posted tweet.

    Example

    Posting a simple text

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Posting a tweet to twitter
    rettiwt.tweet.post({ text: 'Hello World!' })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Example

    Posting a tweet with an image that has been already uploaded

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Posting a tweet, containing an image called 'mountains.jpg', to twitter
    rettiwt.tweet.post({ text: 'What a nice view!', media: [{ id: '1234567890' }] })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Example

    Posting a reply to a tweet

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Posting a simple text reply, to a tweet with id "1234567890"
    rettiwt.tweet.post({ text: 'Hello!', replyTo: "1234567890" })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Example

    Posting a tweet that quotes another tweet

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Posting a simple text tweet, quoting a tweet with id "1234567890"
    rettiwt.tweet.post({ text: 'Hello!', quote: "1234567890" })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Makes an HTTP request according to the given parameters.

    Type Parameters

    • T

      The type of the returned response data.

    Parameters

    Returns Promise<T>

    The raw data response received.

    Example

    Fetching the raw details of a user with username 'user1'

    import { FetcherService, EResourceType } from 'rettiwt-api';

    // Creating a new FetcherService instance using the given 'API_KEY'
    const fetcher = new FetcherService({ apiKey: API_KEY });

    // Fetching the details of the User with username 'user1'
    fetcher.request(EResourceType.USER_DETAILS_BY_USERNAME, { id: 'user1' })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    })
  • Retweet a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    Returns Promise<boolean>

    Whether retweeting was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Retweeting the Tweet with id '1234567890'
    rettiwt.tweet.retweet('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Get the list of users who retweeted a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    • Optional count: number

      The number of retweeters to fetch, must be <= 100.

    • Optional cursor: string

      The cursor to the batch of retweeters to fetch.

    Returns Promise<CursoredData<User>>

    The list of users who retweeted the given tweet.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Fetching the most recent 100 retweeters of the Tweet with id '1234567890'
    rettiwt.tweet.retweeters('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Schedule a tweet.

    Parameters

    • options: TweetArgs

      The options describing the tweet to be posted. Check TweetArgs for available options.

    Returns Promise<undefined | string>

    The id of the schedule.

    Example

    Scheduling a simple text

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Scheduling a tweet to posted at 19th of August, 2024, at 11:59:00 AM, in local time
    rettiwt.tweet.schedule({ text: 'Hello World!', scheduleFor: new Date('2024-08-19 23:59:00') })
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Remarks

    Scheduling a tweet is similar to posting, except that an extra parameter called scheduleFor is used.

  • Search for tweets using a filter.

    Parameters

    • filter: TweetFilter

      The filter to be used for searching the tweets.

    • Optional count: number

      The number of tweets to fetch, must be <= 20.

    • Optional cursor: string

      The cursor to the batch of tweets to fetch.

    Returns Promise<CursoredData<Tweet>>

    The list of tweets that match the given filter.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Fetching the most recent 5 tweets from user 'user1'
    rettiwt.tweet.search({ fromUsers: ['user1'] }, 5)
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Remarks

    For details about available filters, refer to TweetFilter

  • Stream tweets in pseudo real-time using a filter.

    Parameters

    • filter: TweetFilter

      The filter to be used for searching the tweets.

    • pollingInterval: number = 60000

      The interval in milliseconds to poll for new tweets. Default interval is 60000 ms.

    Returns AsyncGenerator<Tweet, any, unknown>

    An async generator that yields matching tweets as they are found.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Creating a function that streams all new tweets from the user 'user1'
    async function streamTweets() {
    try {
    // Awaiting for the tweets returned by the AsyncGenerator returned by the method
    for await (const tweet of rettiwt.tweet.stream({ fromUsers: ['user1'] }, 1000)) {
    console.log(tweet.fullText);
    }
    }
    catch (err) {
    console.log(err);
    }
    }

    // Calling the function
    streamTweets();
  • Unlike a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    Returns Promise<boolean>

    Whether unliking was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Unliking the Tweet with id '1234567890'
    rettiwt.tweet.unlike('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Unpost a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    Returns Promise<boolean>

    Whether unposting was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Unposting the Tweet with id '1234567890'
    rettiwt.tweet.unpost('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Unretweet a tweet.

    Parameters

    • id: string

      The id of the target tweet.

    Returns Promise<boolean>

    Whether unretweeting was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Unretweeting the Tweet with id '1234567890'
    rettiwt.tweet.unretweet('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Unschedule a tweet.

    Parameters

    • id: string

      The id of the scheduled tweet.

    Returns Promise<boolean>

    Whether unscheduling was successful or not.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Unscheduling the Tweet with id '1234567890'
    rettiwt.tweet.unschedule('1234567890')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });
  • Upload a media file to Twitter.

    Parameters

    • media: string | ArrayBuffer

      The path or ArrayBuffer to the media file to upload.

    Returns Promise<string>

    The id of the uploaded media.

    Example

    import { Rettiwt } from 'rettiwt-api';

    // Creating a new Rettiwt instance using the given 'API_KEY'
    const rettiwt = new Rettiwt({ apiKey: API_KEY });

    // Uploading a file called mountains.jpg
    rettiwt.tweet.upload('mountains.jpg')
    .then(res => {
    console.log(res);
    })
    .catch(err => {
    console.log(err);
    });

    Remarks

    • The uploaded media exists for 24 hrs within which it can be included in a tweet to be posted. If not posted in a tweet within this period, the uploaded media is removed.
    • Instead of a path to the media, an ArrayBuffer containing the media can also be uploaded.

Generated using TypeDoc