Adding a Twitter Reply via Twitter API

blogentry, javascript, programming, selfnote


title: Adding a Twitter Reply via Twitter API date: "2019-07-28" banner: ./images/featured-image.gif published_at: "2019-07-28T22:45:07.000Z" tags: "blogentry, javascript, programming, selfnote" author: Sung M. Kim

A quick note to self regarding adding a reply on Twitter using Twitter NPM library.

POST statuses/update

POST statuses/update lets one "tweet" a status on Twitter.

But when you want to add a reply the an existing twitter, you can set the in_reply_to_status_id to the tweet ID you want to reply to.

The documentation for [in_reply_to_status_id](https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update#parameters) states that

The ID of an existing status that the update is in reply to. Note: This parameter will be ignored unless the author of the Tweet this parameter references is mentioned within the status text. Therefore, you must include @username , where username is the author of the referenced Tweet, within the update.

Even though it says that it should be "mentioned within the status text", but it doesn't sound 100% correct as passing username mentioned in the following sentence was enough to trigger the reply thread.

const Twitter = require('twitter')
const config = {
consumer_key: 'secret',
consumer_secret: 'secret',
access_token_key: 'secret',
access_token_secret: 'secret',
const client = new Twitter(config)
// https://github.com/desmondmorris/node-twitter#promises
const update = (status, in_reply_to_status_id = null) =>
client.post('statuses/update', {
username: '@dance2die',
update('Testing twitter NPM library')
.then(tweet => {
console.log(`tweet #1 ==>`, tweet)
return update('Reply #1', tweet.id_str)
.catch(error => console.log(`error ==>`, error))
view raw twitter.js hosted with ❤ by GitHub

On line #17, username is passed and that's sufficient to make in_reply_to_status_id not to be ignored by Twitter.