Github Action to post a Telegram notification with no dependencies

Here’s a Github Action that posts a notification message to a Telegram chat, without using any dependencies.

---
name: Post to Telegram
description: Post a message to a Telegram channel
inputs:
  telegram_bot_token:
    description: Auth token of the Telegram bot via which to post.
    required: true
  telegram_chat_id:
    description: ID of the Telegram channel to which to send the message.
    required: true
  message_content:
    description: String content of the Telegram message to send.
    required: true
runs:
  using: "composite"
  steps:
    - shell: bash
      run: |
        curl "https://api.telegram.org/bot${{ inputs.telegram_bot_token }}/sendMessage" \
          --header "Content-Type: application/json" \
          --request POST \
          --data '{
            "chat_id": "${{ inputs.telegram_chat_id }}",
            "text": "${{ inputs.message_content }}"
          }'

The key part of the action is the bash script that uses curl to post a message to Telegram:

curl "https://api.telegram.org/bot${{inputs.telegram_bot_token}}/sendMessage" \
  --header "Content-Type: application/json" \
  --request POST \
  --data '{
    "chat_id": "${{ inputs.telegram_chat_id }}",
    "text": "${{ inputs.message_content }}"
  }'

You can call the action like this to post a notification about a new PR:

---
jobs:

  telegram_notification:
    name: telegram_notification
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{github.event.after}}
      - uses: ./.github/actions/post_telegram
        with:
          telegram_bot_token: '${{ secrets.TELEGRAM_BOT_TOKEN }}'
          telegram_chat_id: '${{ secrets.TELEGRAM_CHAT_ID }}'
          message_content: |
            ${{ github.repository }}
            ${{ github.actor }} created
            ${{github.event.pull_request.title}}
            PR: https://github.com/${{github.repository}}/pulls/${{github.event.number}}

You’ll need to get a Telegram bot auth token and the target chat ID and store them as Github secrets. You’ll also need to add the bot to the chat so that it is able to post there.


Tech mentioned