npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twitter-v2-simple

v1.3.1

Published

A simplified version of Twitters V2 api with straight forward functions and callbacks.

Downloads

18

Readme

Twitter V2 Simple

About The Project ℹ️

A simplified version of Twitter’s V1 & V2 API 😮‍💨 with straight forward functions and callbacks. If you need quick simple functions such as follow, like, DM and others, then this package is for you. This package only contains two dependencies: Twitter SDK and Twitter API V2🙏. "Twitter V2 Simple" is also a combination of V1 and V2 to bring a seamless integrated workflow. 🦾 Contributor [email protected] This package is still under development. 🛠️

Table Of Contents 📜

User 👤

  1. Who am I
  2. Get users details
  3. Get users replied to tweet
  4. Follow a user 🤝
  5. Unfollow a user
  6. Get followers of a user
  7. Get following of a user

Direct Message 📥

  1. Direct Message a user

Tweet 🐦

  1. Make a tweet
  2. Get a tweet
  3. Reply to a tweet
  4. Like a tweet
  5. Unlike a tweet
  6. Delete a tweet

Media 📸

  1. Make a media tweet image/video
  2. Make a Twitter poll

Getting Started

There are a few steps that need to take place before you can use the full functionality of Twitters V2 Api.

  1. Create a developer account for Twitter Twitter Developer
  2. After obtaining your developer account, you must get elevated access to Twitter V2 Api to complete all functions such as, follow, delete, like, and more. Twitter Developer - Elevated Access.

Installation

You will need to access your API keys, and Tokens to initiate the Twitter V2 client.

  1. Install NPM package
    npm install twitter-v2-simple
  2. Import Twitter-V2-Simple
    import TwitterV2Simple from 'twitter-v2-simple';
    
    //Or
    
    const TwitterV2Simple = require('twitter-v2-simple');
  3. Enter your Twitter API tokens and secrets. Also make sure to use .ENV to protect your keys.
    const twitter = new TwitterV2Simple({
       appKey: 'KdfeirYYhdf........',
       appSecret: 'TYRhdiIuh......',
       accessToken: '1435667.......',
       accessSecret: 'mxGHjgjj.....',
       bearerToken: 'AAAAAAAAA......'
    });

Usage

Twitter V2 Simple only focuses on the main core functions in the user and tweet category. For more advanced functions please visit the original Twitter SDK for further documentation. Twitter SDK

User 👤

Who Am I

Get the current logged in user for Twitter.

//Async Function
const me = await twitter.whoami()
console.log(me)

Returns an object with your id, name, and username.

{
   id: '158143493602009628672',
   name: 'Your Name',
   username: 'yourusername'
}

Get a users details

Get the details of a single user. Pass the Twitter username as an argument.

//Async Function
const user = await twitter.getUser('twitter')
console.log(user)

Returns an object with the users id, name, and username.

{ id: '783214', name: 'Twitter', username: 'Twitter' }

Get users replied to tweet.

You can get up to 100 users who replied to a tweet. Pass the conversation id (same as post id) as an argument along with a number between 10-100. If you do not pass an amount it will default to 10.

//Async Function
const allRepliedUsers = await twitter.getAllRepliedUsers('1580661436132757506', 10)
console.log(allRepliedUsers)  

Returns an array of up to 100 user objects who replied to the tweet. Along with the original tweet details.

