import requests
from PIL import Image
import numpy as np
import io
import base64
import json

def send_msg(token, chat_id, text):
   url_req = "" + token + "/sendMessage" + "?chat_id=" + chat_id + "&text=" + text 
   results = requests.get(url_req)

def send_photo(token, chat_id, image, text):
    data = {"chat_id": chat_id, "caption": text}
    url = "" % token
    ret =, data=data, files={"photo": image})

def send_photos(token, chat_id, images, text):
    data = {"chat_id": chat_id, "caption": text, "media": []}
    url = "" % token
    media = []
    for i, image in enumerate(images):
        media.append({"media": base64.b64encode(image).decode('utf-8'), "type":"photo"})
    data['media'] = media
    ret =, data=data)

class TelegramNotification:
    A example node

    Class methods
    INPUT_TYPES (dict): 
        Tell the main program input parameters of nodes.

    RETURN_TYPES (`tuple`): 
        The type of each element in the output tulple.
    RETURN_NAMES (`tuple`):
        Optional: The name of each output in the output tulple.
    FUNCTION (`str`):
        The name of the entry-point method. For example, if `FUNCTION = "execute"` then it will run Example().execute()
    OUTPUT_NODE ([`bool`]):
        If this node is an output node that outputs a result/image from the graph. The SaveImage node is an example.
        The backend iterates on these output nodes and tries to execute all their parents if their parent graph is properly connected.
        Assumed to be False if not present.
    CATEGORY (`str`):
        The category the node should appear in the UI.
    execute(s) -> tuple || None:
        The entry point method. The name of this method must be the same as the value of property `FUNCTION`.
        For example, if `FUNCTION = "execute"` then this method's name must be `execute`, if `FUNCTION = "foo"` then it must be `foo`.
    def __init__(self):
    def INPUT_TYPES(s):
            Return a dictionary which contains config for all input fields.
            Some types (string): "MODEL", "VAE", "CLIP", "CONDITIONING", "LATENT", "IMAGE", "INT", "STRING", "FLOAT".
            Input types "INT", "STRING" or "FLOAT" are special values for fields on the node.
            The type can be a list for selection.

            Returns: `dict`:
                - Key input_fields_group (`string`): Can be either required, hidden or optional. A node class must have property `required`
                - Value input_fields (`dict`): Contains input fields config:
                    * Key field_name (`string`): Name of a entry-point method's argument
                    * Value field_config (`tuple`):
                        + First value is a string indicate the type of field or a list for selection.
                        + Secound value is a config for type "INT", "STRING" or "FLOAT".
        return {
            "required": {
                "images": ("IMAGE",),
		"telegram_bot_token": ("STRING", {
		    "multiline": False,
		    "default": ""
		"group_id": ("STRING", {
		    "multiline": False,
		    "default": ""

    #RETURN_NAMES = ("image_output_name",)

    FUNCTION = "notify"

    OUTPUT_NODE = True

    CATEGORY = "Example"

    def notify(self, images, telegram_bot_token, group_id):
        #send_msg(telegram_bot_token, group_id, "Generation finished")
        for image in images:
            i = 255. * image.cpu().numpy()
            img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
            with io.BytesIO() as output:
      , format="PNG")
                contents = output.getvalue()
                send_photo(telegram_bot_token, group_id, contents, "Generation finished")
        return (images, )

# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
    "TelegramNotify": TelegramNotification

# A dictionary that contains the friendly/humanly readable titles for the nodes
    "TelegramNotify": "Example Node"

Изменить пасту