-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotelegram.sh
More file actions
104 lines (89 loc) · 3.08 KB
/
totelegram.sh
File metadata and controls
104 lines (89 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Script Information
# Name: totelegram.sh
# Version: 0.1
# Author: drhdev
# Description: This script sends files or file contents to a specified Telegram chat using a bot. It supports sending files as attachments or text content of files as messages. It includes logging capabilities and can be run as a cron job.
# License: GNU Public License
# Installation: Download this script, place it in a desired directory. Make it executable with 'chmod +x totelegram.sh'. Ensure 'curl' is installed on your system.
# Usage: Run './totelgram.sh -file <file_path>' to send a file as an attachment, './totelgram.sh -message <file_path>' to send file content as a message. Use './totelgram.sh -help' for more information. For cron job: '0 2 * * * /path/to/totelgram.sh -file <file_path>' to run daily at 2 am.
# Configuration
TOKEN='YOUR_BOT_TOKEN_HERE'
CHAT_ID='YOUR_CHAT_ID_HERE'
LOG_DIR='/var/log/totelegram'
LOG_FILE="${LOG_DIR}/totelgram.log"
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Function to log message
logMessage() {
local msg="$1"
local date_time="$(date '+%Y-%m-%d %H:%M:%S')"
local hostname="$(hostname)"
echo "${date_time} | ${hostname} | Chat ID: ${CHAT_ID} | ${msg}" >> "${LOG_FILE}"
}
# Function to send message
sendMessage() {
local message=$(<"$1") # Read file content
local response=$(curl -s -X POST https://api.telegram.org/bot$TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$message")
logMessage "Message sent: $(basename "$1")"
[[ "$verbose" == true ]] && echo "Message sent: $(basename "$1")"
}
# Function to send file
sendFile() {
local file="$1"
local response=$(curl -s -X POST -F chat_id=$CHAT_ID -F document=@"$file" https://api.telegram.org/bot$TOKEN/sendDocument)
logMessage "File sent: $(basename "$file")"
[[ "$verbose" == true ]] && echo "File sent: $(basename "$file")"
}
# Function to show help
showHelp() {
cat << EOF
Usage: $0 -file|-message|-help <file_path> [--verbose]
Options:
-file <file_path> Send a file as an attachment.
-message <file_path> Send the content of a file as a message.
-help Display this help and exit.
--verbose Show operation responses on the screen.
Log File:
Logs are written to $LOG_FILE.
EOF
}
# Default to silent mode
verbose=false
# Check arguments
if [[ "$1" == "--verbose" ]]; then
verbose=true
shift
fi
if [[ "$#" -lt 2 ]]; then
showHelp
exit 1
fi
# Process command line arguments
case "$1" in
-file)
if [ ! -f "$2" ]; then
logMessage "Error: File $2 not found."
[[ "$verbose" == true ]] && echo "Error: File $2 not found."
exit 1
fi
sendFile "$2"
;;
-message)
if [ ! -f "$2" ]; then
logMessage "Error: File $2 not found."
[[ "$verbose" == true ]] && echo "Error: File $2 not found."
exit 1
fi
sendMessage "$2"
;;
-help)
showHelp
;;
*)
logMessage "Invalid option: $1"
showHelp
exit 1
;;
esac
[[ "$verbose" == true ]] && echo "Operation completed."