cancel
Showing results for 
Search instead for 
Did you mean: 

Trigger Microsoft Flow Using Twitter Web Hook

The only trigger built-in in Microsoft Flow for Twitter is “When a new tweet is posted”. However, the list of events available in Twitter API account activity documentation is much wider, built of 19 options. But how to get to them?

I was also inspired by a use case, that is allowing to post a thank you tweet once user is followed by another user. That solution was however using Zapier and Buffer online solutions. I wondered if that would be possible using only tools from Office 365/ Azure environment. And the answer was… yes!

Twitter API

The main component of the whole solution is obviously the Twitter API. To make it possible, you need first to have a developer account, an app created and a developer environment created. Both can be used for free.

Remember! The name of the app you use will be displayed along with the activity made by it (you can always change it), e.g.:

 

Displayed name of the app below a post created by it
Azure Function

I decided to use Node.
js since I do know a little about programming in JavaScript 🙂 The function I created is being trigger via HTTP Request, it listens both to GET and POST requests. I also set it Authorization Level to Function.
Configuration of Azure Function - Integrate option

What is crucial, is that the function has to respond differently whether it receives GET request (it is the CRC request to authenticate the endpoint for the webhook) and POST (used by Account Activity API calls).

Handling requests

That is a crucial part to let your Azure Function be registered as the app for the webhook. Twitter is sending every 24 hours a Challenge-Response Check (CRC) request to your app in order to verify its ownership. According to the documentation: https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/guides/securing-... for each such call your app must be capable of getting the crc_token parameter and then to send a response_token built from that crc_token and consumer_secret_key hashed with HMAC SHA-256 and encoded to base64 string. Quite a challenge for Flow. But not Azure Function.

Having all that in mind, the Function I built contains a if-else block, that checks kind of a request and sent parameters:

const crypto = require('crypto');
const https = require('https');
if (req.method === 'GET' && req.query.crc_token) { contents.log('CRC_Token: ' + req.query.crc_token); const responseToken = crypto.createHmac('sha256', process.env["Consumer_API_Key_Secret"]). update(req.query.crc_token).digest('base64'); contents.res = { status: 200, headers: { "Content-Type": "application/json" }, body: { "response_token": "sha256=" + responseToken + "" } }; }
else if (req.method === 'POST' && req.headers['content-type'] === 'application/json' && req.body) { // handling new account activity calls }

If the request is GET, it reads the crc_token value, then takes Consumer_API_Secret_Key from environmental variables, and using crypto library generates response_token that is then returning.

If the request is POST, it parses the incoming body contents and is triggering my Microsoft Flow, what you can read later.

Account Activity Webhook

To create a webhook you need to do the following steps. I used Postman to achieve them.

According to the documentation: https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/guides/getting-s... you have to do it as following:

1. Register app for a webhook by sending a POST call to https://api.twitter.com/1.1/account_activity/all/{YOUR ENVIRONMENT NAME}/webhooks?url={YOUR APP URL}, secured with OAuth1.0.

I did that with Postman:

Register Twitter Account Activity webhook with Azure Function app

  1. Define OAuth 1.0 connection details in Authorization tab.
  2. Remove all parameters from Params tab.
  3. Move them to Body tab, under x-www-form-urlencoded option, as written here: https://github.com/twitterdev/postman-twitter-ads-api/issues/2.

Important! I was trying multiple times to register app for the webhook, and constantly was receiving various “214” errors which were simply different “unathorized” reasons. Finally I re-generated all the keys again and that worked out.

2. Add (subscribe) account for which activities are going to be pushed to webhook. I also did that with Postman. You have to make a POST request to the URL: https://api.twitter.com/1.1/account_activity/all/{YOUR ENVIRONMENT NAME}/subscriptions.json and secure it with OAuth 1.0 authorization header.

Final words

Remember, that this solution requires aP1 subscriptionfor Microsoft Flow, so that you can use all HTTP – related actions.

Having this ways configured trigger for Microsoft Flow will allow you to listen and act for multiple events, such as:

  1. Tweets (by user)
  2. Tweet deletes (by user)
  3. @mentions (of user)
  4. Replies (to or from user)
  5. Retweets (by user or of user)
  6. Quote Tweets (by user or of user)
  7. Retweets of Quoted Tweets (by user or of user)
  8. Likes (by user or of user)
  9. Follows (by user or of user)
  10. Unfollows (by user)
  11. Blocks (by user)
  12. Unblocks (by user)
  13. Mutes (by user)
  14. Unmutes (by user)
  15. Direct Messages sent (by user)
  16. Direct Messages received (by user)
  17. Typing indicators (to user)
  18. Read receipts (to user)
  19. Subscription revokes (by user)

Moreover, having an Azure Function already set up and Flow to handle requests, you can then extend your solution as I did. I have written a second function, that is being called by Flow, to post back a tweet with a thank you message to every new Follower I get.

Important! Be sure that your solution is complaint with best practices and automation rules as described here:
https://help.twitter.com/en/rules-and-policies/twitter-rules-and-best-practices
https://help.twitter.com/en/rules-and-policies/twitter-automation

I hope you like it and find useful. Please check out the video that I have made to describe this scenario!

If you’d like me to help you in implementing this in your solution or to learn more, reach me out via contact form or leave me a comment. Thanks!

Comments

Thanks! I was right there at this point, facing the issue with RT and reference not possible using the standard connector.

 

@TomaszPoszytek 

FYI, here's a blog post where I refer back to you as a tip.

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor
  • I am building business processes and applications that are easy for users' to stick to, so they can follow and understand them. In overall I transform processes to be more reliable and effortless. I am a proud co-organizer of SharePoint Saturday Warsaw and active community member, blogger and international speaker.