0008_How to send a message to Telegram a user and a group

First of all, you need to get a Telegram-Bot Token. How to do it here.

It’s hot to find out Chat ID is here.

The official FAQ is here.

 

The bot`s code is below.

#!/bin/bash
# A connection configuration
SSH_KEY="YOUR-SSH-KEY"
USER="USER NAME"
SERVER=<IP ADDRESS OR FQDN OF YOUR SERVER>

# A token and a Chat ID
TOKEN="YOUR TOKEN"
CHAT_ID="YOUR CHAT ID"

# A log`s folder and a log file
LOG_DIR="$(dirname "$0")/logs"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/${DATE}_YOUR-SCRIPT-NAME.log"

# Get the date in format dd:mm:YYYY
DATE=$(date +"%d_%m_%Y")


# A message to send
MESSAGE=$(ssh -n -i "$SSH_KEY" "$USER@$SERVER" <COMMAND ON YOUR REMOTE SERVER>)

# Send a message
curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" \
     -d chat_id="$CHAT_ID" \
     -d text="$MESSAGE"