mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-12-04 22:01:05 +01:00
allow change lnaddress, remove one step of tutorial.
This commit is contained in:
82
tutorials/02_run_dvm.py
Normal file
82
tutorials/02_run_dvm.py
Normal file
@@ -0,0 +1,82 @@
|
||||
# Welcome back, this time we don't use a notebook, but we run an actual Python Script.
|
||||
# Go to the very bottom of this code and replace the identifier with the one from last exercise.
|
||||
# We use a GenericDVM kind to start with. Now what's this? We have many predefined tasks in the task folder, but
|
||||
# the genericDVM gives you some control for simple manipulation without caring about the tasks. Important is that
|
||||
# we set the Kind of the GenericDVM. In Line 28 you see that we give it Kind 5050 (Text generation).
|
||||
# On https://www.data-vending-machines.org/ there's an overview on all current kinds.
|
||||
# On https://github.com/nostr-protocol/data-vending-machines/ you can make a PR for your own kind, if you come up with one later.
|
||||
# Check the run_dvm function for more explanations
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import dotenv
|
||||
|
||||
from nostr_dvm.tasks.generic_dvm import GenericDVM
|
||||
from nostr_sdk import Kind, Keys
|
||||
from nostr_dvm.utils.admin_utils import AdminConfig
|
||||
from nostr_dvm.utils.dvmconfig import build_default_config
|
||||
from nostr_dvm.utils.nip89_utils import NIP89Config
|
||||
from nostr_dvm.utils.zap_utils import change_ln_address
|
||||
|
||||
|
||||
def run_dvm(identifier):
|
||||
# You have seen this one before, we did this in tutorial 2. This function will either create or load. the parameters of our DVMConfig.
|
||||
# Make sure you replace the identifier down in the main function with the one you generated in tutorial 2, or we will create a new one here.
|
||||
dvm_config = build_default_config(identifier)
|
||||
# As we will use a GenericDVM we need to give it a kind. Here we use kind 5050 (Text Generation) as we want to reply with some simple text.
|
||||
# There is a bunch of predefined DVMs in tasks that already have a kind set, but as we use the genericDVM we have to manually set it here.
|
||||
dvm_config.KIND = Kind(5050)
|
||||
|
||||
# We can set options that we can later use in our process function. They are stored in a simple JSON
|
||||
options = {
|
||||
"some_option": "#RunDVM",
|
||||
}
|
||||
# We give the DVM a human readable name
|
||||
name = "My very first DVM"
|
||||
# Next we initalize a GenericDVM with the name and the dvm_config and the options we just created, as well as
|
||||
# an empty AdminConfig() and NIP89Config(). We will check these out in later tutorials, so don't worry about them now.
|
||||
dvm = GenericDVM(name=name, dvm_config=dvm_config, options=options,
|
||||
nip89config=NIP89Config(), admin_config=AdminConfig())
|
||||
|
||||
|
||||
# Normally we would define the dvm interface as we do in the tasks folder (we will do it later in the tutorials as well,
|
||||
# but here is a small hack to quickly manipulate what our dvm will do.
|
||||
async def process(request_form):
|
||||
# First we always parse the options from our request_form, that is build internally in the create_request_from_nostr_event function.
|
||||
options = dvm.set_options(request_form)
|
||||
# We build our result we are giving back from some text
|
||||
result = "The result of the DVM is: "
|
||||
# and the option we defined above and handed over to our DVM (some_option)
|
||||
result += options["some_option"]
|
||||
print(result)
|
||||
# Then we simply return the result
|
||||
return result
|
||||
|
||||
dvm.process = process # now we simply overwrite our DVM's process function with the one we defined here.
|
||||
# and finally we run the DVM #RunDVM
|
||||
dvm.run()
|
||||
|
||||
# When the DVM is running you should see a blue message with the name and the public key in bech32 and hex format.
|
||||
# For the next exercise, copy the Hex key, and let this DVM run, you will need it :)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
#We open the .env file we created before.
|
||||
env_path = Path('.env')
|
||||
if not env_path.is_file():
|
||||
with open('.env', 'w') as f:
|
||||
print("Writing new .env file")
|
||||
f.write('')
|
||||
if env_path.is_file():
|
||||
print(f'loading environment from {env_path.resolve()}')
|
||||
dotenv.load_dotenv(env_path, verbose=True, override=True)
|
||||
else:
|
||||
raise FileNotFoundError(f'.env file not found at {env_path} ')
|
||||
|
||||
# Replace the identifier with the one from the last notebook, or a new dvmconfig will be stored
|
||||
identifier = "tutorial01"
|
||||
|
||||
# psst, you can change your lightning address here:
|
||||
# change_ln_address(identifier, "a_cool_new_address")
|
||||
|
||||
run_dvm(identifier)
|
||||
Reference in New Issue
Block a user