diff --git a/.gitignore b/.gitignore index 6a920a0..7be50b7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ target *.json *.DS_Store **.DS_Store - +scpt/*.json diff --git a/README.md b/README.md index 336b472..e35cdb2 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,9 @@ $ ai r -s syu.is $ ai n ``` +### bot + +``` +$ ai bot +``` + diff --git a/ai.zsh b/ai.zsh new file mode 100755 index 0000000..21c3171 --- /dev/null +++ b/ai.zsh @@ -0,0 +1,27 @@ +#!/bin/zsh +case $OSTYPE in + darwin*) + alias date="/opt/homebrew/bin/gdate" + ;; +esac +d=${0:a:h}/scpt +source $d/env +source $d/refresh.zsh +source $d/token.zsh +source $d/reply.zsh +source $d/notify.zsh + +case $1 in + refresh|r) + refresh + ;; + token|t) + token + ;; + reply) + reply + ;; + notify|n) + notify + ;; +esac diff --git a/scpt/env b/scpt/env new file mode 100644 index 0000000..22b692b --- /dev/null +++ b/scpt/env @@ -0,0 +1,15 @@ +cfg=~/.config/ai/test.json +host=`cat $cfg|jq -r .host` +handle=`cat $cfg|jq -r .handle` +pass=`cat $cfg|jq -r .password` +date=`date --iso-8601=seconds` + +if [ ! -f $cfg.t ];then + $d/token.zsh +fi + +if [ -f $cfg.t ];then + token=`cat $cfg.t|jq -r .accessJwt` + refresh=`cat $cfg.t|jq -r .refreshJwt` + did=`cat $cfg.t|jq -r .did` +fi diff --git a/scpt/refresh.zsh b/scpt/refresh.zsh new file mode 100755 index 0000000..81998ce --- /dev/null +++ b/scpt/refresh.zsh @@ -0,0 +1,11 @@ +function refresh(){ + token=`cat $cfg.t|jq -r .accessJwt` + refresh=`cat $cfg.t|jq -r .refreshJwt` + if [ ! -f $cfg ];then + token + fi + url=https://$host/xrpc/com.atproto.server.refreshSession + j=`curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $refresh" $url` + echo $j + echo $j >! $cfg.t +} diff --git a/scpt/reply.zsh b/scpt/reply.zsh new file mode 100755 index 0000000..af7d7b0 --- /dev/null +++ b/scpt/reply.zsh @@ -0,0 +1,29 @@ +function reply() { + + url="https://$host/xrpc/com.atproto.repo.createRecord" + col="app.bsky.feed.post" + +json="{ + \"repo\": \"$handle\", + \"did\": \"$did\", + \"collection\": \"$col\", + \"record\": { + \"text\": \"$text\", + \"createdAt\": \"$date\", + \"reply\": { + \"root\": { + \"cid\": \"$cid\", + \"uri\": \"$uri\" + }, + \"parent\": { + \"cid\": \"$cid_p\", + \"uri\": \"$uri_p\" + } + } + } +}" + +echo $json|jq . +url=https://$host/xrpc/com.atproto.repo.createRecord +j=`curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" -d "$json" $url` +} diff --git a/scpt/token.zsh b/scpt/token.zsh new file mode 100755 index 0000000..0e5fda6 --- /dev/null +++ b/scpt/token.zsh @@ -0,0 +1,6 @@ +function token() { + url=https://$host/xrpc/com.atproto.server.createSession + j=`curl -sL -X POST -H "Content-Type: application/json" -d "{\"identifier\":\"$handle\",\"password\":\"$pass\"}" $url` + echo $j + echo $j >! $cfg.t +}