Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Work with Users in Twit.js


    We’ll learn how your bot can get its list of followers, follow people, and look up friendships. We'll use Twit's GET method to get our followers at the followers/list endpoint, to get the users we follow at the friends/ids and friends/list endpoints, and to look up our friendships with the friendships/lookup endpoint. We'll also use Twit's POST method to follow someone at the friendships/create endpoint, and to send messages by posting to the direct_messages/new endpoint.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Now, let's look at how to interact with users.

    The first thing we can do is look at our followers. The way we do that is bot.get. There's two options here. The first is followers/IDs, pass it the bot screen name and our call back.

    This gives us a list of our followers' IDs. If we want a little more information, we can say bot.getfollowers/list. That will give us a list of the users that are following us. If we just wanted their screen names, we could say data.users.foreachuser, then, print out the user screen name.

    We can see that we are being followed by two people, @moviesummarybot, which is another bot we'll make in this course and @ahandvanish, which is me. Let's see how to follow people back.

    Twitter has a lot of rules around following, specifically around bulk following and automating following. You should absolutely familiarize yourself with the rules linked to in the reference section of this course. Generally, the rule is that a bot should only follow users that follow that bot.

    To follow someone, the syntax for that is bot.postfriendships/create. Pass it the screen name of the person you want to follow and your call back. If we run this, we can see that that worked. We can also see that worked by looking up the people we follow.

    To do that, we can say bot.getfriends/IDs, pass it our screen name. This is the same as the followers/IDs list, where we see the IDs of the people that we're following. If we change this to list, now, we can see the actual users who are following.

    This has a lot of information in it. The name, screen name, location, description, everything from whether it's protected to the follower count, the friend count, the language, etc.

    Here's another interesting thing we can do with our followers or people we're following. We can say bot.getfriendships/lookup. This will give us the relation to a specific user.

    If we pass it, the screen name of the friendship we want to look up, this gives us a connections field, which shows that our bot is both following and being followed by @ahandvanish.

    This is a useful tool to follow back new followers. You can run through your followers list and follow back any user that doesn't have the following connection.

    Last thing, we can direct message by saying bot.postdirectmessages/new. Again, you want to follow the Twitter rules and never DM someone that isn't following you. Pass it screen name. You also need the text.

    That looks like it worked. If I go to my personal Twitter account, I can see that the message was sent from my bot.