Sharing my Websockets Real-time Notification Script

  • 3
  • Praise
  • Updated 9 months ago
  • (Edited)
In another thread I was asked to share my code for listening/receiving/processing real-time websocket notifications from the websockets api. My script receives the notification and then flashes our lights, sends a popup to your TV's and desktops, and then has Alexa (Amazon Echo) read the notification out loud.

Demo: (last 15 seconds of this youtube) https://www.youtube.com/watch?v=y09_YaduvEk

Code: http://pastebin.com/Pv92NCTa

Please be sure to read all the comments in the code before trying to use it.

PS - At the bottom of the script is the websocket connection that requires your client id and secret. I made a mistake and left in my application client id + secret. I've since reset my secret, but you'll need to edit that portion of the script to make it work for you.
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb

Posted 2 years ago

  • 3
Photo of xrak en

xrak en

  • 1,196 Points 1k badge 2x thumb
Matt, you left your home address in there. BTW, what app did you use to get the notification on TV?
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Whoops! Thanks for pointing that out. I emailed the site asking them to edit/remove it. The notifications are Ubuntu's native notifications. All of our desktops and TV's are Linux machines, running Ubuntu. So anytime there's a notification I'd like broadcasted, I send it to every machine on our network (via script on my server). See: https://wiki.ubuntu.com/NotifyOSD
Photo of xrak en

xrak en

  • 1,196 Points 1k badge 2x thumb
I was hoping you were using Fire TV (which is what I use), doesn't look like Fire TV supports any easy to use notification. Anyway, thanks again.
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Looks like you can code your own: https://developer.amazon.com/public/solutions/devices/fire-tv/docs/notifications-for-amazon-fire-tv  .... you could also send notifications to your phone, and then use this FireTV app to display the phone-based notifications: https://www.amazon.com/Christian-Fees-Notifications-for-Fire/dp/B00OESCXEK 
Photo of Amy

Amy

  • 68,970 Points 50k badge 2x thumb
Thanks for sharing your knowledge, Matt!
Photo of Michael Danser

Michael Danser

  • 80 Points 75 badge 2x thumb
Hey Matt, my software knowledge is limited. How are you getting Alexa to play a notification without actively triggering her (Hey Alexa...)?
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Hi Michael -- it's all explained here: https://forums.developer.amazon.com/questions/40799/workaround-for-push-notifications.html

And for those who noticed the original pastebin link doesn't work, here's a new one: http://pastebin.com/5UM22bSj
(Edited)
Photo of David Garozzo

David Garozzo

  • 180 Points 100 badge 2x thumb
Thanks for sharing your script! Very cool use cases that you have in there for using the data!

BW - I think you left your friends' phone numbers in there. Hopefully they haven't needed to change their numbers. :)
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Thanks for catching the phone numbers! Script edited!
Photo of David Garozzo

David Garozzo

  • 180 Points 100 badge 2x thumb
Matt, Do you know if the WebSockets (and WebHook) interface is only meant for those who have registered as a developer? That is, if somebody were to write an app to distribute couldn't just use their own token - each user would have to come here to request their own token, right? Where as with the API interface, you could distribute the code with your own tokens, and the user would just need to do the OAuth piece, and then they'd get the data for their own cars.

Then again, if that's true, then I'd be sharing my personal tokens for use with the API, and somebody else could use them for the WebSockets, and get my personal driving information. That wouldn't be good.

So, I guess you really do need to send every user to Automatic to register for a developer account, even with API?
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Hi David,

Actually, every user who authorizes with your application will show up in your websockets app. i.e. any users who follows the oauth workflow here: https://developer.automatic.com/api-reference/#oauth-workflow (given that the workflow is executed with your app client+secret), the user authorizing (their vehicles) will then show up in your websocket events.

For example, if I made my app public and gave you the link to the oauth URL and you clicked it, and then entered your automatic user/pass and granted my app access to your account, your cars would then start showing up in my websocket events. I would then want to have code in my application to only show you your cars when you visit my map. i.e. the responsibility for user privacy is on me, the app owner.

Does that make sense? End-users do not have to create developer accounts / developer apps.

Matt
Photo of David Garozzo

David Garozzo

  • 180 Points 100 badge 2x thumb
Wow. Okay. So if I were to write an app for public consumption, it really should only contain code that makes use of the API calls and not the web sockets. For web socket use, I would have to write a server piece that handled data, and then acted on those events - like send messages to specific users. The web socket code shouldn't be used client-side.

I guess from a security point of view, I would want to configure my app from the developer portal to not use web sockets at all then. Otherwise, anybody who had my client+secret would have full access to my web socket events. And I can't really keep my client+secret secure in a python script.

And if I found that it would be better if my client used the web sockets interface, then I would want to require clients to register their own developer accounts. Which, I believe, is what you have done with your sample script. Right?
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
I believe your understanding is correct. My app is not public, it's just for me and family, our cars, and I store our id and secret in my python script on my server. 

If I were a large company with adequate security and wanted to make an app for public usage, I would need to secure my servers so no one could ever locate my secret. (that's why it's called a secret :))
Photo of David Garozzo

David Garozzo

  • 180 Points 100 badge 2x thumb
Hey Matt,
Just wanted to say thanks for sharing your script. I should soon be releasing my plugin for Indigo (Mac home automation software), and I based some of it on code you developed and released to the community. Thanks!
Dave
Photo of Matt Farley

Matt Farley

  • 1,506 Points 1k badge 2x thumb
Awesome! Glad to hear it David!