appleboy/telegram-action

How to use condition (if) with message for success and failed cases?

Closed this issue ยท 5 comments

Congratulations on the project. It's helping me a lot!

Below is an example to illustrate the need:

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

notify:
  needs: deploy
  runs-on: ubuntu-latest
  steps:
    - name: Send Telegram Message on Push
      uses: appleboy/telegram-action@master
      with:
        to: ${{ secrets.TELEGRAM_TO }}
        token: ${{ secrets.TELEGRAM_TOKEN }}
        format: markdown
        if: ${{ needs.deploy.result == 'failure' }}
        message: *Sorry*. After *${{ github.event_name }}* was *not* deployed!
        if: ${{ needs.deploy.result != 'failure' }}
        message: After *${{ github.event_name }}* the deploy it was done!

Like this

message: ${{ condition && 'Success Message' || 'Failure Message' }}

Thanks, @albinpk!

I tried to use this format with multiple lines, but it was not possible. Here's an example:

notify:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - name: Send Telegram Message on Push
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          message: ${{ needs.deploy.result == 'failure' && `'Success Message' <br /> *${{ github.actor }}* created commit:` || `'Failure Message' <br /> *${{ github.actor }}* created commit:` }}

I saw that it is possible to use multiple lines in the message like this:

notify:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - name: Send Telegram Message on Push
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          message: |
            ${{ needs.deploy.result == 'failure' && 'Success Message' || 'Failure Message *${{ github.actor }}* created commit:' }}
            ...

Although, the value of *${{ github.actor }}* informed in the message text is not displayed. How to solve?

Another workaround is to split the step into two parts: success and failure.

  notify:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - name: Send failure message
        if: ${{ needs.deploy.result == 'failure' }}
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          message: |
            Failure Message *${{ github.actor }}* created commit

      - name: Send success message
        if: ${{ needs.deploy.result != 'failure' }}
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          message: |
            Success Message

Thanks, @albinpk!

this solves the problem.