From 5d3a131d2f334a5db9b68a941af2a46889c86cd6 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 6 Sep 2023 11:00:36 +0330 Subject: added two new scripts, transformer and gotify --- gotify/gotify.py | 71 ++++++++++++++++++++++++ transformer/transformer.py | 133 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 gotify/gotify.py create mode 100644 transformer/transformer.py diff --git a/gotify/gotify.py b/gotify/gotify.py new file mode 100644 index 0000000..93580ea --- /dev/null +++ b/gotify/gotify.py @@ -0,0 +1,71 @@ +import irssi +import typing +import urllib +from urllib import request, parse + +IRSSI = { + "author": "terminaldweller", + "contact": "https://terminaldweller.com", + "name": "gotify", + "description": "sends push notifications via gotify", + "license": "GPL3 or newer", + "url": "https://github.com/irssi/scripts.irssi.org", +} + + +def do_push( + content: bytes, target: bytes, nick: bytes, server: irssi.IrcServer +) -> None: + gotify_token = irssi.settings_get_str(b"gotify_token").decode("utf-8") + gotify_url = irssi.settings_get_str(b"gotify_server_url").decode("utf-8") + push_priosity = irssi.settings_get_int(b"gotify_push_priority") + + form_fields = { + "title": "irssi", + "message": f"received message on {server.tag.decode('utf-8')} from {nick.decode('utf-8')} : {content.decode('utf-8')}", + "priority": push_priosity, + } + + data = parse.urlencode(form_fields) + data = data.encode("utf-8") + url = gotify_url + f"/message?token={gotify_token}" + + req = request.Request(url, data=data, method="POST") + request.urlopen(req) + + +def gotify_sig_handler(*args, **kwargs) -> None: + server = args[0] + msg = args[1] + nick = args[2] + address = args[3] + target = args[4] + do_push(msg, target, nick, server) + + +def run_on_script_load() -> None: + irssi.settings_add_bool( + b"misc", + b"transformer_debug", + False, + ) + irssi.settings_add_str( + b"misc", + b"gotify_server_url", + b"https://gotify.terminaldweller.com", + ) + irssi.settings_add_int( + b"misc", + b"gotify_push_priority", + 10, + ) + irssi.settings_add_str( + b"misc", + b"gotify_token", + b"", + ) + + irssi.signal_add(b"message private", gotify_sig_handler) + + +run_on_script_load() diff --git a/transformer/transformer.py b/transformer/transformer.py new file mode 100644 index 0000000..4c46357 --- /dev/null +++ b/transformer/transformer.py @@ -0,0 +1,133 @@ +import irssi +import json +import typing +import urllib +from urllib import request, parse + +IRSSI = { + "author": "terminaldweller", + "contact": "https://terminaldweller.com", + "name": "transformer", + "description": "transforms incoming text in the channel", + "license": "GPL3 or newer", + "url": "https://github.com/irssi/scripts.irssi.org", +} + + +def do_post(url: str, content: bytes, target: bytes, nick: bytes) -> None: + api_key = irssi.settings_get_str(b"transformer_api_key") + api_key = api_key.decode("utf-8") + model = irssi.settings_get_str(b"transformer_model") + role = irssi.settings_get_str(b"transformer_role") + temp = irssi.settings_get_int(b"transformer_temperature") + n = irssi.settings_get_int(b"transformer_n") + debug = irssi.settings_get_bool(b"transformer_debug") + prompt_system = irssi.settings_get_str(b"transformer_prompt_system") + prompt_user = irssi.settings_get_str(b"transformer_prompt_user") + + headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"} + + data = { + "model": model.decode("utf-8"), + "temperature": temp / 1000.0, + "n": n, + "messages": [ + { + "role": "system", + "content": prompt_system.decode("utf-8") + content.decode("utf-8"), + }, + { + "role": "user", + "content": prompt_user.decode("utf-8") + content.decode("utf-8"), + }, + ], + } + + result = "" + post_data = json.dumps(data).encode("utf-8") + req = request.Request(url, post_data, headers, method="POST") + try: + resp = request.urlopen(req) + json_response = json.load(resp) + window = irssi.window_find_item(target) + window.prnt( + bytes("%N%z005faf%k %9Transformed %N%Z005faf%0%N", encoding="utf-8") + ) + for choice in json_response["choices"]: + result = choice["message"]["content"] + if result != "": + window.prnt( + bytes(nick.decode("utf-8") + " >>> " + result, encoding="utf-8") + ) + except urllib.error.HTTPError as e: + resp = e.read().decode("utf-8") + json_response = json.load(resp) + print(json_response) + + +def transformer_sig_handler(*args, **kwargs) -> None: + URL = "https://api.openai.com/v1/chat/completions" + server = args[0] + msg = args[1] + nick = args[2] + address = args[3] + target = args[4] + channels = ( + irssi.settings_get_str(b"transformer_channel_list").decode("utf-8").split(" ") + ) + source = server.tag + b"/" + target + if any(source.decode("utf-8") in channel for channel in channels): + do_post(URL, msg, target, nick) + + +def run_on_script_load() -> None: + irssi.settings_add_bool( + b"misc", + b"transformer_debug", + False, + ) + irssi.settings_add_str( + b"misc", + b"transformer_api_key", + b"", + ) + irssi.settings_add_str( + b"misc", + b"transformer_model", + b"gpt-3.5-turbo", + ) + irssi.settings_add_str( + b"misc", + b"transformer_role", + b"user", + ) + irssi.settings_add_int( + b"misc", + b"transformer_temperature", + 700, + ) + irssi.settings_add_int( + b"misc", + b"transformer_n", + 1, + ) + irssi.settings_add_str( + b"misc", + b"transformer_prompt_system", + b"if you can't translate parts of the provided text use the original piece of text. the text will ocassionally inlcude URLs. if the original text is in the target language, return an empty response. Do not ask for follow up question.", + ) + irssi.settings_add_str( + b"misc", + b"transformer_prompt_user", + b"translate this into english: ", + ) + irssi.settings_add_str( + b"misc", + b"transformer_channel_list", + b"", + ) + + irssi.signal_add(b"message public", transformer_sig_handler) + + +run_on_script_load() -- cgit v1.2.3