Analyse Sentiments In Issues and PRs And Get Notified
Tags: actionshackathon, githubactions, opensource
Published: 2020-09-17 17:51:27

### My Workflow
I created an action named [Code of conduct compliance through sentiments analysis](https://github.com/marketplace/actions/code-of-conduct-compliance-through-sentiments-analysis) and want to use it in a workflow with Slack integration in the [AsyncAPI Initiative](https://www.asyncapi.com/). I'm a maintainer and community guardian in this project and want to make sure that people respect each other when working in the project.

### Submission Category: Maintainer Must-Haves

Cause you want to keep your community healthy and know when people disrespect each other.

### Yaml File and Link to Code

The most basic example of using this action with other actions is when you want to send the information about negative sentiments to some communicator. Below example shows how to use this action with another action for sending messages to Slack:

```yaml
name: 'Sentiment analysis'

on: 
  issue_comment:
    types: 
      - created
      - edited
  issues:
    types:
      - opened
      - edited
  pull_request:
    types:
      - opened
      - edited
  pull_request_review:
    types:
      - submitted
      - edited
  pull_request_review_comment:
    types:
      - created
      - edited
jobs:
  test:
    name: Checking sentiments
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Check sentiment
        uses: derberg/code-of-conduct-sentiment-analysis-github-action@v1
        # id is needed for a step if you want to access its output in another step
        id: sentiments
        with:
          # you can find an instruction on how to setup Google project with access to Natural Language API here https://github.com/BogDAAAMN/copy-sentiment-analysis#gcp_key-get-your-gcp-api-key
          gcp_key: ${{ secrets.GCP_KEY }} 
      - uses: someimportantcompany/github-actions-slack-message@v1
        # this step runs only if sentiment is a negative numner
        if: steps.sentiments.outputs.sentiment < 0
        with:
          # to get a webhook url of your channel use this documentation https://slack.com/intl/en-pl/help/articles/115005265063-Incoming-webhooks-for-Slack
          # first register new app here (https://api.slack.com/apps
          webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }}
          text: Here ${{steps.sentiments.outputs.source}} you can find a potential negative text that requires your attention as the sentiment analysis score is ${{steps.sentiments.outputs.sentiment}}
          color: orange
```

This is what you see in Slack once the negative sentiment is spotted:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7rbxcp82dx1a7mopqhgy.png)

Not using Slack? then check different actions:

- [Discord integration](https://github.com/marketplace/actions/discord-message-notify)
- [Send an email](https://github.com/marketplace/actions/send-email)

There are plenty of different notifications-like actions that you can use. Check [this](https://github.com/sdras/awesome-actions/blob/master/README.md#notifications-and-messages) awesome list.

The rest of the examples of using the action are stored in the readme of the GitHub Action.

{% github https://github.com/derberg/code-of-conduct-sentiment-analysis-github-action %}


### Additional Resources / Info

This action can analyze sentiments using two different solutions: built-in and 3rd party. Out of the box, this action is integrated with [sentiment](https://www.npmjs.com/package/sentiment) package to do analytics based on [AFINN](http://www2.imm.dtu.dk/pubdb/pubs/6010-full.html) wordlist. Instead of such basic analytics, better use this action to communicate with [Google Natural Language API](https://cloud.google.com/natural-language/docs/basics).

Google API is [not expensive](https://cloud.google.com/natural-language/pricing). It is free up to 5k requests and 1$ up to 1m requests. I recommend this way instead of the dictionary check. 

> Cover photo by <a href="https://unsplash.com/@grakozy?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Greg Rakozy</a> on <a href="https://unsplash.com/s/photos/stars?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>