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
  • Working daily with Microsoft Cloud to deliver the needs of my company, my customers and various Microsoft communities and forums. | Office 365 | Flow | PowerShell | PowerApps | SharePoint |
  • Co-founder of https://plumsail.com, Office 365 and SharePoint expert. Passionate about design and development of easy to use, convenient and flexible products.
  • Microsoft Business Apps MVP. Owner of ThriveFast, an Office 365 consulting company.
  • 7x Microsoft Business Solutions MVP (CRM)
  • I'm keen in MS technologies, SharePoint, Office 365 and development for them
  • Daniel is a Business Productivity Consultant & Microsoft Business Solutions MVP who is very enthusiastic about all things Office 365, Microsoft Flow, PowerApps, Azure & SharePoint (Online). Since the preview, Daniel has been working with Microsoft Flow and later on with Microsoft PowerApps. That led to him being awarded an MVP Award for Business Solutions. He loves to blog, present and evangelize about improving productivity in the modern workspace with these amazing tools!
  • Michelle is an Office 365 solution architect in Twin Cities, MN. She has been delivering business collaboration solutions for years with her focus on SharePoint and Office 365. Michelle is a recent board member of the Minnesota Office 365 User Group and has been a member of the SharePoint community since 2009. She is a frequent speaker at MNSPUG and SharePoint Saturday and co-chaired the Legal SharePoint User Group for 4 years. Her most frequent projects have involved rolling out a large deployment of Office 365, SharePoint Online intranet, build of a "CHAMPS" Office 365 user adoption program and most recently, SharePoint On-Premise to Online Migration. Michelle is very excited about cloud technology as it is shifting her IT Pro focus to collaboration strategy and technical adoption.
  • I'm a Microsoft Office Servers and Services MVP with a special interest in SharePoint, Office 365, Microsoft Flow, Microsoft Teams and PowerApps. I work at Triad Group Plc ( https://triad.co.uk)
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor