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

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

Open
4 of 10 tasks
mjperrone opened this issue Dec 7, 2023 · 6 comments
Open
4 of 10 tasks
Labels
enhancement New feature or request semver:minor

Comments

@mjperrone
Copy link

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 [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

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

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.
@filmaj filmaj added enhancement New feature or request semver:minor labels Dec 7, 2023
@filmaj
Copy link
Contributor

filmaj commented Dec 7, 2023

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
Copy link
Author

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
Copy link
Member

zimeg commented Dec 8, 2023

@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
Copy link
Author

@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
Copy link
Author

@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
Copy link
Contributor

filmaj commented Dec 11, 2023

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? 🤔

@filmaj filmaj changed the title Support reacting to existing messages Support reacting to existing messages (and possibly other Slack HTTP APIs)? Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver:minor
Projects
None yet
Development

No branches or pull requests

3 participants