-
@ Pablo Xannybar
2023-04-07 22:39:19So I just created a quotes bot on Nostr. I'm adding new quotes to it all the time but already it is serving up quotes from Satoshi to Nietzsche.
Making one of these yourself is incredibly easy - much more so than conventional social media like Twitter as there's no locked down API to deal with or beg for keys to. You simply generate a new private key for the bot, add some metadata, setup a script to pick from a random selection your a predefined list of quotes, setup another script to feed the output into a new Nostr status and post it, then put that on your
crontab
so it runs automatically every hour, or two hours, or once a day, or whatever you want.This tutorial will show you how to do that even if you have no coding experience. All you need is access to something running Linux (even if it's just a Raspberry Pi or a cheap VPS) and... that's it.
Keep in mind that whatever you choose to set this up on has to be constantly running for the scripts to work. This is why a Raspberry Pi or a VPS are good options. It requires almost zero CPU power, RAM, or bandwidth.
Ready? Let's go!
Install the dependencies
To make sure all dependencies are installed, simply run:
bash sudo apt install python3 python3-pip python-is-python3 golang git
Make a user for the bot
Especially if you use a VPS for this, having separate non-privileged users for different tasks is just smart:
bash sudo adduser nostrbot
Choose a password then you can jump into the new user with:
bash sudo su - nostrbot
Install noscl
This is the magic sauce that will let you directly interact with Nostr in the command line. If you made a new user, log into it first, then run this:
bash go install github.com/fiatjaf/noscl@latest
Now let's make sure it'll work correctly:
bash nano ~/.bashrc
Scroll to the bottom and add:
bash export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save (ctrl + O) and exit (ctrl + X) then run:
bash source ~/.bashrc
Now you want to create the directory
noscl
can create its config file in when you run it and use it:bash mkdir -p ~/.config/nostr
If you followed the above correctly, you run
noscl
in the command line and you should get an output like this:bash Usage: noscl home noscl setprivate <key> noscl sign <event-json> noscl verify <event-json> noscl public noscl publish [--reference=<id>...] [--profile=<id>...] <content> noscl message [--reference=<id>...] <id> <content> noscl metadata --name=<name> [--about=<about>] [--picture=<picture>] noscl profile <key> noscl follow <key> [--name=<name>] noscl unfollow <key> noscl event <id> noscl share-contacts noscl key-gen noscl relay noscl relay add <url> noscl relay remove <url> noscl relay recommend <url>
That means it's working!
At this point you simply want to run three commands:
bash noscl key-gen noscl setprivate [key generated above] noscl public
The first generates a private/public keypair. The second is where you copy the new private key to set it as the one
noscl
will use. The third prints the public key you can use to look up the bot later on.You'll also need to add some relays. You can do that now or at the end. The process is simple:
bash noscl relay add wss://relay.2nodez.com noscl relay add wss://relay.snort.social noscl relay add wss://offchain.pub noscl relay add wss://nostr-relay.alekberg.net noscl relay add wss://no.str.cr
And so on... add as many as you like.
You can do this at any point, just make sure to add at least a few before the first time you run the bot, otherwise it won't have anything to publish to!
You can also add some metadata such as a name and an about section using the command line, or you can sign in with a client of your choice and use that instead for a friendly GUI option.
To do it on the command line:
bash noscl metadata --name="My Bot" --about="This is my Nostr bot for posting quotes." --picture="https://..."
Get your list of quotes and a Python script to pick one at random
This is the bit you'll have to do manually. Pick out some quotes you like and put them in a file called
quotes.txt
in the home folder. Make sure each quote is on a new line.Now create a Python script by running
nano quotepicker.py
and simply pasting this in:```python import random
text_file = open("quotes.txt") line = text_file.readlines()
print(random.choice(line)) text_file.close() ```
Now as long as you have your Python script and your
quotes.txt
with at least a few quotes for testing, both in the same directory, you should be able to simply run this and get a different one each time:bash python3 quotepicker.py
Keep in mind, the more quotes you have, the less likely it is you will get the same quote when you run the script again.
Once you have a decent number of quotes (I recommend at least 100) you can move onto the next step. You're almost done!
Setup the bash script and crontab
This bit is super easy and should take under a minute.
Run
nano nostrpost.sh
and paste this in:bash b=$(python3 /home/nostrbot/quotepicker.py) echo $b /home/nostrbot/go/bin/noscl publish "$b"
Now let's make it executable:
bash chmod +x nostrpost.sh
At this point, assuming you already added some relays, we can do a testrun:
bash ./nostrpost.sh
It should echo the quote it's picked then show you the progress of publishing to each relay.
If you look the bot up on your Nostr client of choice using the public key we printed earlier, assuming you have the same relays added, it should pop up and you can see it for yourself and even follow it!
And finally, assuming the test run went well, all you need to do is set the
crontab
so it runs automatically:bash crontab -e
Scroll to the bottom of this file and just add:
bash @hourly /home/nostrbot/nostrpost.sh >> /home/nostrbot/nostrbot.log
This will run your script every hour, on the hour (e.g. exactly 7pm) and write the results to a log file, which is useful for debugging if something breaks at any point.
At this point you're done. Add more relays if you want, wait for the next hour, and it should automatically post another quote!
If you found this helpful feel free to follow me on Nostr at the pubkey below and, if you're feeling generous, zap me at
xanny@xanny.family
!npub17rlc0emedw5xljztfqrmykjaacsx6ujvdas64zznjadrnhhwlavq4jjtgg
PV!