Jenkins Build Failure Alerts to Telegram Without a Bot Token

Jenkins Build Failure Alerts to Telegram Without a Bot Token

push.tg

Most Jenkins-to-Telegram guides start with BotFather, a bot token, a chat ID and a plugin. You can skip all of that: a Declarative Pipeline's post section can run one curl command when a build fails, and push.tg turns that POST into a Telegram message.

Step 1: get your endpoint (30 seconds)

Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.

There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so everyone on call sees the messages.

Step 2: add a post section to your Jenkinsfile

pipeline {
  agent any
  environment {
    PUSHTG = 'https://push.tg/k3x9q'
  }
  stages {
    stage('Build') {
      steps {
        sh 'make test'
      }
    }
  }
  post {
    failure {
      sh 'curl -s --data-urlencode "data=FAILED: $JOB_NAME #$BUILD_NUMBER $BUILD_URL" "$PUSHTG"'
    }
    fixed {
      sh 'curl -s --data-urlencode "data=FIXED: $JOB_NAME #$BUILD_NUMBER" "$PUSHTG"'
    }
  }
}

failure runs only when the build failed. fixed runs when a build passes after the previous one failed or was unstable, so you also hear when it's green again, without a message for every normal build.

JOB_NAME, BUILD_NUMBER and BUILD_URL are standard Jenkins environment variables. BUILD_URL is only filled in if your Jenkins URL is set in the system configuration.

Two details that save you a headache

  • Keep the single quotes around the sh script. The shell then expands $JOB_NAME and the others; that's what the Jenkins docs recommend over Groovy string interpolation.
  • Use --data-urlencode "data=..." rather than -d. Build URLs and job names can contain characters like & or =, and sending the text as a URL-encoded data field keeps it in one piece.

Other conditions you can use

The post section also supports unstable, aborted, regression, unsuccessful, changed and always. For example, unsuccessful covers failed, unstable and aborted builds in one block.

Keep the address out of the Jenkinsfile (optional)

Anyone with your address can post to your chat. If the Jenkinsfile lives in a shared repo, store the URL as a Secret text credential and bind it with the Credentials Binding plugin:

withCredentials([string(credentialsId: 'pushtg-url', variable: 'PUSHTG')]) {
  sh 'curl -s --data-urlencode "data=FAILED: $JOB_NAME #$BUILD_NUMBER" "$PUSHTG"'
}

Step 3: test it

Make one build fail on purpose (sh 'exit 1' in a test branch) and watch Telegram. If nothing arrives, try the address from the agent with curl:

curl -d "test from $(hostname)" https://push.tg/k3x9q

Each Telegram chat or group gets its own push.tg address, so one team's pipelines can report to their group and another team's to theirs.

Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Send one test by hand and look at Telegram before you trust it:

Official references: Jenkins Pipeline syntax: post, Jenkins: using environment variables and Credentials Binding plugin.

Open @trafficRobot · push.tg

More copy-paste guides

Report Page