Skip to content

Instantly share code, notes, and snippets.

Last active April 7, 2021 02:07
Show Gist options
  • Save trolologuy/c290ac3edc46fe6bc2b69ccc497cd4bc to your computer and use it in GitHub Desktop.
Save trolologuy/c290ac3edc46fe6bc2b69ccc497cd4bc to your computer and use it in GitHub Desktop.
Simple Telegram notifications bot using curl. Planned for use with travis to get notified on build status sending most of the required information added a login-notifier as well..

How to use the script?

To be able to use this script, please follow the steps below. If something is unclear, don't hesitate to add a comment below.

1) Create a new Bot and get the $BOT_TOKEN

Request a bot token to the @Botfather Start a direct conversation and follow the steps described.

Once the new bot is created search for the sentence "Use this token to access the HTTP API:" This token will be the $BOT_TOKEN variable in the script.

2) Get the $CHAT_ID with @RawDataBot

Either start a discussion with @RawDataBot or add him to a group. and type /start. You will then receive something like

    "update_id": 123456,
    "message": {
        "message_id": 123456,
        "from": {
            "id": 123456,
            "is_bot": false,
            "first_name": "YOUR_NAME",
            "username": "YOUR_USERNAME",
            "language_code": "XX"
        "chat": {
            "id": YYYYYYYYY,
            "first_name": "YOUR_NAME",
            "username": "YOUR_USERNAME",
            "type": "private"
        "date": 123456,
        "text": "/start",
        "entities": [
                "offset": 0,
                "length": 6,
                "type": "bot_command"

The "YYYYYYYYY" value will be the $CHAT_ID in the script.

3) Setup travis CI to use the variables we got in the previous steps

Access your build within Travis CI, go to Settings / Environment Variables and add the "BOT_TOKEN" and "CHAT_ID". Don't forget to leave "Display value in build log" unchecked otherwise you'll publish your secret onto your build. Or consult this page of documentation.

4) Edit the .travis.yml

Edit your .travis.yml file to add the following lines to deploy:. Don't forget to adapt the settings if needed, for example "YOUR_BRANCH".

  - provider: script
    script: bash"
      branch: YOUR_BRANCH

5) Add the to the same folder as your .travis.yml file

The file can be found here

How to use the script?

A) Follow the Steps 1 and 2 from the previous setup to get the token and id. B) Create the telegram-login-notifier.shfile in /opt/ C) Exchange the values in the script (XXXXXXX and YYYYYYY). D) Add the following line in the /etc/profile: ./

curl -fqsSL | bash
IP=$(echo $SSH_CLIENT | awk '{ print $1}')
MESSAGE=$(echo "Login on Server: $(hostname) the $(date +%Y-%m-%d) at $(date +%H:%M) User: $USER" "from IP: "$IP)
curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$MESSAGE" > /dev/null
# This script aims to simplify the notification of the state of your Travis CI build via a simple telegram bot.
# For usage, please consult the here:
# You can find all other environment variables below:
if [ "${TRAVIS_TEST_RESULT}" == 0 ]
echo "The build was a success"
curl -s -X POST${BOT_TOKEN}/sendMessage -d chat_id="${CHAT_ID}" -d text=$'✅ Succesfull build by Travis-CI.\nRepository: '"${TRAVIS_REPO_SLUG}"$'\nBranch: '"${TRAVIS_BRANCH}"$'\nBuild URL: '"${TRAVIS_BUILD_WEB_URL}"$'\nCommit message: '"${TRAVIS_COMMIT_MESSAGE}"'' > /dev/null
elif [ "${TRAVIS_TEST_RESULT}" == 1 ]
echo "The build was a failure"
curl -s -X POST${BOT_TOKEN}/sendMessage -d chat_id="${CHAT_ID}" -d text=$'❌ Unsuccesfull build by Travis-CI.\nRepository: '"${TRAVIS_REPO_SLUG}"$'\n Branch: '"${TRAVIS_BRANCH}"$'\nBuild URL: '"${TRAVIS_BUILD_WEB_URL}"$'\nCommit message: '"${TRAVIS_COMMIT_MESSAGE}"'' > /dev/null
echo "The build status could not be retrieved"
curl -s -X POST${BOT_TOKEN}/sendMessage -d chat_id="${CHAT_ID}" -d text=$'⚠️ The state of the build made by Travis-CI could not be retrieved.\nRepository: '"${TRAVIS_REPO_SLUG}"$'\nBranch: '"${TRAVIS_BRANCH}"$'\nBuild URL: '"${TRAVIS_BUILD_WEB_URL}"$'\nCommit message: '"${TRAVIS_COMMIT_MESSAGE}"'' > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment