[ad_1]
This article was originally published by Gigi at dergigi.com
I have bullish on nostr for a while now, but even I am surprised by how quickly the ecosystem around this open protocol is growing. Yes, it’s still early. But it’s a good time to look around and damage build something. How else are you going to explore and learn?
So after porting two of my bots to nostr I thought why not write a little guide on it helped fuel the bot revolution shows how sausages are made.
While there’s a bit of piping involved, creating a bot that does nothing but post stuff is a breeze. And best of all, we don’t even need to write a single line of code!
All right, let’s destroy it.
- Results: create a bot that says “gm” every morning.
- Approach: run noscl in a cron job set to 6:15am
There he is. Simple.
If you have a linux system running somewhere and know your way around the command line, you’re basically done. If you are a normal person and don’t have it, then read on.
We will use Ubuntu Server, as it is widely available.
7 steps to GM:
- 1) Create a custom bot user
- 2) Roleplay as a bot and install go
- 3) Set the path of the road
- 4) Install noscl
- 5) Create bots
- 6) Say “Good morning!”
- 7) Automate with crontab
1) Create a custom bot user
We’re going to create a special user on our system that does nothing but post “gm” to nostr. Let’s call him “gmbot”.
$ sudo useradd -d /home/gmbot -m gmbot
I’ll let you choose a password.
$ sudo passwd gmbot
We will also add this user to the sudoers group:
$ sudo usermod -aG sudo gmbot
Great, done. Active to use users!
2) Roleplay as a bot and install go
Let’s push the gear stick before we switch to automatic. Switch to gmbot user:
$ su gmbot
The nostr client we’ll be using is written in go, therefore we need to install go:
$ sudo snap install go --classic
If you want to avoid snaps, you can also install go from source or via a custom PPA.
Whichever installation method you use, make sure it is go
Work:
$ go version
If you see output that says go version go1.19.5 linux/amd64
(or something like this) we can move on to the next step. Road!
3) Set the path of the road
Your edits .bashrc
with the editor of your choice…
$ vim ~/.bashrc
…and set the go path by adding the following at the end of the file:
# Set go path to user's home directly
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save, exit and load:
$ source ~/.bashrc
4) Install noscl
As mentioned before, we’ll be using fiatjaf’s noscl client to post notes and other things. Install the latest version via go install
:
$ go install github.com/fiatjaf/[email protected]
It will install noscl
for your “gmbot” user, because we have set the installation path to the user’s home directory. Neat!
Now try it!
$ noscl
This is what it will spit out:
> can’t open config file /home/gmbot/.config/nostr/config.json:
> open /home/gmbot/.config/nostr/config.json: no such file or directory
Ouch, that’s not good.
The config directory doesn’t exist, so noscl
unable to create the required configuration file. Making directories fixes this!
$ mkdir -p ~/.config/nostr
Now noscl should work. Run again…
$ noscl
…and you will see the following “usage” text:
Usage:
noscl home
noscl setprivate <key>
noscl sign <event-json>
noscl verify <event-json>
...
We will do a noscl setprivate
next, which will include our bot’s private key in the configuration file, effectively creating a bot account on the nostr protocol. (Side note: if an account is created in a forest, but never publishes records, does it exist?)
5) Create bots
Every nostr account needs a private key. No locks, no signed messages, no “good morning” posts. Simple as that.
You can make your bot lock by rolling some dice. Don’t have dice? Well, that sucks.
luckily, noscl
can generate keys as well. So let’s use that to generate a private key for your bot.
$ noscl key-gen
This will produce output like this:
> seed: armed birth test cargo ... orchard autumn
> private key: fc4b95d1....c5b98bd
Copy-paste the private key and set it via noscl setprivate
. Unless you hate yourself. If you hate yourself, type by hand.
$ noscl setprivate <KEY_GENERATED_ABOVE>
You have to use your real private key instead <KEY_GENERATED_ABOVE>
. Easy mistake to make! Also make sure to save your bot’s private key personal. “Not your keys, not your notes” and so on.
6) Say “Good morning!”
Pretend it’s 6:15 am. It’s time to say “good morning”.
You can try shouting “good morning” into the void, but noscl
will yell back at you, letting you know you have no relay set up. No relay, no party. We need to add at least one relay to speak to the nostrverse, so let’s add everyone’s favorite relay: your mother.
$ noscl relay add wss://nostr.mom
If successful, you will see something like:
> Added relay wss://nostr.mom.
You don’t have to use this exact relay, and you are free to add multiple relays. Consult nostr.watch for a list of public relays.
It’s time to publish “good morning!”
$ noscl publish "Good morning!"
Ta-daa! Your bot is still alive! You will see something like this:
> Sent event 4869429dcc20bd87567e3370c577793aac58f66bb07d130562738285dee6569f to 'wss://nostr.mom'.
You can use a “nostr explorer” such as nostr.guru to look up the event, i.e. the note you just published. It may take a minute for it to appear, but if all else works, you’ll see it stare back at you:
7) Automate with crontab
Still logged in as gmbot
user, edit the user-level crontab with
$ crontab -e
…and add the following line at the end of the file:
15 6 * * * /home/gmbot/go/bin/noscl publish "Good morning!"
Save the file & exit, and crontab will notify you that it is installing a new crontab. A very good crontab, but also very redundant.
> crontab: installing new crontab
There he is!
Now every morning — as long as the server you’re using to set all this up is running — the bot you create will amaze the world with a “Good morning!” censorship resistant.
You can use your bot’s private key to update profile metadata, i.e. set a name, description, profile picture, banner, and so on. You can even give your bot an express address, like I did!
If you found this little tutorial valuable, please feel free to give it back by throwing a few sats at the bot: ⚡[email protected]rgigi.com
Follow bots on nostr:
npub1gmgmj6punek0gjp6s26e0d5nfumghmszeg33vgqzs6vhn0x0vq3q5mdaxm
As always, feel free to remix, improve, and translate this guide. It is published under a permissive license, like all of my other stuff.
GM 🌅🤙
[ad_2]
Source link