mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-08-08 10:15:27 +02:00
Merge branch 'main' into dev
This commit is contained in:
70
bot.py
70
bot.py
@@ -87,10 +87,6 @@ class Bot:
|
|||||||
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
|
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
|
||||||
+ str(user.balance) + " Sats) Task: " + str(task))
|
+ str(user.balance) + " Sats) Task: " + str(task))
|
||||||
|
|
||||||
duration = 1
|
|
||||||
required_amount = get_amount_per_task(self.dvm_config.SUPPORTED_DVMS[index].TASK,
|
|
||||||
self.dvm_config, duration)
|
|
||||||
|
|
||||||
if user.isblacklisted:
|
if user.isblacklisted:
|
||||||
# For some reason an admin might blacklist npubs, e.g. for abusing the service
|
# For some reason an admin might blacklist npubs, e.g. for abusing the service
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
||||||
@@ -98,7 +94,7 @@ class Bot:
|
|||||||
"services.", None).to_event(self.keys)
|
"services.", None).to_event(self.keys)
|
||||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
|
|
||||||
elif user.balance >= required_amount or required_amount == 0:
|
else:
|
||||||
command = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "")
|
command = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "")
|
||||||
input = command.split("-")[0].rstrip()
|
input = command.split("-")[0].rstrip()
|
||||||
|
|
||||||
@@ -111,9 +107,11 @@ class Bot:
|
|||||||
tags = [i_tag.as_vec(), bid_tag.as_vec(), relays_tag.as_vec(), alt_tag.as_vec()]
|
tags = [i_tag.as_vec(), bid_tag.as_vec(), relays_tag.as_vec(), alt_tag.as_vec()]
|
||||||
|
|
||||||
remaining_text = command.replace(input, "")
|
remaining_text = command.replace(input, "")
|
||||||
params = remaining_text.rstrip().split("-")
|
print(remaining_text)
|
||||||
|
params = remaining_text.rstrip().split(" -")
|
||||||
|
|
||||||
for i in params:
|
for i in params:
|
||||||
|
print(i)
|
||||||
if i != " ":
|
if i != " ":
|
||||||
try:
|
try:
|
||||||
split = i.split(" ")
|
split = i.split(" ")
|
||||||
@@ -121,9 +119,12 @@ class Bot:
|
|||||||
print(str(param))
|
print(str(param))
|
||||||
value = str(split[1])
|
value = str(split[1])
|
||||||
print(str(value))
|
print(str(value))
|
||||||
tag = Tag.parse(["param", param, value])
|
if param == "cashu":
|
||||||
|
tag = Tag.parse([param, value])
|
||||||
|
else:
|
||||||
|
tag = Tag.parse(["param", param, value])
|
||||||
tags.append(tag.as_vec())
|
tags.append(tag.as_vec())
|
||||||
print("Added params: " + tag.as_vec())
|
print("Added params: " + str(tag.as_vec()))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
print("Couldn't add " + str(i))
|
print("Couldn't add " + str(i))
|
||||||
@@ -149,18 +150,6 @@ class Bot:
|
|||||||
|
|
||||||
send_event(encrypted_nip90request, client=self.client, dvm_config=dvm_config)
|
send_event(encrypted_nip90request, client=self.client, dvm_config=dvm_config)
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
print("Bot payment-required")
|
|
||||||
time.sleep(2.0)
|
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
|
||||||
"Balance required, please zap me with at least " +
|
|
||||||
str(int(required_amount - user.balance))
|
|
||||||
+ " Sats, then try again.",
|
|
||||||
nostr_event.id()).to_event(self.keys)
|
|
||||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)
|
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)
|
||||||
message = "DVMs that I support:\n\n"
|
message = "DVMs that I support:\n\n"
|
||||||
@@ -182,7 +171,6 @@ class Bot:
|
|||||||
print("Error in bot " + str(e))
|
print("Error in bot " + str(e))
|
||||||
|
|
||||||
def handle_nip90_feedback(nostr_event):
|
def handle_nip90_feedback(nostr_event):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
is_encrypted = False
|
is_encrypted = False
|
||||||
status = ""
|
status = ""
|
||||||
@@ -249,22 +237,32 @@ class Bot:
|
|||||||
# if we get a bolt11, we pay and move on
|
# if we get a bolt11, we pay and move on
|
||||||
user = get_or_add_user(db=self.dvm_config.DB, npub=entry["npub"],
|
user = get_or_add_user(db=self.dvm_config.DB, npub=entry["npub"],
|
||||||
client=self.client, config=self.dvm_config)
|
client=self.client, config=self.dvm_config)
|
||||||
balance = max(user.balance - amount, 0)
|
if user.balance > amount:
|
||||||
update_sql_table(db=self.dvm_config.DB, npub=user.npub, balance=balance,
|
balance = max(user.balance - amount, 0)
|
||||||
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
|
update_sql_table(db=self.dvm_config.DB, npub=user.npub, balance=balance,
|
||||||
nip05=user.nip05, lud16=user.lud16, name=user.name,
|
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
|
||||||
lastactive=Timestamp.now().as_secs())
|
nip05=user.nip05, lud16=user.lud16, name=user.name,
|
||||||
time.sleep(2.0)
|
lastactive=Timestamp.now().as_secs())
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
|
||||||
PublicKey.from_hex(entry["npub"]),
|
PublicKey.from_hex(entry["npub"]),
|
||||||
"Paid " + str(
|
"Paid " + str(
|
||||||
amount) + " Sats from balance to DVM. New balance is " +
|
amount) + " Sats from balance to DVM. New balance is " +
|
||||||
str(balance)
|
str(balance)
|
||||||
+ " Sats.\n",
|
+ " Sats.\n",
|
||||||
None).to_event(self.keys)
|
None).to_event(self.keys)
|
||||||
|
|
||||||
|
print("[" + self.NAME + "] Replying " + user.name + " with \"scheduled\" confirmation")
|
||||||
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
|
else:
|
||||||
|
print("Bot payment-required")
|
||||||
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, PublicKey.from_hex(entry["npub"]),
|
||||||
|
"Current balance: " + str(user.balance) + " Sats. Balance required, please zap me with at least " +
|
||||||
|
str(int(amount - user.balance))
|
||||||
|
+ " Sats, then try again.",
|
||||||
|
None).to_event(self.keys)
|
||||||
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
|
return
|
||||||
|
|
||||||
print("[" + self.NAME + "] Replying " + user.name + " with \"scheduled\" confirmation")
|
|
||||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
|
||||||
|
|
||||||
if len(tag.as_vec()) > 2:
|
if len(tag.as_vec()) > 2:
|
||||||
bolt11 = tag.as_vec()[2]
|
bolt11 = tag.as_vec()[2]
|
||||||
|
2
dvm.py
2
dvm.py
@@ -113,7 +113,7 @@ class DVM:
|
|||||||
cashu_redeemed = False
|
cashu_redeemed = False
|
||||||
if cashu != "":
|
if cashu != "":
|
||||||
cashu_redeemed, cashu_message = redeem_cashu(cashu, amount, self.dvm_config, self.client)
|
cashu_redeemed, cashu_message = redeem_cashu(cashu, amount, self.dvm_config, self.client)
|
||||||
if cashu_message != "":
|
if cashu_message != "success":
|
||||||
send_job_status_reaction(nip90_event, "error", False, amount, self.client, cashu_message,
|
send_job_status_reaction(nip90_event, "error", False, amount, self.client, cashu_message,
|
||||||
self.dvm_config)
|
self.dvm_config)
|
||||||
return
|
return
|
||||||
|
@@ -55,7 +55,6 @@ class Translation(DVMTaskInterface):
|
|||||||
dvm_config=dvm_config)
|
dvm_config=dvm_config)
|
||||||
text = evt.content()
|
text = evt.content()
|
||||||
|
|
||||||
|
|
||||||
elif tag.as_vec()[0] == 'param':
|
elif tag.as_vec()[0] == 'param':
|
||||||
param = tag.as_vec()[1]
|
param = tag.as_vec()[1]
|
||||||
if param == "language": # check for param type
|
if param == "language": # check for param type
|
||||||
|
@@ -171,7 +171,7 @@ def update_user_balance(db, npub, additional_sats, client, config):
|
|||||||
|
|
||||||
if config is not None:
|
if config is not None:
|
||||||
keys = Keys.from_sk_str(config.PRIVATE_KEY)
|
keys = Keys.from_sk_str(config.PRIVATE_KEY)
|
||||||
time.sleep(1.0)
|
#time.sleep(1.0)
|
||||||
|
|
||||||
message = ("Added " + str(additional_sats) + " Sats to balance. New balance is " + str(new_balance) + " Sats.")
|
message = ("Added " + str(additional_sats) + " Sats to balance. New balance is " + str(new_balance) + " Sats.")
|
||||||
|
|
||||||
|
@@ -238,14 +238,20 @@ def zap(lud16: str, amount: int, content, zapped_event: Event, keys, dvm_config,
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def parse_cashu(cashu_token):
|
def parse_cashu(cashu_token: str):
|
||||||
try:
|
try:
|
||||||
prefix = "cashuA"
|
try:
|
||||||
assert cashu_token.startswith(prefix), Exception(
|
prefix = "cashuA"
|
||||||
f"Token prefix not valid. Expected {prefix}."
|
assert cashu_token.startswith(prefix), Exception(
|
||||||
)
|
f"Token prefix not valid. Expected {prefix}."
|
||||||
token_base64 = cashu_token[len(prefix):]
|
)
|
||||||
cashu = json.loads(base64.urlsafe_b64decode(token_base64))
|
if not cashu_token.endswith("="):
|
||||||
|
cashu_token = cashu_token + "="
|
||||||
|
|
||||||
|
token_base64 = cashu_token[len(prefix):]
|
||||||
|
cashu = json.loads(base64.urlsafe_b64decode(token_base64))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
token = cashu["token"][0]
|
token = cashu["token"][0]
|
||||||
proofs = token["proofs"]
|
proofs = token["proofs"]
|
||||||
@@ -289,9 +295,9 @@ def redeem_cashu(cashu, required_amount, config, client) -> (bool, str):
|
|||||||
request = requests.post(url, data=request_body, headers=headers)
|
request = requests.post(url, data=request_body, headers=headers)
|
||||||
tree = json.loads(request.text)
|
tree = json.loads(request.text)
|
||||||
print(request.text)
|
print(request.text)
|
||||||
is_paid = tree["paid"] if tree.get("paid") else "false"
|
is_paid = tree["paid"] if tree.get("paid") else False
|
||||||
print(is_paid)
|
print(is_paid)
|
||||||
if is_paid == "true":
|
if is_paid:
|
||||||
print("token redeemed")
|
print("token redeemed")
|
||||||
return True, "success"
|
return True, "success"
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user