plotlyImageToSlack
downloads the image of a Plotly chart and messages it to you via Slack. plotlyImageToSlack
is intended to be run as an AWS Lambda function.
Note: at the time of creation Plotly image previews did not load in Slack.
-
boto3: We assume that you are familiar with using AWS's
boto3
Python client, and that you have followed AWS's instructions to configure your AWS credentials. -
Plotly: You will need a Plotly account and API token. If you are not already a Plotly user you can test this function with graphs from their public feed
-
Slack: You will need a Slack channel where you would like to post your Plotly images, and a token that has permissions to post to this channel. We suggest using the token from a bot user.
Run pip install -r requirements.txt
to install development requirements. Once you've followed the steps in the previous section, run the following code to create or fetch an IAM role, and create a Lambda function:
python create_lambda_function.py \
--plotly-api-token <YOUR PLOTLY API TOKEN> \
--plotly-username <YOUR PLOTLY USERNAME> \
--slack-channel <'#channel' or '@username' on SLACK> \
--slack-token <SLACK BOT TOKEN>
You can test your function by running:
from aws_clients import LambdaClient
lambda_client = LambdaClient()
lambda_client.invoke("https://plot.ly/~matlab_user_guide/436")
If the function was configured correctly, you should receive a slack message with a Plotly image.
You can debug, change environment variables, and test via the AWS Console. test_event.json
is a test event that you can copy-paste into the AWS Lambda console.
You'll find util functions for the lambda
and iam
boto clients in aws_clients.py, and a script for re-packaging the Lambda function and its dependencies in zip_for_lambda.sh.
To learn more about packaging and deploying functions to Lambda, read my blog post AWS Lambda Functions Made Easy.