https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/Azure%20Function%20Twitter.png

Overview

This blog post details how to use a Powershell Azure Function App to get information from a RestAPI and send a social media update.
The data can come from anywhere, and in the case of this example I’m getting the data from WioLink IoT Sensors. This builds upon my previous post here that details using Powershell to get environmental information and put it in Power BI.  Essentially the difference in this post is outputting the manipulated data to social media (Twitter) whilst still using a TimerTrigger Powershell Azure Function App to perform the work and leverage the “serverless” Azure Functions model.

Prerequisites

The following are prerequisites for this solution;

Create a folder on your local machine for the Powershell Module then save the module to your local machine using the powershell command ‘Save-Module” as per below.
Save-Module -Name InvokeTwitterAPIs -Path c:\temp\twitter

Create a Function App Plan

If you don’t already have a Function App Plan create one by searching for Function App in the Azure Management Portal. Give it a Name, Select Consumption so you only pay for what you use, and select an appropriate location and Storage Account.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/CreateFunctionApp2.png

Create a Twitter App

Head over to http://dev.twitter.com and create a new Twitter App so you can interact with Twitter using their API. Give you Twitter App a name. Don’t worry about the URL too much or the need for the Callback URL. Select Create your Twitter Application.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/CreateTwitterApp.png
Select the Keys and Access Tokens tab and take a note of the API Key and the API Secret. Select the Create my access token button.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/TwitterAppSettings.png
Take a note of your Access Token and Access Token Secret. We’ll need these to interact with the Twitter API.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/AccessToken.png

Create a Timer Trigger Azure Function App

Create a new TimerTrigger Azure Powershell Function. For my app I’m changing from the default of a 5 min schedule to hourly on the top of the hour. I did this after I’d already created the Function App as shown below. To update the schedule I edited the Function.json file and changed the schedule to “schedule”: “0 0 * * * *”
Give your Function App a name and select Create.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/ScheduleFunction.png

Configure Azure Function App Application Settings

In your Azure Function App select “Configure app settings”. Create new App Settings for your Twitter Account, Twitter Account AccessToken, AccessTokenSecret, APIKey and APISecret using the values from when you created your Twitter App earlier.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/TwitterVariables.png

Deployment Credentials

If you haven’t already configured Deployment Credentials for your Azure Function Plan do that and take note of them so you can upload the Twitter Powershell module to your app in the next step.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/Deployment%20Credentials.png
Take note of your Deployment Username and FTP Hostname.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/AppDevSettings.png

Upload the Twitter Powershell Module to the Azure Function App

Create a sub-directory under your Function App named bin and upload the Twitter Powershell Module using a FTP Client. I’m using WinSCP.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/UploadPSModulesTwitter.png
From the Applications Settings option start Kudu.
Traverse the folder structure to get the path do the Twitter Powershell Module and note it.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/TwitterPSM.png
Update the code to replace the sample from the creation of the Trigger Azure Function as shown below to import the Twitter Powershell Module. Include the get-help lines for the module so we can see in the logs that the modules were imported and we can see the cmdlets they contain.

Validating our Function App Environment

Update the code to replace the sample from the creation of the Trigger Azure Function as shown below to import the Twitter Powershell Module. Include the get-help line for the module so we can see in the logs that the module was imported and we can see the cmdlets they contain. Select Save and Run.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/get-Help%20twitter.png
Below is my output. I can see the output from the Twitter Module.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/ModulesLoaded-Twitter.png

Function Application Script

Below is my sample script. It has no error handling etc so isn’t production ready, but gives a working example of getting data in from an API (in this case IoT sensors) and sends a tweet out to Twitter.

Viewing the Tweet

And here is the successful tweet.
https://dl.dropboxusercontent.com/u/76015/BlogImages/FunctionIOTWeather/Tweet.png

Summary

This shows how easy it is to utilise Powershell and Azure Function Apps to get data and transform it for use in other ways. In this example a social media platform. The input could easily be business data from an API and the output a corporate social platform such as Yammer.
Follow Darren on Twitter @darrenjrobinson

Category:
Azure Platform, Internet of Things, PowerShell, WebAPI
Tags:
, , , ,