← Twitter Developer App + Node integrations

Run Node Code with Node API on New Message from Twitter Developer App API

Pipedream makes it easy to connect APIs for Node, Twitter Developer App and 1000+ other apps remarkably fast.

Trigger workflow on
New Message from the Twitter Developer App API
Next, do this
Run Node Code with the Node API
No credit card required
Into to Pipedream
Watch us build a workflow
Watch us build a workflow
7 min
Watch now ➜

Trusted by 500,000+ developers from startups to Fortune 500 companies

Adyen logo
Brex logo
Carta logo
Checkr logo
Chameleon logo
DevRev logo
LinkedIn logo
Netflix logo
New Relic logo
OnDeck logo
Replicated logo
Scale AI logo
Teamwork logo
Warner Bros. logo
Xendit logo

Developers Pipedream

Getting Started

This integration creates a workflow with a Twitter Developer App trigger and Node action. When you configure and deploy the workflow, it will run on Pipedream's servers 24x7 for free.

  1. Select this integration
  2. Configure the New Message trigger
    1. Connect your Twitter Developer App account
    2. Configure timer
  3. Configure the Run Node Code action
    1. Connect your Node account
  4. Deploy the workflow
  5. Send a test event to validate your setup
  6. Turn on the trigger

Details

This integration uses pre-built, source-available components from Pipedream's GitHub repo. These components are developed by Pipedream and the community, and verified and maintained by Pipedream.

To contribute an update to an existing component or create a new component, create a PR on GitHub. If you're new to Pipedream component development, you can start with quickstarts for trigger span and action development, and then review the component API reference.

Trigger

Description:Emit an event each time the authenticated user receives a Direct Message
Version:0.0.1
Key:twitter-new-message

Trigger Code

const common = require("../common");

module.exports = {
  ...common,
  key: "twitter-new-message",
  name: "New Message",
  description: "Emit an event each time the authenticated user receives a Direct Message",
  version: "0.0.1",
  dedupe: "unique",
  hooks: {
    async deploy() {
      const accountId = await this.twitter_developer_app.getAccountId();
      this._setAccountId(accountId);
    },
  },
  methods: {
    _getAccountId() {
      return this.db.get("accountId");
    },
    _setAccountId(accountId) {
      this.db.set("accountId", accountId);
    },
    _getNextCursor() {
      return this.db.get("nextCursor");
    },
    _setNextCursor(nextCursor) {
      this.db.set("nextCursor", nextCursor);
    },
    _isRelevant(directMessage) {
      return (
        directMessage.type === "message_create" &&
        directMessage.message_create &&
        directMessage.message_create.sender_id != this._getAccountId()
      );
    },
    _processEvent(directMessage) {
      const meta = this.generateMeta(directMessage);
      this.$emit(directMessage, meta);
    },
    generateMeta(data) {
      const {
        id,
        created_timestamp: ts,
        message_create: { message_data: { text = "" } },
      } = data;
      const maxTextLength = 16;
      const trimmedText = text.length > maxTextLength
        ? `${text.slice(0, maxTextLength)}...`
        : text;
      return {
        id,
        summary: trimmedText,
        ts,
      };
    },
  },
  async run() {
    const lastMessageId = this._getNextCursor();
    const directMessages = await this.twitter_developer_app.getNewDirectMessages({
      lastMessageId,
    });

    const receivedDirectMessages = directMessages.filter(this._isRelevant.bind(this));
    if (receivedDirectMessages.length === 0) {
      console.log("No new direct messages detected. Skipping...");
      return;
    }

    receivedDirectMessages
      .reverse()
      .forEach(this._processEvent.bind(this));

    const { id: maxMessageId } = receivedDirectMessages.pop();
    this._setNextCursor(maxMessageId);
  },
};

Trigger Configuration

This component may be configured based on the props defined in the component code. Pipedream automatically prompts for input values in the UI and CLI.
LabelPropTypeDescription
Twitter Developer Apptwitter_developer_appappThis component uses the Twitter Developer App app.
N/Adb$.service.dbThis component uses $.service.db to maintain state between executions.
timer$.interface.timer

Trigger Authentication

Twitter Developer App uses API keys for authentication. When you connect your Twitter Developer App account, Pipedream securely stores the keys so you can easily authenticate to Twitter Developer App APIs in both code and no-code steps.

Video Tutorial on how to create and connect a Twitter Developer App to Pipedream

First, you'll need to sign up for a Twitter developer account and create a new application in the Twitter app dashboard.

  1. Register a new app on https://developers.twitter.com
  2. Apply for a new Twitter app, be detailed about your use
  3. After approval, create a new Standalone App
  4. Copy the api_key and api_secret_key into a Twitter Developer App Account on Pipedream
  5. Generate a new set of keys for your specific Twitter account
  6. Paste the access_token and access_token_secret in the Twitter Developer App Account on Pipedream

Once you've created an app, visit the Keys and tokens section of the app's settings, and add the API key, API secret key, Access token, and Access token secret below.

About Twitter Developer App

Use a Twitter developer app you've created to send API requests

Action

Description:Write custom Node.js code and use any of the 400k+ npm packages available. Refer to the [Pipedream Node docs](https://pipedream.com/docs/code/nodejs) to learn more.
Version:0.0.2
Key:node-run-node-code

Action Code

import node from "../../node.app.mjs";

export default {
  name: "Run Node Code",
  version: "0.0.2",
  key: "node-run-node-code",
  description: "Write custom Node.js code and use any of the 400k+ npm packages available. Refer to the [Pipedream Node docs](https://pipedream.com/docs/code/nodejs) to learn more.",
  props: {
    node,
  },
  type: "action",
  methods: {},
  async run({
    steps, $,
  }) {
    // Return data to use it in future steps
    return steps.trigger.event;
  },
};

Action Configuration

This component may be configured based on the props defined in the component code. Pipedream automatically prompts for input values in the UI.

LabelPropTypeDescription
NodenodeappThis component uses the Node app.

Action Authentication

The Node API does not require authentication.

About Node

Anything you can do with Node.js, you can do in a Pipedream workflow. This includes using most of npm's 400,000+ packages.

More Ways to Connect Node + Twitter Developer App

Run Node Code with Node API on New Tweet Metrics from Twitter Developer App API
Twitter Developer App + Node
 
Try it
Run Node Code with Node API on New Tweet Metrics from Twitter Developer App API
Twitter Developer App + Node
 
Try it
Run Node Code with Node API on New Message from Twitter Developer App API
Twitter Developer App + Node
 
Try it
New Tweet Metrics from the Twitter Developer App API
 
Try it
New Message from the Twitter Developer App API

Emit an event each time the authenticated user receives a Direct Message

 
Try it
New Message from the Twitter Developer App API

Emit new event each time the authenticated user receives a Direct Message

 
Try it
New Tweet Metrics from the Twitter Developer App API

Emit new event on each new twitter metric

 
Try it
Send Direct Message (DM) with the Twitter Developer App API

Send a DM to a user.

 
Try it
Run Node Code with the Node API

Write custom Node.js code and use any of the 400k+ npm packages available. Refer to the Pipedream Node docs to learn more.

 
Try it