Instant telegram notification if GitHub is Stared

· 2 min read

I really like the sense of achievement from open source projects. Over the past year, I’ve consistently worked on several open source projects and have gained some stars from users, with some even adding me on WeChat to raise requirements - this is the charm of open source.

For example, when someone stars your project, it would be great to receive Telegram notifications promptly. Since I’ve already implemented NPM package release notifications before, this was relatively easy to do, but there were still some pitfalls, so I’m documenting them here which might help some friends.

Configuration

on:
  watch:
    action: started
env:
  REPO_NAME: ${{ github.event.repository.name }}
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Notify
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_TO }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          message: Someone stars **${{env.REPO_NAME}}** repository, see [here](https://github.com${{github.repository}}).
          format: markdown

Configuration Instructions

There are many variables in GitHub Action. ${{ github.event.repository.name }} and ${{github.repository}} are context variables that don’t need to be configured - you can use them directly. ${{ secrets.TELEGRAM_TO }} is a repository configuration variable that needs to be set in the repository settings.

GITHUB_SERVER_URL Doesn’t Work?

The official documentation states that the default environment variable is $GITHUB_SERVER_URL, and the usage method is $GITHUB_SERVER_URL. However, if you write it in the message above, it won’t work. The reason is that environment variables are used when working with shell commands, but if you need to use the GitHub context in a job step, you need to use the proper context syntax.

Telegram Configuration Parameter Acquisition

  • TELEGRAM_TOKEN

    • Create a bot through @BotFather to obtain the token. Note that the complete token format will look like this: 12345678:BBFntuCD6nRx1ZIYZ-eCyfP1UO4FeAjnz2M
  • TELEGRAM_TO

    • You need to send a message to the bot to ensure the chat is enabled

    • Visit https://api.telegram.org/bot$TELEGRAM_TOKEN/getUpdates to get the chatID from the response

Final Thoughts

As described above, you can implement Telegram notifications. Get started!