Tweets filter through keyword via Node.js Code

Hello,

I am trying to create a workflow in which tweets are pulled through twitter, then filtered through keyword via Node.js code and if the tweet contains one of the keywords, send to next process. Can someone help me or guide me on this.

Thanks

@rzey Let me know if this works for you:

  1. Visit https://pipedream.com/new to create a new workflow
  2. In the trigger step, choose the Twitter app and the Search Mentions trigger. You’ll want to enter your username, with the @ sign, as the Search Term . This will trigger the workflow each time you’re mentioned.
  3. Add a new code step to your workflow.
  4. if you’d like to exit early when your tweet matches specific words, try something like:
// If the tweet text does NOT contain the word "test", exit
if (!steps.trigger.event.full_text.match(/test/g)) {
  $end("not a match")
}
  1. After this step, you can run whatever logic you’d like on tweets that pass this filter.

Take a look at this workflow for an example. Let me know if that helps!

Thanks,
Dylan

1 Like

Dear Sir,

Thanks for your response and code. I would be grateful if you can advise how to add multiple keywords (above you have used one keyword “test”).

Kind Regards,
Raza

One technique I like to use is simply to modify the regular expression to include the | operator, which means “OR”. So if you run something like:

if (!steps.trigger.event.full_text.match(/test|hello|world/g)) {
  $end("not a match")
}

It will match if the tweet contains the word “test” OR “hello” OR “world”.

2 Likes

much appreciated thanks

Dear Sir,

What shall i use if i want to ignore tweets if they contain a specific keyword

Thanks and Kind Regards,
Raza

Before, you ran this code to only process tweets that matched specific keywords:

if (!steps.trigger.event.full_text.match(/test|hello|world/g)) {
  $end("not a match")
}

If you’d like to ignore specific keywords, just remove the ! at the front:

if (steps.trigger.event.full_text.match(/keywords|to|ignore/g)) {
  $end("Tweet matched keyword on ignore list")
}
2 Likes

Dear Sir,

Highly appreciated.

Thanks