Automated posting to the StockTwits API using queues

StockTwits has a great API that allows you to automatically post to the website. I process a lot of stock market data, and whenever something interesting happens, I want to send an alert via the StockTwits website. A typical design pattern for this type of system is a message queue. Here, I decided to implement with Amazon SQS.

The source code for the worker.

NOTE: Additional code is required to queue the messages; that should be a straightforward exercise for the reader, but that’s as simple as resp = sqs_client.send_message({ queue_url: STOCKTWITS_QUEUE_URL, message_body: "Whatever you want to say" }). An additional hurdle to get up and running would be registering your application with StockTwits ( and retrieving an access token for your own account: Helpful tip: you do not need to set up endpoint to receive the StockTwits authorization postback as it’s a GET request and the parameter you need to extract (“code”, for use here: curl --data "client_id=CLIENT_ID&client_secret=SECRET&code=CODE&grant_type=authorization_code&redirect_uri=") appears as a URL query parameter.

This should be running constantly in the background of some server. However, a possibly interesting approach might be to port the program to AWS Lambda and then modify the program’s to use trigger another Lambda invocation (in the future or immediately) instead of using a while loop with recursion. This technique definitely merits exploration. In such a design, a Lambda invocation should be triggered immediately after queuing. After the Lambda main function ends, it should either invoke again or invoke for exponential_backoff seconds in the future. Or, rather than invoking manually after queueing, you could use Lambda scheduling, instead, to invoke every minute and send an entire batch of messages. The main benefit of this design pattern is that you don’t have to maintain a server with the worker running.

Other Notes:

– You may wish to store your STOCKTWITS_QUEUE_URL, AWS ACCESS_KEY and AWS SECRET in environment variables
– If you are interested in following me on StockTwits, you may do so here: