⚠️ This lesson is retired and might contain outdated information.

Work with Users in Twit.js

Hannah Davis
InstructorHannah Davis
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 2 years ago

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.

[00:00] Now, let's look at how to interact with users.

[00:03] 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.

[00:40] 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.

[01:24] 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.

[01:37] 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.

[01:52] 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.

[02:28] 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.

[03:07] 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.

[03:22] 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.

[03:40] 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.

[04:06] 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.

[04:17] 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.

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