Last active
January 14, 2024 20:02
-
-
Save embersee/85ee467838e3fed442fa7a979f474f2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| TELEGRAM_BOT_TOKEN="" | |
| CHAT_ID="" | |
| title="$1" | |
| message="$2" | |
| disable_notification=false | |
| # Function to display script usage instructions | |
| function display_help { | |
| echo "Usage: $0 <title> <message> [OPTIONS]" | |
| echo "Send a formatted message to a Telegram chat." | |
| echo | |
| echo "Options:" | |
| echo " -n, --no-notification Disable notifications" | |
| echo " -h, --help Display this help and exit" | |
| exit 1 | |
| } | |
| # Function to capitalize the first letter of the title | |
| function capitalize_title { | |
| echo "$1" | awk '{print toupper(substr($0, 1, 1)) tolower(substr($0, 2))}' | |
| } | |
| # Parse command line options | |
| while [[ "$#" -gt 0 ]]; do | |
| case $1 in | |
| -n|--no-notification) | |
| disable_notification=true | |
| ;; | |
| -h|--help) | |
| display_help | |
| ;; | |
| *) | |
| title=$(capitalize_title "$1") | |
| message="$2" | |
| break | |
| ;; | |
| esac | |
| shift | |
| done | |
| # Get formatted date | |
| formatted_date="$(date '+%a, %d %b %Y')" | |
| # Get time | |
| time="$(date '+%H:%M:%S')" | |
| # Get hostname without .local | |
| hostname="$(hostname | sed 's/.local//g')" | |
| # Construct the message | |
| formatted_message="*${title}*\n_${formatted_date} at ${time}_\nFrom: *${hostname}*\n${message}" | |
| # Send the message using curl | |
| curl -X POST \ | |
| -H 'Content-Type: application/json' \ | |
| -d "{\"chat_id\": \"$CHAT_ID\", \"text\": \"$formatted_message\", \"parse_mode\": \"Markdown\", \"disable_notification\": $disable_notification}" \ | |
| "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" > /dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment