Introducing PSTwitterAPI - a PowerShell module for the Twitter API

1 minute read

Introducing PSTwitterAPI

<salespitch>Ever wanted to get twitter notifications on your build jobs? Or collect some tweets for a report? Or automate some tweets for your blog post? All in PowerShell? This is the module for you! </salespitch>

Seriously, for whatever the reasons you need to interact with the Twitter’s API, this is the most complete solution available out there!

Twitter API Helper functions

I originally created the module InvokeTwitterAPIs that is sitting in the PowerShell Gallery, but a lot has changed since then. I wanted to refactor the whole project and make it easier/faster overall. It wasn’t complete, and many endpoints where missing.

Instead of writing each endpoint by hand, I created a Script to scrape the Twitter API documentation, get the resource URL, description and parameters. It then uses this to generate all the helper functions dynamically. You can go check it out here: GitHub: APIHelper

It’s not pretty, but it does the job!

Example 1: Get Twitter user information

Import-Module PSTwitterAPI

# Provide Authentication for the Twitter API
# https://twittercommunity.com/t/how-to-get-my-api-key/7033
Set-TwitterOAuthSettings -ApiKey $env:ApiKey -ApiSecret $env:ApiSecret -AccessToken $env:AccessToken -AccessTokenSecret $env:AccessTokenSecret

# Get user twitter profile
Get-TwitterUsers_Lookup -screen_name 'mkellerman'
  1. Provide authentication token to the module
  2. Use one of the +120 helper functions to get/send requests to Twitter
  3. Profit

Example 2: Create WordCloud from your recent tweets

Import-Module PSTwitterAPI
Import-Module PSWordCloud

Set-TwitterOAuthSettings -ApiKey $env:ApiKey -ApiSecret $env:ApiSecret -AccessToken $env:AccessToken -AccessTokenSecret $env:AccessTokenSecret

# Request the last 200 tweets from the desired user.
$TwitterStatuses = Get-TwitterStatuses_UserTimeline -screen_name 'mkellerman' -count 200

# Get list of stop words that should be excluded from the world cloud.
$ExcludeWord = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/Alir3z4/stop-words/master/english.txt' -UseBasicParsing).Content -Split "`n"

# Generate a word cloud from the text in your tweets.
New-WordCloud -InputObject $TwitterStatuses.text -Path ".\TwitterStatuses.png" -ExcludeWord $ExcludeWord -ImageSize 720p

  1. Provide authentication token to the module
  2. Get last 200 tweets from your user
  3. Get stop words to be excluded from the world cloud
  4. Generate word cloud
  5. Profit
PSWordCloud for @mkellerman using PSTwitterAPI
PSWordCloud for @mkellerman using PSTwitterAPI

Open Source: Come and help!

I’ve built the foundation, but there are many things left to do:

  • Test each helper function (Manual/Pester).
  • If you see a missing API endpoint, create an Issue.
  • Any improvements you can suggest/make please make a PR!

You are using Twitter via Powershell? Join me on the PowerShell Slack/Discord #PSTwitterAPI channel!

And as always, go check it out and let me know what you think!