slackapi / slack-github-action

Send data into Slack using this GitHub Action!
MIT License
960 stars 150 forks source link

Support reacting to existing messages (and possibly other Slack HTTP APIs)? #269

Closed mjperrone closed 2 hours ago

mjperrone commented 11 months ago

Description

I would like to utilize the reactions.add method to react to a message

Feature request

Potential usage example

- name: React to an existing Slack message
  id: slack
  uses: slackapi/slack-github-action/react@v1.24.0
  with:
    channel-id: 'CHANNEL_ID'
    ts: ${{ steps.slack.outputs.ts }}
    name: 'thumbsup'
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

Api call that would result in

webResponse = await web.reactions.add({ ts, channel: channelId.trim(), name: emojiName});

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])

filmaj commented 11 months ago

Seems like a reasonable feature request! We will be revisiting this project in the new calendar year post holiday break, so will consider this request at that time.

mjperrone commented 11 months ago

Thanks for the quick response @filmaj , though I do hope to build a github workflow that reacts to slack messages sooner than next calendar year. Does slack provide (or is slack aware of) another github action that supports all interactions with the slack api? I would be okay with something with a worse interface than this if it works.

zimeg commented 11 months ago

@mjperrone as a workaround you can use a curl request and the reactions.add method to react to a posted message with something like this:

name: Post Slack message

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:

    - name: Send message into channel
      id: slack
      uses: slackapi/slack-github-action@v1.24.0
      with:
          channel-id: 'C0123456789'
          slack-message: "hello <!channel>"
      env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

    - name: Respond with affection
      run: |
        curl -X POST -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
          -H "Content-type: application/json" \
          --data '{"channel":"C0123456789","timestamp":"${{ steps.slack.outputs.ts }}","name":"heart"}' \
          https://slack.com/api/reactions.add
mjperrone commented 11 months ago

@mjperrone as a workaround you can use a curl request and the reactions.add method

Well that certainly makes me feel pretty silly for reaching for a purpose made github action for what I'm trying to do! cURLing the API directly turns out to be more concise and more flexible than using this action... 😅

In case anyone is interested, here is what I ended up with:

name: PR-Opened
run-name: PR Slack Message Notification
on:
  pull_request:
    types:
      - opened
      - ready_for_review
      - closed
  pull_request_review:
    types:
      - submitted
jobs:
  slack-notification:
    runs-on: ubuntu-latest
    name: Sends a message to Slack when a PR is opened
    if: (github.event.action == 'opened' && github.event.pull_request.draft == false) || github.event.action == 'ready_for_review'
    steps:
      - name: Post PR summary message to slack
        run: |
          curl -X POST -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
          -H "Content-type: application/json; charset=utf-8" \
          --data '{"channel":"CHANNEL","text":"${{env.SLACK_MESSAGE}}"}' \
          https://slack.com/api/chat.postMessage > output.json
          cat output.json
          cat output.json | jq -r '.ts' > slack-message-timestamp.txt
        env:
          SLACK_MESSAGE: "${{ github.event.pull_request.user.login }}: ${{ github.event.pull_request.html_url }} `${{ github.event.pull_request.title }}` (+${{ github.event.pull_request.additions }}, -${{ github.event.pull_request.deletions }})"
      - name: Cache slack message timestamp
        uses: actions/cache/save@v3
        with:
          path: slack-message-timestamp.txt
          key: ${{ github.event.pull_request.html_url }}
  slack-emoji-react:
    runs-on: ubuntu-latest
    name: Adds emoji reaction to slack message when a PR is closed or reviewed
    if: (github.event.action == 'closed' && github.event.pull_request.merged == false) || github.event.action == 'submitted'
    steps:
      - name: Decide which emoji to add
        run: |
          if [[ "${{ github.event.action }}" == "closed" ]]; then
            if [[ "${{ github.event.pull_request.merged }}" == "false" ]]; then
              echo "EMOJI=pr-closed" >> $GITHUB_ENV
            fi
          elif [[ "${{ github.event.action }}" == "submitted" ]]; then
            if [[ "${{ github.event.review.state }}" == "approved" ]]; then
              echo "EMOJI=white_check_mark" >> $GITHUB_ENV
            elif [[ "${{ github.event.review.state }}" == "changes_requested" ]]; then
              echo "EMOJI=x" >> $GITHUB_ENV
            elif [[ "${{ github.event.review.state }}" == "commented" ]]; then
              echo "EMOJI=speech_balloon" >> $GITHUB_ENV
            fi
          fi
      - name: Read slack message timestamp from cache
        uses: actions/cache/restore@v3
        with:
          path: slack-message-timestamp.txt
          key: ${{ github.event.pull_request.html_url }}
      - name: React to PR summary message in slack with emoji
        run: |
          SLACK_TIMESTAMP=$(cat slack-message-timestamp.txt)
          echo "${{env.EMOJI}} -> $SLACK_TIMESTAMP"
          curl -X POST -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
          -H "Content-type: application/json; charset=utf-8" \
          --data '{"channel":"CHANNEL","timestamp":"'$SLACK_TIMESTAMP'","name":"${{env.EMOJI}}"}' \
          https://slack.com/api/reactions.add
mjperrone commented 11 months ago

@filmaj feel free to close this since I got what I wanted. I'm not closing it in case you still think it's a worthwhile feature to consider.

filmaj commented 11 months ago

Thanks @zimeg for the great suggestion and thank you @mjperrone for sharing your solution!

I will keep this issue open as it's worthwhile to study when we revisit this project. Perhaps your solution above could be generalized for use with any Slack HTTP API that someone would want to easily integrate into GitHub Actions? 🤔

zimeg commented 2 hours ago

@mjperrone @filmaj And thank y'all both a ton for sharing these brainstorms! We've just released support for calling one of the Slack API methods as part of @v2.0.0 🚀

Now, posting and reacting to a message might resemble this:

- name: Send a message to channel
  id: message
  uses: slackapi/slack-github-action@v2.0.0
  with:
    method: chat.postMessage
    token: ${{ secrets.SLACK_BOT_TOKEN }}
    payload: |
      channel: ${{ secrets.SLACK_CHANNEL_ID }}
      text: "chore(<!channel>): merge w the latest changes on main"
- name: Respond with affection
  uses: slackapi/slack-github-action@v2.0.0
  if: ${{ steps.message.outputs.ok }}
  with:
    method: reactions.add
    token: ${{ secrets.SLACK_BOT_TOKEN }}
    payload: |
      channel: ${{ secrets.SLACK_CHANNEL_ID }}
      timestamp: "${{ steps.message.outputs.ts }}"
      name: heart

More details on updating to the latest version are outlined in the release notes and README, so for now I'll go ahead and close this issue 🙏 ✨