Compare commits
7 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a07e5c9fd4 | |||
|
|
8797d25b56 | ||
| b66ec3ca1f | |||
| 2a2ab5ffff | |||
|
|
24f69aa520 | ||
|
|
45816e0239 | ||
|
|
2c262a8770 |
7 changed files with 112 additions and 15 deletions
2
.env-example
Normal file
2
.env-example
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
UserToken='***'
|
||||
TargetInstance='***'
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,2 +1,6 @@
|
|||
quotes.json
|
||||
quotes.txt
|
||||
.env
|
||||
Credentials
|
||||
docker-compose.yml
|
||||
oauth.sh
|
||||
|
|
|
|||
9
Dockerfile
Normal file
9
Dockerfile
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
FROM bash:5
|
||||
WORKDIR /usr/local/quotebot
|
||||
|
||||
COPY main.sh ./
|
||||
|
||||
RUN adduser -S quotebot --disabled-password --uid 1000 && apk add curl && apk add uuidgen
|
||||
USER quotebot
|
||||
|
||||
ENTRYPOINT ["bash", "/usr/local/quotebot/main.sh"]
|
||||
11
README
11
README
|
|
@ -1,11 +0,0 @@
|
|||
Hello World!
|
||||
|
||||
This is a quote bot (shell script) which shows a random quote from a file every time it is invoked.
|
||||
|
||||
Note: You need to make it executable to use it.
|
||||
|
||||
The reason i use a file and not the web is hackability, at the moment.
|
||||
|
||||
Please provide a quotes.txt file with one line per quote.
|
||||
|
||||
Have fun!
|
||||
70
README.md
Normal file
70
README.md
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
Hello World!
|
||||
|
||||
This is a quote bot (shell script) which shows a random quote from a file every time it is invoked.
|
||||
|
||||
Note: You need to make it executable to use it.
|
||||
|
||||
chmod u+x main.sh
|
||||
|
||||
The reason i use a file and not the web is hackability, at the moment.
|
||||
|
||||
Please provide a quotes.txt file with one line per quote.
|
||||
|
||||
## REST-Version
|
||||
The Rest-Version has functioning mastodon connection over REST-API. You need to run the full oauth user authentication before. I tried to use the "Development" section in Mastodon Preferences but it didnt work. What did work was the out of bounds creation of a new application and authenticating it with a user. It does not appear in the user Development section.
|
||||
|
||||
Here's the documentation for the api:
|
||||
https://docs.joinmastodon.org/client/intro/
|
||||
|
||||
You essentially need to run the following commands:
|
||||
|
||||
|
||||
### test connection
|
||||
|
||||
curl https://mastodon.example/endpoint?q=test&n=0
|
||||
|
||||
### create app
|
||||
https://docs.joinmastodon.org/methods/apps/
|
||||
this will return secrets, write them down!
|
||||
|
||||
curl -X POST -F 'client_name=Bot3' -F 'redirect_uris=urn:ietf:wg:oauth:2.0:oob' -F 'scopes=read write push' -F 'website=https://myapp.example' https://mastodon.example.com/api/v1/apps
|
||||
|
||||
### verify the app works
|
||||
https://docs.joinmastodon.org/methods/apps/#verify_credentials
|
||||
|
||||
curl -H 'Authorization: Bearer ****' https://mastodon.example.com/api/v1/accounts/verify_credentials
|
||||
|
||||
### authorize a user
|
||||
https://docs.joinmastodon.org/methods/oauth/#authorize and https://docs.joinmastodon.org/client/authorized/#login you will have to input this in a browser then login and authorize the app, it will provide an authorization code
|
||||
|
||||
https://mastodon.example/oauth/authorize
|
||||
?client_id=CLIENT_ID
|
||||
&scope=read+write+push
|
||||
&redirect_uri=urn:ietf:wg:oauth:2.0:oob
|
||||
&response_type=code
|
||||
|
||||
### obtain token
|
||||
https://docs.joinmastodon.org/methods/oauth/#token
|
||||
|
||||
curl -X POST -F 'grant_type=authorization_code' -F 'client_id=\*' -F 'client_secret=\*' -F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' -F 'code=\*' https://mastodon.example.com/oauth/token
|
||||
|
||||
### make your first post
|
||||
|
||||
curl -i -H 'Authorization: Bearer ***' -d 'status=Hello World!' https://mastodon.example.com/api/v1/statuses
|
||||
|
||||
To find this out took me hours and hours. I'm writing it down to spare you time.
|
||||
|
||||
## automation
|
||||
the oauth part will me mostly automated at some point but rn it is still manual. feel free to help automate it.
|
||||
|
||||
## docker
|
||||
you can use the dockerfile and docker-compose.yml i provided. I will at some point push a docker image which is ready to use. Until then you will have to build the image first. Here's how:
|
||||
|
||||
git clone https://code.kraftw3rk.de/alex/quote-bot.git
|
||||
git switch rest-bot
|
||||
touch quotes.txt #(fill with quotes, one per line, no longer than your instance character limit!)
|
||||
mv .env-example .env #(insert the user token where the stars are!)
|
||||
mv docker-compose-example.yml docker-compose.yml
|
||||
docker compose up -d
|
||||
|
||||
Have fun!
|
||||
9
docker-compose-example.yml
Normal file
9
docker-compose-example.yml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
services:
|
||||
quotebot:
|
||||
container_name: quotebot
|
||||
image: haui/quotebot
|
||||
build: ./
|
||||
volumes:
|
||||
- ./.env:/usr/local/quotebot/.env
|
||||
- ./quotes.txt:/usr/local/quotebot/quotes.txt
|
||||
restart: unless-stopped
|
||||
22
main.sh
22
main.sh
|
|
@ -1,8 +1,22 @@
|
|||
#!/bin/sh
|
||||
|
||||
#if json quote list exist
|
||||
#!/bin/bash
|
||||
set -x
|
||||
IdempotencyKey="$(uuidgen)"
|
||||
. ./.env
|
||||
Authorization="${UserToken}"
|
||||
while /bin/true; do
|
||||
if [ -f "./quotes.txt" ]; then
|
||||
shuf -n 1 ./quotes.txt
|
||||
status=$(shuf -n 1 ./quotes.txt)
|
||||
|
||||
result=$(curl -X POST \
|
||||
-H "Authorization: Bearer ${Authorization}" \
|
||||
-H "Idempotency-Key: ${IdempotencyKey}" \
|
||||
-d "status=${status}" \
|
||||
"${TargetInstance}/api/v1/statuses") # HTTP/1.1
|
||||
|
||||
echo "${result}"
|
||||
|
||||
else
|
||||
echo "file does not exist!"
|
||||
fi
|
||||
sleep 3600
|
||||
done
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue