Trigger Microsoft Flow using Twitter webhook
Table of contents:
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!
Sreencast
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.
- The developer account can be created here: https://developer.twitter.com.
- App can be created here: https://developer.twitter.com/en/apps/create
- Developer environment can be created here: https://developer.twitter.com/en/account/environments – it has to be “Account Activity / API” environment.
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.:
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.
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-webhooks 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-started-with-webhooks 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:
- Define OAuth 1.0 connection details in Authorization tab.
- Remove all parameters from Params tab.
- 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 a P1 subscription for 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:
- Tweets (by user)
- Tweet deletes (by user)
- @mentions (of user)
- Replies (to or from user)
- Retweets (by user or of user)
- Quote Tweets (by user or of user)
- Retweets of Quoted Tweets (by user or of user)
- Likes (by user or of user)
- Follows (by user or of user)
- Unfollows (by user)
- Blocks (by user)
- Unblocks (by user)
- Mutes (by user)
- Unmutes (by user)
- Direct Messages sent (by user)
- Direct Messages received (by user)
- Typing indicators (to user)
- Read receipts (to user)
- 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 compliant 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. 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!
Eric Sauvé @ZePowerDiver
Being able to mention people (@) and do RT is great. Thank you Tomasz!
Tomasz Poszytek
You’re welcome! 🙂
Dennis Augustine
Hi Thomasz, for the life of me I can’t figure out why my function which follows your pattern isn’t posting the HTTP request to the Power Automate Flow. It fails silently.
Tomasz Poszytek
Well, it was some time ago since I published that post, possibly Twitter endpoints changed? Hard to say.