-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannouncement-bot
54 lines (48 loc) · 2.64 KB
/
announcement-bot
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
#!/bin/bash
admin_user=''
admin_password=''
base_url=''
announcement_room=''
key_reaction='👌'
authenticate(){
checkauth=$(curl --silent -X GET --header 'Authorization: Bearer '"$apikey"'' ''"$base_url"'/_matrix/client/r0/admin/whois/'"$admin_user"'' | jq -r .error | cat)
if [[ $checkauth != "null" ]]; then
response=$(curl --silent -X POST --header 'Content-Type: application/json' --data '{"type":"m.login.password", "user":"'"$admin_user"'", "password":"'"$admin_password"'"}' ''"$base_url"'/_matrix/client/r0/login')
apikey=$(echo $response | jq -r .access_token)
fi
}
send_announcement() {
readarray -t users < <( curl --silent -X GET --header 'Authorization: Bearer '"$apikey"'' ''"$base_url"'/_synapse/admin/v2/users?from=0&limit=10000&guests=true' | jq .users[].name | sed 's/"//g' )
count=$(curl --silent -X GET --header 'Authorization: Bearer '"$apikey"'' ''"$base_url"'/_synapse/admin/v2/users?from=0&limit=10000&guests=true' | jq .total)
for user in ${users[@]}; do
curl --silent -X POST --header 'Authorization: Bearer '"$apikey"'' --header 'Content-Type: application/json' --header 'Accept: application/json' --data '{"user_id":"'"$admin_user"'","content":{"msgtype":"m.text","body":"'"$content"'"}}' ''"$base_url"'/_synapse/admin/v1/send_server_notice' > /dev/null 2>&1
done
}
sandstorm_by_darude() {
authenticate
curl --silent -X GET --header 'Authorization: Bearer '"$apikey"'' --header 'Accept: application/json' ''"$base_url"'/_matrix/client/r0/sync' | jq '.rooms.join.'"$announcement_room"'.timeline' | jq '.events[] | select((.type=="m.room.message") or (.type=="m.reaction")) | [{sender:.sender,content:.content.body,relation:.content."m.relates_to".event_id,rel_type:.content."m.relates_to".rel_type,key:.content."m.relates_to".key,id:.event_id,age:.unsigned.age}]' | jq '[.[] | . as $a | [keys[] | select($a[.] != null) | {(.): $a[.]}] | add]' | jq -s '.' > sync.json
msgcount=0
for line in `cat sync.json | jq '.[][] | select((.rel_type=="m.annotation") and (.key=="'"$key_reaction"'")) | [{"key":.key,"relation":.relation,"id":.id}]' | jq '.[]' | jq -s '.' | jq 'unique_by(.relation) | .[].relation' | sed 's/"//g'`; do
let "msgcount=msgcount+1"
grep -Fxq $line ANNOUNCEMENTLOG
if [ $? -eq 1 ]; then
echo $line >> ANNOUNCEMENTLOG
cat sync.json | jq --arg msgid $line '.[][] | select(.id==$msgid)' >> messages.json
fi
done
if [ -f messages.json ]; then
cat messages.json | jq .content | sed 's/^.//; s/.$//' > send
cat send | while read content; do
send_announcement
done
rm messages.json send
fi
rm sync.json
}
if [ ! -f ANNOUNCEMENTLOG ]; then
touch ANNOUNCEMENTLOG
fi
while true; do
sandstorm_by_darude
sleep 60
done