Skip to content

Instantly share code, notes, and snippets.

@trolologuy
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 telegram-notifier.sh 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 notify.sh)"
    on:
      branch: YOUR_BRANCH

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

The file can be found here

How to use the telegram-login-notifier.sh 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 telegram-login-notifier.sh script (XXXXXXX and YYYYYYY). D) Add the following line in the /etc/profile: ./telegram-login-notifier.sh

#!/bin/bash
curl -fqsSL gist.githubusercontent.com/trolologuy/c290ac3edc46fe6bc2b69ccc497cd4bc/raw/53f038f266bbbbff342c549e77c10f4fc401f2a9/telegram-notifier.sh | bash
#!/bin/bash
BOT_TOKEN=XXXXXXX
CHAT_ID=YYYYYYY
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)
URL="https://api.telegram.org/bot$BOT_TOKEN/sendMessage"
curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$MESSAGE" > /dev/null
#!/bin/bash
# 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 readme.md here:
# https://gist.github.com/trolologuy/c290ac3edc46fe6bc2b69ccc497cd4bc#file-readme-md
# You can find all other environment variables below:
# https://docs.travis-ci.com/user/environment-variables/
if [ "${TRAVIS_TEST_RESULT}" == 0 ]
then
echo "The build was a success"
curl -s -X POST https://api.telegram.org/bot${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 ]
then
echo "The build was a failure"
curl -s -X POST https://api.telegram.org/bot${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
else
echo "The build status could not be retrieved"
curl -s -X POST https://api.telegram.org/bot${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
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment