Skip to content

Commit

Permalink
Merge pull request #146 from stephenyeargin/slack-threads
Browse files Browse the repository at this point in the history
Allow response in thread
  • Loading branch information
stephenyeargin authored Oct 2, 2022
2 parents 2f8b637 + 9eccb3a commit 49271f1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ Then add **hubot-grafana** to your `external-scripts.json`:
| `HUBOT_GRAFANA_DEFAULT_TIME_ZONE` | No | Default time zone for rendered images (defaults to `""`) |
| `HUBOT_GRAFANA_ORG_ID` | No | Default organization id, need for image rendering in new versions of Grafana (defaults to `""`) |
| `HUBOT_GRAFANA_API_ENDPOINT` | No | Default rendering api endpoint, need for image rendering in new versions of Grafana (defaults to `d-solo`) |
| `HUBOT_GRAFANA_USE_THREADS` | No | When set to any value, graphs are sent in thread instead of as new message (only Slack supports this for now). |
| `HUBOT_GRAFANA_MAX_RETURNED_DASHBOARDS` | No | Count of dashboards to return to prevent chat flood (defaults to `25`) |

^ _Not required when `HUBOT_GRAFANA_PER_ROOM` is set to 1._

^^ _Not required for `auth.anonymous` Grafana configurations. All other authentication models will require a user-specific API key._


### Image Hosting Configuration

By default, *hubot-grafana* will assume you intend to render the image, unauthenticated, directly from your Grafana instance. The limitation is that you will only receive a link to those images, but they won't appear as images in most circumstances in your chat client.
Expand Down
9 changes: 8 additions & 1 deletion src/grafana.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# HUBOT_GRAFANA_S3_SECRET_ACCESS_KEY - Optional; Secret access key for S3
# HUBOT_GRAFANA_S3_PREFIX - Optional; Bucket prefix (useful for shared buckets)
# HUBOT_GRAFANA_S3_REGION - Optional; Bucket region (defaults to us-standard)
# HUBOT_GRAFANA_USE_THREADS - Optional; When set to any value, graphs are sent in thread instead of as new message.
# HUBOT_SLACK_TOKEN - Optional; Token to connect to Slack (already configured with the adapter)
# ROCKETCHAT_URL - Optional; URL to your Rocket.Chat instance (already configured with the adapter)
# ROCKETCHAT_USER - Optional; Bot username (already configured with the adapter)
Expand Down Expand Up @@ -76,6 +77,7 @@ module.exports = (robot) ->
rocketchat_user = process.env.ROCKETCHAT_USER
rocketchat_password = process.env.ROCKETCHAT_PASSWORD
max_return_dashboards = process.env.HUBOT_GRAFANA_MAX_RETURNED_DASHBOARDS or 25
use_threads = process.env.HUBOT_GRAFANA_USE_THREADS or false

if rocketchat_url && ! rocketchat_url.startsWith 'http'
rocketchat_url = 'http://' + rocketchat_url
Expand Down Expand Up @@ -395,6 +397,7 @@ module.exports = (robot) ->

# Format the title with template vars
formatTitleWithTemplate = (title, template_map) ->
title = '' unless title
title.replace /\$\w+/g, (match) ->
if template_map[match]
return template_map[match]
Expand All @@ -406,6 +409,7 @@ module.exports = (robot) ->
switch robot.adapterName
# Slack
when 'slack'
msg.message.thread_ts = msg.message.rawMessage.ts if use_threads
msg.send {
attachments: [
{
Expand Down Expand Up @@ -521,7 +525,7 @@ module.exports = (robot) ->
request.post testAuthData, (err, httpResponse, slackResBody) ->
if err
robot.logger.error err
msg.send "#{title} - [Slak auth.test Error - invalid token/can't fetch team url] - #{link}"
msg.send "#{title} - [Slack auth.test Error - invalid token/can't fetch team url] - #{link}"
else
slack_url = JSON.parse(slackResBody)["url"]

Expand All @@ -536,6 +540,9 @@ module.exports = (robot) ->
file: grafanaDashboardRequest()
filetype: 'png'

# Post images in thread if configured
uploadData['formData']['thread_ts'] = msg.message.rawMessage.ts if use_threads

# Try to upload the image to slack else pass the link over
request.post uploadData, (err, httpResponse, body) ->
res = JSON.parse(body)
Expand Down

0 comments on commit 49271f1

Please sign in to comment.