Work with Users in Twit.js

InstructorHannah Davis

Share this video with your friends

Send Tweet

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.

Lois Tatis Tatis
~ 5 years ago

For anyone that stumbled into issues with this lesson, it looks like the twitter api has been updated.

To get this working with the bot.post function takes an updated url direct_messages/events/new and you have to create an event object and pass it in a few attributes.

docs here: https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event

example:

const event = {
   type: "message_create",
   message_create: {
     target: {
      recipient_id: "<the id of the user you want to dm>"
    },
     message_data: {
       text: "<your text here>"
     }
    },

 }
 bot.post('direct_messages/events/new', { event }, function(err, data, response) {
    if (err) {
      console.warn(err)
    } else {
      console.log(data);
    }
  })

It might be worth updating this video at some point!

p.s the example above uses the format directly from the twitter docs, I think you can get this to also work with the twit lib by using streams/events

Quizzy
~ 4 years ago

Thanks @Lois Tatis, that gave me the quick answer I needed. In addition to this, I just want to highlight, the recipient_id field won't work if you enter a screen_name.