Tracking Twitter mentions with Monday.com
Tags: javascript, tutorial, opensource
Published: 2020-11-06 19:07:35

Monitoring social media mentions is an essential part of any business. It gives brands an opportunity to track, analyze and respond to conversations about them on social media. In this quick tutorial, I will show you an example how you can a setup a simple Twitter mentions tracker with Monday.com.

In this article we will be using Reshuffle's [open source integration framework](https://dev.reshuffle.com/) to easily integrate Twitter and Monday services to meet your brands needs for social monitoring. 

##Let's build it
Normal setup and configuration could take a while in order to get these services to work together. Reshuffle is an open source, lightweight, and event-driven framework that helps you  build integrations that completes complex tasks. 

These integrations and workflows are created inside a Reshuffle App. The objects that let you interact with these services are called connectors.

The first thing we need to do is to declare a Reshuffle App and a [Twitter connector](https://github.com/reshufflehq/reshuffle-twitter-connector) that will search for any @mentions or #hashtag passed in.

```javascript
  const app = new Reshuffle();
  const twitter = new TwitterConnector(app, {
    customerKey: process.env.TWITTER_CUSTOMER_KEY,
    customerSecret: process.env.TWITTER_CUSTOMER_SECRET,
  });
```
To keep track of mentions, we will be pushing them onto a Monday board using the [Monday connector](https://github.com/reshufflehq/reshuffle-monday-connector).

```javascript
  const monday = new MondayConnector(app, {
    token: process.env.MONDAY_TOKEN,
    baseURL: "https://localhost:8000",
  });
```

Now that we have the connectors declared, we can start using them.

First thing is to make a new board from your Monday dashboard. Get the board ID from the url (eg. new-board-name.monday.com/board/2193445), this will be used to get the board data like column IDs, item names etc. Make a new board with columns that look similar to this. 

| Column        | Type         |
| ------------- | ------------ |
| tweet         | Long-text    |
| user          | Text         |
| created-at    | Date         |


![Monday Board](https://i.imgur.com/yp1Rw4s.jpg)

```javascript
(async () => {
    const boardItems = await monday.getBoardItems(BOARD_ID);

    for (let id in boardItems.items) {
      if (!tweetsCache[boardItems.items[id].name]) {
        tweetsCache[boardItems.items[id].name] = { fetched: true };
      }
    }
  })().catch(console.error);
```
As you can see above, we use the `BOARD_ID` to fetch any items that is currently on the board using the `getBoardItems` method. For each of the row names, we will be using the Twitter IDs, as you will see later on. This function will run before any Twitter connector actions to prevent duplicate tweets from being saved onto the board.

With this duplicate check in place, we can move on with the Twitter connector.

We need to define the event to listen to using the Twitter connector's `on()` method. In this case, we will listen for search events.

```javascript
  twitter.on({ search: "@reshuffleHQ" }, async (event, app) => {
    for (const tweet of event.tweets) {
      if (!tweetsCache[tweet.id]) {
        tweetsCache[tweet.id] = {
          user: tweet.user.screen_name,
          date: tweet.created_at,
          tweet: tweet.text,
        };
        addNewItem(tweet)
      }
    }
```
We will get an array of tweets from the event object. You can see in the `if` block, we run a check on the object from earlier, to prevent duplicate tweets from being added onto the Monday board. If the tweet does not exist in the object, we can continue to adding a new row with the `addNewItem` function we will create. 

```javascript
const createItems = async (tweetInfo) => {
    monday
      .getColumn(BOARD_ID)
      .then((res) => {
        return res.boards[0].columns.map(({ title }) => title);
      })
      .then(async (title) => {
        const testObj = {
          [title[1]]: () => tweetInfo.text,
          [title[2]]: () => tweetInfo.user.screen_name,
          [title[3]]: () =>
            new Date(Date.parse(tweetInfo.created_at))
              .toISOString()
              .split("T")[0],
        };

        const testQuery = await monday.createItem(
          BOARD_ID,
          JSON.stringify(tweetInfo.id),
          testObj
        );
      });
  };
```
In this function, we will be using multiple methods from the Monday connector. First we use the `getColumn`, to get the titles of each column, then we create an object using the column titles as the key. Finally, we can pass it through to the `createItem` method to generate a row that will look something like this.


![Monday Board](https://i.imgur.com/ZeYphML.jpg)

Last but not least, let's initiate the integration by starting the Reshuffle App

```javascript
app.start();
```
With minimal code, Twitter mentions are easily tracked with the Monday board. Now its your turn to get creative! Find new solutions to monitoring social mentions, maybe extend this functionality by tracking the frequency of daily mentions.