Add Scripts/bluesky-dm-to-telegram.sh
This commit is contained in:
89
Scripts/bluesky-dm-to-telegram.sh
Normal file
89
Scripts/bluesky-dm-to-telegram.sh
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -x
|
||||
|
||||
# https://docs.bsky.app/docs/get-started
|
||||
|
||||
PDSHOST="<URL>"
|
||||
BLUESKY_HANDLE="<HANDLE>.bsky.social"
|
||||
BLUESKY_PASSWORD="<PASSWORD>" # App-Passwort
|
||||
TOKEN_FILE="<FILEPATH>"
|
||||
ACCESS_JWT=$(cat $TOKEN_FILE | jq -r .ACCESS_JWT)
|
||||
REFRESH_JWT=$(cat $TOKEN_FILE | jq -r .REFRESH_JWT)
|
||||
CONVO_ID="<ID>" # Konversations-ID mit dem Bluesky-Bot
|
||||
MESSAGE_FILE="<FILEPATH>"
|
||||
CHAT_ID="<ID>" # Telegram-Chat mit dem Bot
|
||||
TOKEN="<TOKEN>" # Token des Telegram-Bot
|
||||
LAST_ID="<FILEPATH"
|
||||
|
||||
# Erstelle neue Session
|
||||
create_session() {
|
||||
curl -X POST $PDSHOST/xrpc/com.atproto.server.createSession \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"identifier": "'"$BLUESKY_HANDLE"'", "password": "'"$BLUESKY_PASSWORD"'"}'
|
||||
}
|
||||
|
||||
# DEBUG: Erstelle Post
|
||||
create_post() {
|
||||
curl -X POST $PDSHOST/xrpc/com.atproto.repo.createRecord \
|
||||
-H "Authorization: Bearer $ACCESS_JWT" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"repo\": \"$BLUESKY_HANDLE\", \"collection\": \"app.bsky.feed.post\", \"record\": {\"text\": \"Hello world! I posted this via the API.\", \"createdAt\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}}"
|
||||
}
|
||||
|
||||
# Hole alle Nachrichten
|
||||
get_messages() {
|
||||
curl -s "https://api.bsky.chat/xrpc/chat.bsky.convo.listConvos?limit=50" \
|
||||
-H "Authorization: Bearer $ACCESS_JWT" | jq .
|
||||
}
|
||||
|
||||
# Erneuere Session
|
||||
refresh_session() {
|
||||
curl -sX POST "$PDSHOST/xrpc/com.atproto.server.refreshSession" \
|
||||
-H "Authorization: Bearer $REFRESH_JWT" | jq '{ACCESS_JWT: .accessJwt, REFRESH_JWT: .refreshJwt}' \
|
||||
> $TOKEN_FILE
|
||||
}
|
||||
|
||||
# Hole Status der Session
|
||||
get_session() {
|
||||
curl -s -X GET $PDSHOST/xrpc/com.atproto.server.getSession \
|
||||
-H "Authorization: Bearer $ACCESS_JWT" | jq -r .active
|
||||
}
|
||||
|
||||
# Hole letzte Nachricht (Nachrichten-ID, URI, Handle)
|
||||
get_message() {
|
||||
curl -s -X GET https://api.bsky.chat/xrpc/chat.bsky.convo.getConvo?convoId="$CONVO_ID" \
|
||||
-H "Authorization: Bearer $ACCESS_JWT" \
|
||||
| jq '{message_id: .convo.lastMessage.id, uri: .convo.lastMessage.embed.record.uri, handle: .convo.lastMessage.embed.record.author.handle}' \
|
||||
> $MESSAGE_FILE
|
||||
}
|
||||
|
||||
#####
|
||||
|
||||
# Prüfe, ob Session noch aktiv ist oder erneuere Session
|
||||
session_active=$(get_session)
|
||||
if [[ $session_active = "true" ]]; then
|
||||
echo "Session ist aktiv."
|
||||
else
|
||||
refresh_session
|
||||
fi
|
||||
|
||||
# Hole letzte Nachricht
|
||||
get_message
|
||||
|
||||
# Erstelle Post-URL aus Variablen
|
||||
id=$(cat $MESSAGE_FILE | jq -r .message_id)
|
||||
uri=$(cat $MESSAGE_FILE | jq -r '(.uri | split("/")[-1])')
|
||||
handle=$(cat $MESSAGE_FILE | jq -r .handle)
|
||||
|
||||
# Prüfe, ob letzte Nachricht schon geschickt wurde
|
||||
if [[ $(cat "$LAST_ID") != "${id}" ]]; then
|
||||
|
||||
# Sende Post-URL an Telegram-Bot
|
||||
url="https://bsky.app/profile/${handle}/post/${uri}"
|
||||
|
||||
# Wenn senden erfolgreich, speichere Nachrichten-ID
|
||||
if curl -s -X POST https://api.telegram.org/bot$TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$url"; then
|
||||
echo ${id} > $LAST_ID
|
||||
fi
|
||||
fi
|
Reference in New Issue
Block a user