Files

74 lines
2.4 KiB
Python

import torch
import requests
import io
from PIL import Image
import numpy as np
class TelegramNotificationNode:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"bot_token": ("STRING", {"default": "", "multiline": False}),
"chat_id": ("STRING", {"default": "", "multiline": False}),
"text": ("STRING", {"default": "ComfyUI notification!", "multiline": True}),
},
"optional": {
"image": ("IMAGE",),
}
}
RETURN_TYPES = ()
FUNCTION = "send_notification"
OUTPUT_NODE = True
CATEGORY = "notifications"
def send_notification(self, bot_token, chat_id, text, image=None):
if not bot_token or not chat_id:
print("Telegram Notification Error: Bot Token and Chat ID are required.")
return ()
try:
if image is not None:
# ComfyUI image is (Batch, Height, Width, Channels) tensor
# We'll take the first image in the batch if there are multiple
img_tensor = image[0]
img_np = (img_tensor.cpu().numpy() * 255).astype(np.uint8)
img_pil = Image.fromarray(img_np)
# Save to memory
bio = io.BytesIO()
bio.name = 'image.png'
img_pil.save(bio, 'PNG')
bio.seek(0)
url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
files = {'photo': bio}
data = {'chat_id': chat_id, 'caption': text}
response = requests.post(url, files=files, data=data)
else:
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
data = {'chat_id': chat_id, 'text': text}
response = requests.post(url, data=data)
if response.status_code != 200:
print(f"Telegram Notification Error: {response.text}")
else:
print("Telegram Notification Sent Successfully!")
except Exception as e:
print(f"Telegram Notification Exception: {str(e)}")
return ()
NODE_CLASS_MAPPINGS = {
"TelegramNotificationNode": TelegramNotificationNode
}
NODE_DISPLAY_NAME_MAPPINGS = {
"TelegramNotificationNode": "Telegram Notification Bot"
}