mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-17 21:31:52 +01:00
Merge branch 'main' into dev
This commit is contained in:
commit
d126589493
70
bot.py
70
bot.py
@ -87,10 +87,6 @@ class Bot:
|
||||
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
|
||||
+ 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:
|
||||
# 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(),
|
||||
@ -98,7 +94,7 @@ class Bot:
|
||||
"services.", None).to_event(self.keys)
|
||||
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] + " ", "")
|
||||
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()]
|
||||
|
||||
remaining_text = command.replace(input, "")
|
||||
params = remaining_text.rstrip().split("-")
|
||||
print(remaining_text)
|
||||
params = remaining_text.rstrip().split(" -")
|
||||
|
||||
for i in params:
|
||||
print(i)
|
||||
if i != " ":
|
||||
try:
|
||||
split = i.split(" ")
|
||||
@ -121,9 +119,12 @@ class Bot:
|
||||
print(str(param))
|
||||
value = str(split[1])
|
||||
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())
|
||||
print("Added params: " + tag.as_vec())
|
||||
print("Added params: " + str(tag.as_vec()))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Couldn't add " + str(i))
|
||||
@ -149,18 +150,6 @@ class Bot:
|
||||
|
||||
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:
|
||||
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)
|
||||
message = "DVMs that I support:\n\n"
|
||||
@ -182,7 +171,6 @@ class Bot:
|
||||
print("Error in bot " + str(e))
|
||||
|
||||
def handle_nip90_feedback(nostr_event):
|
||||
|
||||
try:
|
||||
is_encrypted = False
|
||||
status = ""
|
||||
@ -249,22 +237,32 @@ class Bot:
|
||||
# if we get a bolt11, we pay and move on
|
||||
user = get_or_add_user(db=self.dvm_config.DB, npub=entry["npub"],
|
||||
client=self.client, config=self.dvm_config)
|
||||
balance = max(user.balance - amount, 0)
|
||||
update_sql_table(db=self.dvm_config.DB, npub=user.npub, balance=balance,
|
||||
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
|
||||
nip05=user.nip05, lud16=user.lud16, name=user.name,
|
||||
lastactive=Timestamp.now().as_secs())
|
||||
time.sleep(2.0)
|
||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
|
||||
PublicKey.from_hex(entry["npub"]),
|
||||
"Paid " + str(
|
||||
amount) + " Sats from balance to DVM. New balance is " +
|
||||
str(balance)
|
||||
+ " Sats.\n",
|
||||
None).to_event(self.keys)
|
||||
if user.balance > amount:
|
||||
balance = max(user.balance - amount, 0)
|
||||
update_sql_table(db=self.dvm_config.DB, npub=user.npub, balance=balance,
|
||||
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
|
||||
nip05=user.nip05, lud16=user.lud16, name=user.name,
|
||||
lastactive=Timestamp.now().as_secs())
|
||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
|
||||
PublicKey.from_hex(entry["npub"]),
|
||||
"Paid " + str(
|
||||
amount) + " Sats from balance to DVM. New balance is " +
|
||||
str(balance)
|
||||
+ " Sats.\n",
|
||||
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:
|
||||
bolt11 = tag.as_vec()[2]
|
||||
|
2
dvm.py
2
dvm.py
@ -113,7 +113,7 @@ class DVM:
|
||||
cashu_redeemed = False
|
||||
if cashu != "":
|
||||
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,
|
||||
self.dvm_config)
|
||||
return
|
||||
|
@ -55,7 +55,6 @@ class Translation(DVMTaskInterface):
|
||||
dvm_config=dvm_config)
|
||||
text = evt.content()
|
||||
|
||||
|
||||
elif tag.as_vec()[0] == 'param':
|
||||
param = tag.as_vec()[1]
|
||||
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:
|
||||
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.")
|
||||
|
||||
|
@ -238,14 +238,20 @@ def zap(lud16: str, amount: int, content, zapped_event: Event, keys, dvm_config,
|
||||
return None
|
||||
|
||||
|
||||
def parse_cashu(cashu_token):
|
||||
def parse_cashu(cashu_token: str):
|
||||
try:
|
||||
prefix = "cashuA"
|
||||
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))
|
||||
try:
|
||||
prefix = "cashuA"
|
||||
assert cashu_token.startswith(prefix), Exception(
|
||||
f"Token prefix not valid. Expected {prefix}."
|
||||
)
|
||||
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]
|
||||
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)
|
||||
tree = json.loads(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)
|
||||
if is_paid == "true":
|
||||
if is_paid:
|
||||
print("token redeemed")
|
||||
return True, "success"
|
||||
else:
|
||||
|
Loading…
x
Reference in New Issue
Block a user