The code detailed in this post won’t work anymore. If you’re looking for updated PowerShell to use with Telstra’s APIs, please check out this updated post. 

Recently, Telstra released their first public API, which in true telco fashion leverages an existing product in their stable; SMS. The service allows anyone with a Telstra t.dev account (get one here) to get an API key which will allow you to send up to 100 messages per day, 1000 per month to Australian mobiles. Obviously, this is going to be great for anyone wishing to use a free SMS service for labbing, testing, or sending your buddies anonymous cat facts.

I’m not so much a dev, so the first thing I wanted to do was to test this out using PowerShell. Using PowerShell, I get to look like I’m doing something super-important whilst I send my buddies cat facts. The following is the code I used to make this happen.

First, we want to get ourselves an access token, so we can auth to the service.

$app_key = "Th1SiSn0TreAllYmYAppK3ybUtTHanKsAnyW4y"
$app_secret = "n0rmYS3cr3t"
$auth_string = "https://api.telstra.com/v1/oauth/token?client_id=" + $app_key + "&client_secret=" + $app_secret + "&grant_type=client_credentials&scope=SMS"
$auth_values = Invoke-RestMethod $auth_string

Now that we have an auth token, we can use it to send, receive, and check the status of messages.

# Send SMS
$tel_number = "0488888888"
$token = $auth_values.access_token
$body = "On average, cats spend 2/3 of every day sleeping. That means a nine-year-old cat has been awake for only three years of its life"
$sent_message = Invoke-RestMethod "https://api.telstra.com/v1/sms/messages" -ContentType "application/json" -Headers @{"Authorization"="Bearer $token"} -Method Post -Body "{`"to`":`"$tel_number`", `"body`":`"$body`"}"
$sent_message

At this point, I receive an SMS to my phone, which I can reply to

telstraSMS_reply

The message can also be queried to check its delivery status, and check if the message has been replied to, as below:

# Get Message Status
$messageid = $sent_message.messageId
$message_status = Invoke-RestMethod "https://api.telstra.com/v1/sms/messages/$messageid" -Headers @{"Authorization"="Bearer $token"}
$message_status
# Get Message Response
$message_response = Invoke-RestMethod "https://api.telstra.com/v1/sms/messages/$messageid/response" -Headers @{"Authorization"="Bearer $token"}
$message_response

Executing the above code gives me the following

telstraSMS_powershell

Now obviously, you can wrap all these up in some functions, parse in external parameters, strap into PowerShell workflows in FIM, incorporate into SSPR, and just about anything else you can think of (in your labs). There are some caveats to using the service, some obvious of course:

  • It’s SMS, so a 160 character limit applies
  • You can send only one message at a time
  • The service is not intended for large volumes of messages
  • 100 messages per day/1000 per month limit
  • The service is in beta
  • Telstra cannot guarantee delivery once the message is passed to another telco
  • Australia mobiles only

Initially in my testing, I found messages sat in the state of “SENT” and would not update to “DELIVERED”. After some general fist waving and mutterings about beta services, I rebooted my phone and the messages I had queued came through. Although I have had no issue with SMS delivery in the past, I’m happy to put this down to the handset bugging out. In all subsequent testing, the messages came through so quickly, that my phone buzzed immediately after hitting enter on the command.

I hope the code snippets provided help you out with spinning this up in your labs, but please check the Telstra T’s and C’s before sending out some informative cat facts.

 

Category:
FIM, PowerShell, Testing

Join the conversation! 5 Comments

  1. Nice one Dan, thanks.

  2. Dan – Love your work!

  3. Unfortunatley, the code provided here does not work as it is (at least not for me)… here is the working powershell code

    # Send SMS
    # Ensure everything thing should be in single line, while new line should be started with $ as per the code below.

    $tel_number = “04XXXXXXXX”
    $token = “enter_your_token_code_here”
    $body = “Testing the text from SMS API gateway, hohoooooo!!!”
    $sent_message = Invoke-RestMethod “https://api.telstra.com/v1/sms/messages” -ContentType “application/jason” -Headers @{Authorization=”Bearer $token”}-Method Post -Body “{`”to`”: `”$tel_number`”, `”body`”:`”$body`”}”
    $sent_message

  4. How to I receive the token in PowerShell window after running the first code?

Comments are closed.