Skip to main content

A tool integrating Redash with Slack

Project description

redashsync is a tool integrating Redash with Slack. It gets data from Redash and send it to Slack channel.

Install

pip install redashsync

How to get necessary keys

Redash API Key and Query ID

Click Show API Key and You would get below address.

http://localhost/api/queries/2/results.json?api_key=2CHjsILfC3fD4Q8nJU7IejS2CuRtKG8lKztY1HOk

Your API Key : 2CHjsILfC3fD4Q8nJU7IejS2CuRtKG8lKztY1HOk

Your Query ID : 2

Slack Bot Token and Setting

https://api.slack.com/apps

Create New App.

Get Your Bot Token.

Add an OAuth Scope. 1) chat:write 2) files:write

Invite your slack bot.

Your YAML

Follow the format below to complete your yaml file.

# ./resource/sample.yml

slack_configs:
  slack:
    token: <YOUR_SLACK_BOT_TOKEN> # "xoxb-***-***-***"
    username: <YOUR_USER_NAME> # "redash-echo"

redash_configs:
  url: <YOUR_HOST> # "http://localhost"
  user_email: <YOUR_EMAIL> # "myname@example.com"
  query_api_key: <YOUR_QUERY_API_KEY>  # "2CHjsILfC3fD4Q8nJU7IejS2CuRtKG8lKztY1HOk"

Use

Send messages regularly

from redashsync.redash.data import Redash
from redashsync.slack.messages import Slack
from redashsync.config.yaml import Config
from datetime import datetime
from cron_converter import Cron

config = Config("./resource/sample.yml")
redashHost = config.getRedashHost()
redashQueryKey = config.getQueryApiKey()
redashQueryId = config.getQueryId()
slackBotToken = config.getSlackBotToken()

redash = Redash(redashHost)
slack = Slack(slackBotToken)

cron = Cron()
cron.from_string("*/1 * * * *") # Send every 1 minute
referenceTime = datetime.now()
schedule = cron.schedule(referenceTime)
expectedTime = schedule.next().isoformat()

while True:
    if(expectedTime == datetime.now().isoformat()[0:19]):
        print(expectedTime)
        expectedTime = schedule.next().isoformat()

        response = redash.getQueriesFromRedash(redashQueryId, redashQueryKey)
        result = redash.getColumnData(response, ['Open', 'Close'])
        # result : [ [123, 456], [789, 987], ... ]
        
        template = "*시가* : %s \n*종가* : %s \n"
        
        # 2 is inside array's length.
        result = redash.getMarkdown(template, result[0:5], 2)

        # "#redash-echo" is channel name.
        slack.sendMessage("#redash-echo", result)

Send a graph image

from redashsync.redash.data import Redash
from redashsync.slack.messages import Slack
from redashsync.config.yaml import Config
from datetime import datetime
from cron_converter import Cron

config = Config("./resource/sample.yml")
redashHost = config.getRedashHost()
redashQueryKey = config.getQueryApiKey()
redashQueryId = config.getQueryId()
slackBotToken = config.getSlackBotToken()

redash = Redash(redashHost)
slack = Slack(slackBotToken)

# Histogram
response = redash.getQueriesFromRedash(redashQueryId, redashQueryKey)
result = redash.getColumnData(response, ['Open', 'Close'])

# If you want to name image one.png and locate it at ./resource 
redash.visualizeHistplotAndSave(result, "./resource/one.png")
slack.sendFile("./resource/one.png", "#redash-echo", "Title", "comment")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redashsync-0.0.8.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

redashsync-0.0.8-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file redashsync-0.0.8.tar.gz.

File metadata

  • Download URL: redashsync-0.0.8.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for redashsync-0.0.8.tar.gz
Algorithm Hash digest
SHA256 ea0561b2b281cacb3c8f8149ee614f2350174a1f9543498434304ce9e8cadc14
MD5 0e7f1d82858173fa8aad9f74176dccb9
BLAKE2b-256 7d275cafe235744f9b48c169e610cb96ea53f8f954f178b813ee036886f93074

See more details on using hashes here.

File details

Details for the file redashsync-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: redashsync-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for redashsync-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 109d3c86a7eb1e1481c1376df37b959abebcb17a21e61b40fc7ae147f15f4995
MD5 3ac53ca24d6389c0743ca697f21402da
BLAKE2b-256 14c86ffa64b10647075acb86447157f34063c14e611dc4045f24405501ac393d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page