Function
action
v1.0.2
Nano GPT Receive Nano
Automates receiving Nano cryptocurrency and displaying wallet info through OpenWebUI for use with nano-gpt.com.
Function ID
nanogpt_receive_nano
Creator
@elliott
Downloads
31+

Function Content
python
"""
title: Nano GPT Revice Nano and info
author: Elliott Groves
version: 1.0.2
date: 2024-10-10
description: Nano GPT recive Nano balance button for openwebui.
author_url: https://github.com/Orciotrox/Nano-GPT.com_OpenWebUI
funding_url: https://github.com/Orciotrox/Nano-GPT.com_OpenWebUI
nano_donation_address: nano_1pkmodta8fg8ti39pr1doe1mjbwo8cu3c9mt5u38d73d5t57d9nmgmnheifk
icon_url: data:image/svg+xml;base64, IDxzdmcgIHZlcnNpb249IjEuMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiAgd2lkdGg9IjMwMC4wMDAwMDBwdCIgaGVpZ2h0PSIzMDAuMDAwMDAwcHQiIHZpZXdCb3g9IjAgMCAzMDAuMDAwMDAwIDMwMC4wMDAwMDAiICBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSJ4TWlkWU1pZCBtZWV0Ij4gIDxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKDAuMDAwMDAwLDMwMC4wMDAwMDApIHNjYWxlKDAuMTAwMDAwLC0wLjEwMDAwMCkiIGZpbGw9IiM5YjliOWIiIHN0cm9rZT0ibm9uZSI+IDxwYXRoIGQ9Ik0xMzE1IDI5NDkgYy01NjEgLTc3IC0xMDE4IC00NTAgLTEyMDQgLTk4MSAtMTAwIC0yODcgLTEwMiAtNjMwIC02IC05MjQgMzMgLTEwMCAxMDMgLTI0OSAxNTMgLTMyOCAxMTMgLTE3NiAyODMgLTM0NiA0NTggLTQ1OCAxMzcgLTg4IDM2NiAtMTc0IDUzOSAtMjAzIDEzMSAtMjIgMzQ5IC0yMiA0ODAgMCA2MTIgMTAzIDEwOTUgNTg1IDEyMDEgMTIwMCAyMiAxMjkgMjIgMzQ3IDAgNDc4IC0yNiAxNTUgLTU2IDIwO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pydantic import BaseModel, Field
from typing import Optional
import requests
import asyncio


class Action:
    class Valves(BaseModel):
        NANO_GPT_API_KEY: str = Field(
            default="Your Nano GPT API Key",
            description="API key for authenticating requests to the Nano GPT API.",
        )
        NANO_GPT_API_BASE_URL: str = Field(
            default="https://nano-gpt.com/api",
            description="Base URL for accessing Nano GPT API endpoints.",
        )

    def __init__(self):
        self.valves = self.Valves()

    async def action(
        self,
        body: dict,
        __user__=None,
        __event_emitter__=None,
        __event_call__=None,
    ) -> Optional[dict]:
        print("action: Receive Nano")

        try:
            # Call the receive Nano method
            response_data = self.receive_nano()

            if not response_data:
                raise Exception("No data in the response")

            headers = {
                "x-api-key": self.valves.NANO_GPT_API_KEY,
                "Content-Type": "application/json",
            }

            # Check balance and deposit address
            rbo = requests.post(
                url=f"{self.valves.NANO_GPT_API_BASE_URL}/check-nano-balance",
                headers=headers,
                timeout=30,
            )
            if rbo.status_code == 200:
                rb = rbo.json()
                balance = rb.get("balance", "Error")
                resultdep = rb.get("nanoDepositAddress", "Error")
            else:
                balance = "Error"
                resultdep = "Error"

            balance_message = f"\n- **🪙 Balance 🪙:** {balance} Nano"
            deposit_message = f"\n- **🏛️ Nano Deposit Address 🏛️:** `{resultdep}`"

            # Determine the message based on received blocks
            if response_data.get("receivedBlocks"):
                message = "Successfully received Nano"
                # message = "\n- **📌 Response 📌:** Successfully received Nano"
            elif not response_data.get("receivedBlocks") and not response_data.get(
                "failedReceivableBlocks"
            ):
                message = "No Nano to receive"
                # message = "\n- **📌 Response 📌:** No Nano to receive"
            else:
                message = "Error Unexpected response"
                # message = "\n- **📌 Response 📌:** Error Unexpected response"

            urlqr = f"https://api.qrserver.com/v1/create-qr-code/?data={resultdep}&size=300x300"
            table = f"\n\n---\n## Attempting to receive Nano:\n
\n📌 Receive Response 📌\n\n{message}\n\n
\n
\n🪙 Your Balance 🪙\n\n{balance} Nano\n\n
\n
\n🏛️ Deposit Address 🏛️\n\n`{resultdep}`\n\n
\n
\n🏛️ Deposit QR Code 🏛️\n\n![🏛️ Your Nano Deposit QR Code 🏛️]({urlqr})\n\n
\n
\n🎁 Support the Project 🎁\n\n[Nano-GPT.com Referal Invite](https://nano-gpt.com/invite/8AQjX8kA)\n\n
" message = table # Emit status messages back to the UI await __event_emitter__( { "type": "message", "data": {"content": message}, } ) except Exception as e: error_message = f"\n\n---\n ## Error receiving Nano:\n\n``` ⚠️ Error ⚠️\n{str(e)}\n\n```\n\n###### Try resetting the API Key for Nano-gpt.com in openwebui.\n\n###### Sign Up for Nano-GPT.com & Support the project:\n###### [Nano-GPT.com Signup & Referal Invite](https://nano-gpt.com/invite/8AQjX8kA)\n---" print(error_message) await __event_emitter__( { "type": "message", "data": {"content": message}, } ) def receive_nano(self): # Function to interact with the Nano GPT API and receive Nano url = f"{self.valves.NANO_GPT_API_BASE_URL}/receive-nano" headers = { "x-api-key": self.valves.NANO_GPT_API_KEY, "Content-Type": "application/json", } try: print("Attempting to receive Nano...") response = requests.post(url, headers=headers) response.raise_for_status() return response.json() except requests.RequestException as e: print(f"Error receiving Nano: {e}") return None