Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally use Slack's web API instead of per-channel webhooks #94

Merged
merged 6 commits into from
Jan 4, 2021

Conversation

yasunariw
Copy link
Collaborator

@yasunariw yasunariw commented Dec 21, 2020

Description of the task

Send Slack notifications by querying the API with an access token instead of querying per-channel webhooks.

As per #56, it's cumbersome to generate and maintain an incoming webhook per Slack channel, especially when there are multiple channels to message.

If the user can configure an access token, they can use the single token to message any channel that the bot is added to. This is a one-time setup, so additional channels can be configured later on without providing a webhook URL for it.

Summary:

  • notification in slack.atd: The API endpoint expects the channel to be provided as a field in the request body.
  • send_notification in api_remote.ml: Modified to use the stored access token to authenticate and send a message to the chat.postMessage API endpoint, falling back on webhooks if unable to find a token.
  • Better validation of Slack response. For some reason, Slack always returns a 200 response and communicates the presence of an error through a ok field in the payload, so checking for status code alone isn't enough to catch errors:(

How to test

Existing tests were promoted as the slack payload now contains a channel field, but otherwise no regressions.

make test

Additional tests were run on a dummy Slack workspace to check oauth behavior.

References

@yasunariw yasunariw changed the title Yasu/slack webhook to api Use Slack's web API instead of per-channel webhooks Dec 21, 2020
@yasunariw yasunariw requested a review from ygrek December 23, 2020 12:04
@yasunariw yasunariw marked this pull request as ready for review December 24, 2020 01:52
@yasunariw yasunariw changed the base branch from yasu/upgrade-status-rules to master December 24, 2020 03:17
@yasunariw yasunariw force-pushed the yasu/slack-webhook-to-api branch 4 times, most recently from cf9b42f to d527ce4 Compare December 30, 2020 12:43
Copy link
Contributor

@ygrek ygrek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for exhaustive PR descriptions and docs

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
documentation/secret_docs.md Outdated Show resolved Hide resolved
documentation/secret_docs.md Show resolved Hide resolved
lib/action.ml Outdated Show resolved Hide resolved
src/request_handler.ml Outdated Show resolved Hide resolved
lib/api_remote.ml Outdated Show resolved Hide resolved
lib/slack.atd Outdated Show resolved Hide resolved
lib/slack.ml Outdated Show resolved Hide resolved
@yasunariw
Copy link
Collaborator Author

@ygrek I reinstated webhooks and applied your oauth related suggestions to #104

@yasunariw yasunariw changed the title Use Slack's web API instead of per-channel webhooks Optionally use Slack's web API instead of per-channel webhooks Dec 31, 2020
lib/api_remote.ml Outdated Show resolved Hide resolved
Slack Webhooks and Web API should be able to share the same
payload structure.
Adds a way to send notifications without per-channel webhooks. The
sending mechanism will authenticate with a bearer token that is
valid for any channel that the bot has been added to. The channel
is specified in a new `channel` field in the notification payload.

A note on error handling: for web API requests, Slack always
returns a 200 response and communicates the error message in the
response body, so checking for status alone isn't enough.
@yasunariw yasunariw merged commit 8a7d4d3 into master Jan 4, 2021
@yasunariw yasunariw deleted the yasu/slack-webhook-to-api branch January 4, 2021 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use Slack's web API instead of per-channel webhooks
3 participants