-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_listening.sh
executable file
·122 lines (95 loc) · 3.67 KB
/
signal_listening.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
# Get and print the script's absolute path
SCRIPT_PATH=$(readlink -f "$0" 2>/dev/null || realpath "$0")
# Get the directory of the script
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
echo "Absolute path to .env: $SCRIPT_DIR""/.env"
source $SCRIPT_DIR"/utils/utils.sh"
#################################################################
# SET VARIABLES
#################################################################
# Where the messages are received
RECEIVED_MESSAGES_FILE=$SCRIPT_DIR"/tmp/received_messages.json"
# commands and url extract from signal
URL=$SCRIPT_DIR"/tmp/url.txt"
GROUP_ID_URL=$SCRIPT_DIR"/tmp/group_id.txt"
NAME_ID_URL=$SCRIPT_DIR"/tmp/name.txt"
# cheatsheet file
CHEATSHEET_FILE=$SCRIPT_DIR"/cheatsheet.html"
# Attachment saved
ATTACHMENT=~/.local/share/signal-cli/attachments/
################################################################
################################################################
# Load environment variables
source $SCRIPT_DIR"/.env"
################################################################
# Execute signal-cli receive command and save the output to a file
timestamp_echo
echo "Listening for messages..."
signal-cli --output=json receive > $RECEIVED_MESSAGES_FILE
################################################################
### Process the received messages
################################################################
# Check if any messages were received
message=$(cat $RECEIVED_MESSAGES_FILE)
# if message is empty, exit
if [ -z "$message" ]; then
echo "No message found"
rm $RECEIVED_MESSAGES_FILE
exit 1
fi
jq -r --argjson groupIds "$TARGET_GROUP_ID" '
.envelope |
if .syncMessage != null and (.syncMessage.sentMessage.groupInfo.groupId // "" | IN($groupIds[])) then
{message: .syncMessage.sentMessage.message, groupId: .syncMessage.sentMessage.groupInfo.groupId, name: .sourceName}
elif .dataMessage != null and (.dataMessage.groupInfo.groupId // "" | IN($groupIds[])) then
{message: .dataMessage.message, groupId: .dataMessage.groupInfo.groupId, name: .sourceName}
else
empty
end
' $RECEIVED_MESSAGES_FILE | while IFS= read -r line; do
echo $line
if [[ $line == *"message"* ]]; then
url=${line#*'message": "'}
url=${url%'",'*}
echo $url >> "$URL"
echo "Message: ${url:0:30}"
elif [[ $line == *"groupId"* ]]; then
groupid=${line#*'groupId": "'}
groupid=${groupid%'"'*}
echo $groupid >> "$GROUP_ID_URL"
elif [[ $line == *"name"* ]]; then
nameid=${line#*'name": "'}
nameid=${nameid%'"'*}
echo $nameid >> "$NAME_ID_URL"
fi
done
# Check if the message is a help message and send the cheatsheet to the corresponding group ID
line_number=1
while IFS= read -r message_line; do
if echo "$message_line" | grep -q "help"; then
group_id=$(sed "${line_number}q;d" "$GROUP_ID_URL")
if [ -n "$group_id" ]; then
echo "Sending Cheatsheet to $group_id" >> "$LOG_FILE"
signal-cli send -g "$group_id" -m "[Bot] Here is a Cheatsheet" -a "$CHEATSHEET_FILE"
fi
fi
line_number=$((line_number + 1))
done < "$URL"
################################################################
################################################################
#### USE CASES
################################################################
# Torrent
source $SCRIPT_DIR"/utils/torrent.sh"
# latex
source $SCRIPT_DIR"/utils/latex.sh"
# Show and Delete files
source $SCRIPT_DIR"/utils/files.sh"
# Download files
source $SCRIPT_DIR"/utils/download.sh"
################################################################
# remove the received messages file from tmp
rm -r $SCRIPT_DIR/tmp/*
# delete downloaded attachments
rm -r "$ATTACHMENT"*