users: [
         {
            id: '121497883484980166656',
            name: 'John Smith',
            username: 'thesmithman'
         },
         {
            id: '158071034563650621440',
            name: 'Kim B',
            username: 'bkim9033'
         },
         {
            id: '158168275334791299584',
            name: 'Alpha G ',
            username: 'g_aalfa_1'
         },
         // More users
tweets: [
         {
            text: 'a hit Tweet. 🤝🏽',
            author_id: '783214',
            id: '158249263045184706048',
            edit_history_tweet_ids: [Array]
         },
      ]

Follow a user

You can follow a user by passing the user id as an argument.

//Async Function
const follow = await twitter.follow('783214')
console.log(follow)

Returns a boolean following object with the value of true or false, along with pending status.

{ following: true, pending_follow: false }

Unfollow a user

You can also unfollow a user by passing the users id as an argument.

//Async Function
const unfollowed = await twitter.unfollow('783214')
console.log(unfollowed)

Returns a boolean following object with the value of false.

{ following: false }

Get followers of a user

You can get the followers from a user by providing the user id and the amount of users you want to return as an argument.

//Async Function
const followers = await twitter.getFollowers('783214', 100)
console.log(followers)

Returns an array with user objects and total result count.

{
  data: [
   {
      id: '158260446354554138113',
      name: 'Tina',
      username: 'tin452'
    ,
   {
      id: '1582604434533938063360',
      name: 'Melese',
      username: 'Melese85'
   },
   { id: '158234529484546396160', 
      name: 'James', 
      username: 'james5118' 
   }
   //More usernames
  ],
  meta: { result_count: 100, next_token: 'MISPFPRRGFTCV1GZZZ' }
}

Get following of user

You can get everyone a user is following by passing the user id and the amount of users as an argument.

//Async Function
const following = await twitter.getFollowing('783214', 5)
console.log(following)

Returns an array with user objects and total result count.

{
  data: [
   {
      id: '158260446354554138113',
      name: 'Bossman',
      username: 'boss_man-4000'
    ,
   {
      id: '1582604434533938063360',
      name: 'Michael Fin',
      username: 'fin-mich3456'
   },
   { id: '158234529484546396160', 
      name: 'ChrisTy', 
      username: 'christy4you' 
   }
   //More usernames
  ],
  meta: { result_count: 5, next_token: 'MISPFPRRGFTCV1GZZZ' }
}

Direct Message a user

You can send a Direct Message to a user by passing the user id and the message you want to send as an argument.

//Async
const sendDm = await twitter.dm('783214', 'Twitter API is the best!')
console.log(sendDm)

Returns an object with the Direct Message details.

   {
      type: 'message_create',
      id: '158261136734898521605',
      created_timestamp: '1666158928261',
         message_create: {
            target: { recipient_id: '783214' },
            sender_id: '15826342311348438521605',
            message_data: { text: 'Twitter API is the best!', entities: [Object] }
         }
   }

Tweet 🐦

Make a tweet

You can make a tweet by passing a message in as an argument.

//Async Function
const makeTweet = await twitter.tweet('You are the number one dev in the world!')
console.log(makeTweet)

Returns a large object with the status of the created tweet.

{
  created_at: 'Sat Oct 22 19:22:30 +0000 2022',
  id: 1583901622342344305700,
  id_str: '1583901627605405696',
  full_text: 'You are the number one dev in the world!',
  truncated: false,
  display_text_range: [ 0, 38 ],
  entities: { hashtags: [], symbols: [], user_mentions: [], urls: [] },
  // Alot more information 

Get a tweet.

Get the details of a given tweet.

//Async Function
const tweet = await twitter.getTweet('1580661436132757506')
console.log(tweet)

Returns tweet object.

{
  created_at: 'Thu Oct 13 20:47:08 +0000 2022',
  id: 158066143634534534534757500,
  id_str: '1580661543543534436132757506',
  full_text: 'a hit Tweet 🤝🏽 https://t.co/VGrsylXB8D https://t.co/2C7cah4KzW',
  truncated: false,
  display_text_range: [ 0, 35 ],
  entities: {
    hashtags: [],
    symbols: [],
    user_mentions: [],
    urls: [ [Object] ],
    media: [ [Object] ]
  },
  extended_entities: { media: [ [Object] ] }

Reply to a tweet

You can reply to a tweet by passing a message and the tweet id in as an argument.

//Async Function
const replyTweet = await twitter.reply("Let's work together!", '1580661436132757506')
console.log(replyTweet)

Returns a large object with the tweet reply status.

{
  created_at: 'Sat Oct 22 19:30:55 +0000 2022',
  id: 15839037345344486412000,
  id_str: '158395435436186412032',
  full_text: "@twitter Let's work together!",
  truncated: false,
  display_text_range: [ 13, 33 ],
  entities: { hashtags: [], symbols: [], user_mentions: [ [Object] ], urls: [] },
  // Alot more information 

Like a tweet

You can like a tweet by passing it's id as an argument. Tweet ids are located at the end of the twitter.com link. (Example: https://twitter.com/Twitter/status/1580661436132757506)

//Async Function
const liked = await twitter.like('1580661436132757506')
console.log(liked)

Returns a boolean liked object with the value of true.

{ liked: true }

Unlike a tweet

You can unlike a tweet by passing it's id as an argument. Tweet ids are located at the end of the twitter.com link. (Example: https://twitter.com/Twitter/status/1580661436132757506)

//Async Function
const unliked = await twitter.unlike('1580661436132757506')
console.log(unliked)

Returns a boolean liked object with the value of false.

{ liked: false }

Delete a tweet

You can delete a tweet that belongs to you by passing the tweet id as an argument.

//Async Function
const deleteTweet = await twitter.delete('1580661436132757506')
console.log(deleteTweet)

Returns a large object with the deleted tweet's status.

{
  created_at: 'Mon Oct 17 08:23:23 +0000 2022',
  id: 158192381666432424323400,
  id_str: '15819238123423939968',
  full_text: "@twitter Let's work together!",
  truncated: false,
  display_text_range: [ 0, 24 ],
  entities: { hashtags: [], symbols: [], user_mentions: [], urls: [] },
  // Alot more information 

Make a media tweet

You can make a video or image tweet by passing the message and path to the file as an argument.

//Async Function
const tweetImage = await twitter.tweetMedia('Check out my new image! 👀', '/home/images/your_image.png')
console.log(tweetImage)

Returns a large object with the media tweet details along with the media link.

{
  created_at: 'Sun Oct 23 01:53:52 +0000 2022',
  id: 158345434121582166000,
  id_str: '1584543521582166019',
  full_text: 'Check out my new image! 👀 https://t.co/Oj0sMb5gdf45r',
  truncated: false,
  display_text_range: [ 0, 4 ],
  entities: {
    hashtags: [],
    symbols: [],
    user_mentions: [],
    urls: [],
    media: [ [Object] ]
  },
  extended_entities: { media: [ [Object] ] },
  // Alot more information 

Make a Twitter poll

You can make a Twitter poll by simply passing a poll object containing a question, options (array), and days.

const twitterPoll = await twitter.poll({
    question: 'Do you like Twitter V2 Simple? 🤔',
    options: ['Yes', 'Kinda', 'No'],
    days: 7 //Between 1-7 days
})
console.log(twitterPoll)

Returns a small object with the poll text and id.

{
  data: {
    id: '15840382454354354354203265',
    text: 'Do you like Twitter V2 Simple? 🤔'
  }
}